Skip to content

Commit

Permalink
better skips to avoid long check times
Browse files Browse the repository at this point in the history
  • Loading branch information
wjakethompson committed Jan 29, 2024
1 parent a5f64f7 commit e8abf5a
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 38 deletions.
38 changes: 26 additions & 12 deletions tests/testthat/test-ecpe.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
if (!identical(Sys.getenv("NOT_CRAN"), "true")) skip("No MCMC on CRAN")

out <- capture.output(
suppressMessages(
cmds_ecpe_lcdm <- measr_dcm(
data = ecpe_data, missing = NA, qmatrix = ecpe_qmatrix,
resp_id = "resp_id", item_id = "item_id", type = "lcdm",
method = "optim", seed = 63277, backend = "cmdstanr",
prior = c(prior(uniform(-15, 15), class = "intercept"),
prior(uniform(0, 15), class = "maineffect"),
prior(uniform(-15, 15), class = "interaction")))
if (!identical(Sys.getenv("NOT_CRAN"), "true")) {
skip("No MCMC on CRAN")
} else {
out <- capture.output(
suppressMessages(
cmds_ecpe_lcdm <- measr_dcm(
data = ecpe_data, missing = NA, qmatrix = ecpe_qmatrix,
resp_id = "resp_id", item_id = "item_id", type = "lcdm",
method = "optim", seed = 63277, backend = "cmdstanr",
prior = c(prior(uniform(-15, 15), class = "intercept"),
prior(uniform(0, 15), class = "maineffect"),
prior(uniform(-15, 15), class = "interaction")))
)
)
)
}

test_that("lcdm model works for ecpe", {
skip_on_cran()

expect_s3_class(cmds_ecpe_lcdm, "measrfit")
expect_s3_class(cmds_ecpe_lcdm, "measrdcm")
expect_equal(names(cmds_ecpe_lcdm),
Expand Down Expand Up @@ -64,6 +68,8 @@ test_that("lcdm model works for ecpe", {
})

test_that("extract ecpe", {
skip_on_cran()

lcdm_param <- measr_extract(cmds_ecpe_lcdm, "item_param")
all_param <- get_parameters(ecpe_qmatrix, item_id = "item_id",
type = "lcdm") %>%
Expand Down Expand Up @@ -105,6 +111,8 @@ test_that("extract ecpe", {
})

test_that("ecpe probabilities are accurate", {
skip_on_cran()

ecpe_preds <- predict(cmds_ecpe_lcdm, newdata = ecpe_data,
resp_id = "resp_id")

Expand Down Expand Up @@ -176,6 +184,8 @@ test_that("ecpe probabilities are accurate", {
})

test_that("ecpe reliability", {
skip_on_cran()

ecpe_reli <- reliability(cmds_ecpe_lcdm)

# list naming
Expand Down Expand Up @@ -247,6 +257,8 @@ test_that("ecpe reliability", {
})

test_that("m2 calculation is correct", {
skip_on_cran()

m2 <- fit_m2(cmds_ecpe_lcdm)

expect_equal(m2$m2, 507.0756, tolerance = 0.1)
Expand All @@ -262,6 +274,8 @@ test_that("m2 calculation is correct", {
})

test_that("mcmc requirements error", {
skip_on_cran()

err <- rlang::catch_cnd(add_fit(cmds_ecpe_lcdm, method = "ppmc"))
expect_s3_class(err, "error_bad_method")
expect_match(err$message, "`method = \"mcmc\"`")
Expand Down
74 changes: 48 additions & 26 deletions tests/testthat/test-mcmc.R
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
if (!identical(Sys.getenv("NOT_CRAN"), "true")) skip("No MCMC on CRAN")

out <- capture.output(
suppressMessages(
cmds_mdm_lcdm <- measr_dcm(
data = mdm_data, missing = NA, qmatrix = mdm_qmatrix,
resp_id = "respondent", item_id = "item", type = "lcdm",
method = "mcmc", seed = 63277, backend = "cmdstanr",
iter_sampling = 500, iter_warmup = 1000, chains = 2,
parallel_chains = 2,
prior = c(prior(uniform(-15, 15), class = "intercept"),
prior(uniform(0, 15), class = "maineffect")))
if (!identical(Sys.getenv("NOT_CRAN"), "true")) {
skip("No MCMC on CRAN")
} else {
out <- capture.output(
suppressMessages(
cmds_mdm_lcdm <- measr_dcm(
data = mdm_data, missing = NA, qmatrix = mdm_qmatrix,
resp_id = "respondent", item_id = "item", type = "lcdm",
method = "mcmc", seed = 63277, backend = "cmdstanr",
iter_sampling = 500, iter_warmup = 1000, chains = 2,
parallel_chains = 2,
prior = c(prior(uniform(-15, 15), class = "intercept"),
prior(uniform(0, 15), class = "maineffect")))
)
)
)

out <- capture.output(
suppressMessages(
cmds_mdm_dina <- measr_dcm(
data = mdm_data, missing = NA, qmatrix = mdm_qmatrix,
resp_id = "respondent", item_id = "item", type = "dina",
attribute_structure = "independent",
method = "mcmc", seed = 63277, backend = "rstan",
iter = 1500, warmup = 1000, chains = 2,
cores = 2,
prior = c(prior(beta(5, 17), class = "slip"),
prior(beta(5, 17), class = "guess")))

out <- capture.output(
suppressMessages(
cmds_mdm_dina <- measr_dcm(
data = mdm_data, missing = NA, qmatrix = mdm_qmatrix,
resp_id = "respondent", item_id = "item", type = "dina",
attribute_structure = "independent",
method = "mcmc", seed = 63277, backend = "rstan",
iter = 1500, warmup = 1000, chains = 2,
cores = 2,
prior = c(prior(beta(5, 17), class = "slip"),
prior(beta(5, 17), class = "guess")))
)
)
)
}

test_that("as_draws works", {
skip_on_cran()

draws <- as_draws(cmds_mdm_dina)
expect_s3_class(draws, "draws_array")

Expand All @@ -48,6 +52,8 @@ test_that("as_draws works", {
})

test_that("get_mcmc_draws works as expected", {
skip_on_cran()

test_draws <- get_mcmc_draws(cmds_mdm_lcdm)
expect_equal(posterior::ndraws(test_draws), 1000)
expect_equal(posterior::nvariables(test_draws), 10)
Expand All @@ -60,13 +66,17 @@ test_that("get_mcmc_draws works as expected", {
})

test_that("log_lik is calculated correctly", {
skip_on_cran()

log_lik <- prep_loglik_array(cmds_mdm_lcdm)

# expected value from 2-class LCA fit in Mplus
expect_equal(sum(apply(log_lik, c(3), mean)), -331.764, tolerance = 1.000)
})

test_that("loo and waic work", {
skip_on_cran()

err <- rlang::catch_cnd(loo(rstn_dina))
expect_s3_class(err, "error_bad_method")
expect_match(err$message, "`method = \"mcmc\"`")
Expand All @@ -83,6 +93,8 @@ test_that("loo and waic work", {
})

test_that("loo and waic can be added to model", {
skip_on_cran()

err <- rlang::catch_cnd(measr_extract(cmds_mdm_lcdm, "loo"))
expect_s3_class(err, "rlang_error")
expect_match(err$message, "LOO criterion must be added")
Expand Down Expand Up @@ -114,6 +126,8 @@ test_that("loo and waic can be added to model", {
})

test_that("model comparisons work", {
skip_on_cran()

err <- rlang::catch_cnd(loo_compare(cmds_mdm_lcdm, cmds_mdm_dina))
expect_s3_class(err, "error_missing_criterion")
expect_match(err$message, "does not contain a precomputed")
Expand Down Expand Up @@ -147,6 +161,8 @@ test_that("model comparisons work", {
})

test_that("ppmc works", {
skip_on_cran()

test_ppmc <- fit_ppmc(cmds_mdm_lcdm, model_fit = character(),
item_fit = character())
expect_equal(test_ppmc, list())
Expand Down Expand Up @@ -214,6 +230,8 @@ test_that("ppmc works", {
})

test_that("ppmc extraction errors", {
skip_on_cran()

err <- rlang::catch_cnd(measr_extract(cmds_mdm_lcdm, "ppmc_raw_score"))
expect_s3_class(err, "rlang_error")
expect_match(err$message, "Model fit information must be added")
Expand All @@ -237,6 +255,8 @@ test_that("ppmc extraction errors", {
})

test_that("model fit can be added", {
skip_on_cran()

test_model <- cmds_mdm_dina
expect_equal(test_model$fit, list())

Expand Down Expand Up @@ -321,6 +341,8 @@ test_that("model fit can be added", {
})

test_that("respondent probabilities are correct", {
skip_on_cran()

mdm_preds <- predict(cmds_mdm_lcdm, newdata = mdm_data,
resp_id = "respondent", summary = TRUE)
mdm_full_preds <- predict(cmds_mdm_lcdm, summary = FALSE)
Expand Down

0 comments on commit e8abf5a

Please sign in to comment.