diff --git a/R/variance_thresholding.R b/R/variance_thresholding.R index 7eb9ee0..f2ff3f0 100644 --- a/R/variance_thresholding.R +++ b/R/variance_thresholding.R @@ -6,7 +6,7 @@ #' @param data tibble. The features to select from #' @param threshold double. The variance threshold #' -#' @return vector. The indexes of selected features +#' @return double vector. The indexes of selected features #' @export #' @examples #' data <- data.frame(x1=c(1,2,3,4,5), x2=c(0,0,0,0,0), x3=c(1,1,1,1,1)) @@ -22,15 +22,15 @@ variance_thresholding <- function(data, threshold = 0) { } selected_feature_indexes <- c() - i <- 1 - for (name in names(data)) { + for (i in 1:length(names(data))) { + # Get variance of a column var_i <- stats::var(data[,i]) if (var_i > threshold) { + # Add the column to the selected pile if + # its variance is above the threshold selected_feature_indexes <- c(selected_feature_indexes, i) } - - i <- i + 1 } selected_feature_indexes diff --git a/tests/testthat/test-variance_thresholding.R b/tests/testthat/test-variance_thresholding.R index 7211ad7..565c72d 100644 --- a/tests/testthat/test-variance_thresholding.R +++ b/tests/testthat/test-variance_thresholding.R @@ -15,7 +15,7 @@ testthat::test_that("relevant features remain", { ) feature_indexes <- variance_thresholding(data) - testthat::expect_identical(feature_indexes, c(1, 4)) + testthat::expect_equal(feature_indexes, c(1, 4)) }) #' @@ -23,20 +23,28 @@ testthat::test_that("relevant features remain", { #' #' @NoRd testthat::test_that("appropriate features are dropped with higher threshold", { - data <- dplyr::tibble( + data_a <- dplyr::tibble( a = c(1,2,3), b = c(2,2,2), c = c('123', '123', '123'), d = c('123', '14', '1') ) - feature_indexes <- variance_thresholding(data, threshold = 2) - testthat::expect_identical(feature_indexes, c(4)) + feature_indexes_a <- variance_thresholding(data_a, threshold = 2) + testthat::expect_equal(feature_indexes_a, c(4)) + + data_b <- dplyr::tibble( + a = c(1,2,3), + b = c(2,2,2) + ) + + feature_indexes_b <- variance_thresholding(data_b, threshold = 100) + testthat::expect_equal(feature_indexes_b, NULL) }) #' #' Make sure the function blows up when invalid inputs are past in for -#' both the threshold and X +#' both the threshold and X #' #' @NoRd testthat::test_that("inputs checking", {