From 8a2864b6ff0762d206dfeb7b91e30195529090a6 Mon Sep 17 00:00:00 2001 From: stephematician Date: Fri, 5 Apr 2024 08:39:51 +1100 Subject: [PATCH 1/2] Fixes #633 use any() in test for trivial minpuc --- R/quickpred.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/quickpred.R b/R/quickpred.R index 569cf13e..f785d31e 100644 --- a/R/quickpred.R +++ b/R/quickpred.R @@ -106,7 +106,7 @@ quickpred <- function(data, mincor = 0.1, minpuc = 0, include = "", predictorMatrix[maxc > mincor] <- 1 # exclude predictors with a percentage usable cases below minpuc - if (minpuc != 0) { + if (any(minpuc != 0)) { p <- md.pairs(data) puc <- p$mr / (p$mr + p$mm) predictorMatrix[puc < minpuc] <- 0 From 72bb81d101bafc9bdefba14d7b928b8a120f3f20 Mon Sep 17 00:00:00 2001 From: stephematician Date: Fri, 5 Apr 2024 08:41:11 +1100 Subject: [PATCH 2/2] Add basic tests for quickpred Tests for acceptable arguments Tests for output format --- tests/testthat/test-quickpred.R | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 tests/testthat/test-quickpred.R diff --git a/tests/testthat/test-quickpred.R b/tests/testthat/test-quickpred.R new file mode 100644 index 00000000..ca96549a --- /dev/null +++ b/tests/testthat/test-quickpred.R @@ -0,0 +1,64 @@ +context("quickpred") + +test_that("returns square binary matrix", { + + predictorMatrix <- quickpred(nhanes) + + expect_is(predictorMatrix, 'matrix') + expect_equal(nrow(predictorMatrix), ncol(predictorMatrix)) + expect_in(predictorMatrix, c(0, 1)) + +}) + +test_that("mincor supports scalar, vector, matrix", { + + n_col <- ncol(nhanes) + expect_in(quickpred(nhanes, mincor=0), c(0, 1)) + expect_in(quickpred(nhanes, mincor=1), 0) + expect_in(quickpred(nhanes, mincor=rep(0.1, n_col)), c(0, 1)) + expect_in( + quickpred(nhanes, mincor=matrix(rep(0.1, n_col*n_col), ncol=n_col)), + c(0, 1) + ) + +}) + +test_that("minpuc supports scalar, vector, matrix", { + + n_col <- ncol(nhanes) + expect_in(quickpred(nhanes, minpuc=0), c(0, 1)) + expect_in(quickpred(nhanes, minpuc=rep(0.1, n_col)), c(0, 1)) + expect_in( + quickpred(nhanes, minpuc=matrix(rep(0.1, n_col*n_col), ncol=n_col)), + c(0, 1) + ) + +}) + +test_that("include one or more variables", { + + result_include_bmi <- quickpred(nhanes, include="bmi") + has_missing <- apply(is.na(nhanes), 2, any) + not_bmi <- setdiff(names(nhanes)[has_missing], "bmi") + expect_in(result_include_bmi[not_bmi, "bmi"], 1) + + expect_in(quickpred(nhanes, include=names(nhanes)), c(0, 1)) + + n_col <- ncol(nhanes) + result_include_all <- quickpred(nhanes, include=names(nhanes)) + expect_in( + result_include_all[has_missing, ] - (1 - diag(n_col)[has_missing,]), + 0 + ) + +}) + +test_that("exclude one or more variables", { + + result_exclude_age <- quickpred(nhanes, exclude="age") + expect_in(result_exclude_age[, "age"], 0) + + result_exclude_all <- quickpred(nhanes, exclude=names(nhanes)) + expect_in(result_exclude_all, 0) + +})