Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test for trivial minpuc argument in quickpred #634

Merged
merged 2 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion R/quickpred.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
64 changes: 64 additions & 0 deletions tests/testthat/test-quickpred.R
Original file line number Diff line number Diff line change
@@ -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)

})
Loading