Skip to content

Commit

Permalink
Fix disconnect between bv and population in selection() (#24)
Browse files Browse the repository at this point in the history
In `selection()`, if `population` is sorted or modified, `bv` still retained the old ordering and data. The resulting MUS is overweighted towards small values and/or still contains negative values. This proposed fix keeps `bv` updated when `population` is modified.
  • Loading branch information
alvanson authored Feb 24, 2021
1 parent 0f6b95c commit 1f6cfb8
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions R/selection.R
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,15 @@ selection <- function(population, sampleSize, units = "records", algorithm = "ra
bv <- population[, bookValues]
if(units == "mus" && sampleSize > sum(bv))
stop("Cannot take a sample larger than the population value")
if(ordered && !is.null(bv))
if(ordered && !is.null(bv)) {
population <- population[order(bv, decreasing = !ascending), ]
bv <- population[, bookValues]
}
if(!is.null(bv) && any(bv < 0)){
warning("The book values contain negative values, these are removed from the data")
negativeValues <- which(bv < 0)
population <- population[-negativeValues, ]
bv <- population[, bookValues]
}
# Set a seed for reproducibility
set.seed(seed)
Expand Down Expand Up @@ -219,4 +222,4 @@ selection <- function(population, sampleSize, units = "records", algorithm = "ra
# Add class 'jfaSelection' to the result.
class(result) <- "jfaSelection"
return(result)
}
}

0 comments on commit 1f6cfb8

Please sign in to comment.