Skip to content
Merged
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
31 changes: 19 additions & 12 deletions tests/testthat/test-sample_covariates_mvtnorm.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,40 @@ test_that("samples from exponential distribution when exponential = TRUE", {
})

test_that("conditional argument filters data before sampling", {
# TODO: Think of a good way to test this, since the output can include values
# outside the conditional range.
skip()
dat <- data.frame(
AGE = c(20, 30, 40, 50, 60),
WT = c(45, 55, 65, 75, 85)
)
cndl <- list(WT = c(50, 70))
out <- sample_covariates_mvtnorm(
n <- 1000
out_uncond <- sample_covariates_mvtnorm(
data = dat,
n_subjects = n
)
out_cond <- sample_covariates_mvtnorm(
data = dat,
conditional = cndl,
n_subjects = 50
n_subjects = n
)
expect_true(all(out$WT >= 50 & out$WT <= 70))
outside_uncond <- sum(out_uncond$WT < 50 | out_uncond$WT > 70)/n
outside_cond <- sum(out_cond$WT < 50 | out_cond$WT > 70)/n
expect_true(outside_uncond > outside_cond * 3) # uncond should be higher by wide margin
})

test_that("conditional argument works with multiple variables", {
# TODO: Think of a good way to test this, since the output can include values
# outside the conditional range.
skip()
dat <- data.frame(
age = 18:65,
height = seq(150, 197, length.out = 48),
weight = rnorm(48)
)
cndl <- list(age = c(25, 35), height = c(160, 180))
out <- sample_covariates_mvtnorm(dat, n_subjects = 50, conditional = cndl)
expect_true(all(out$age >= 25 & out$age <= 35))
expect_true(all(out$height >= 160 & out$height <= 180))
n <- 1000
out_uncond <- sample_covariates_mvtnorm(dat, n_subjects = n)
out_cond <- sample_covariates_mvtnorm(dat, n_subjects = n, conditional = cndl)
outside_age_uncond <- sum(out_uncond$age < 25 | out_uncond$age > 35)/n
outside_age_cond <- sum(out_cond$age < 25 | out_cond$age > 35)/n
outside_height_uncond <- sum(out_uncond$height < 160 | out_uncond$height > 180)/n
outside_height_cond <- sum(out_cond$height < 160 | out_cond$height > 180)/n
expect_true(outside_age_uncond > outside_age_cond * 3)
expect_true(outside_height_uncond > outside_height_cond * 3)
})