From 1f6cfb8d20a0ac6da105648a95454a53386b7b4e Mon Sep 17 00:00:00 2001 From: Evan Thompson Date: Wed, 24 Feb 2021 01:04:15 -0800 Subject: [PATCH] Fix disconnect between bv and population in selection() (#24) 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. --- R/selection.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/R/selection.R b/R/selection.R index f8f32d057..e38068dc9 100644 --- a/R/selection.R +++ b/R/selection.R @@ -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) @@ -219,4 +222,4 @@ selection <- function(population, sampleSize, units = "records", algorithm = "ra # Add class 'jfaSelection' to the result. class(result) <- "jfaSelection" return(result) -} \ No newline at end of file +}