Skip to content

Commit

Permalink
Add tests for factory methods and missing parameters to test-lfmcmc.R
Browse files Browse the repository at this point in the history
  • Loading branch information
apulsipher committed Nov 13, 2024
1 parent 60982d7 commit 3bf57ca
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
56 changes: 53 additions & 3 deletions inst/tinytest/test-lfmcmc.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ agents_smallworld(model_sir, n = 1000, k = 5, d = FALSE, p = 0.01)
verbose_off(model_sir)
run(model_sir, ndays = 50, seed = model_seed)

# Check bad init of LFMCMC model -----------------------------------------------
expect_error(lfmcmc_bad <- LFMCMC(), 'argument "model" is missing')
expect_error(lfmcmc_bad <- LFMCMC(c("not_a_model")), "model should be of class 'epiworld_model'")

# Create LFMCMC model ----------------------------------------------------------
lfmcmc_model <- LFMCMC(model_sir)
expect_silent(lfmcmc_model <- LFMCMC(model_sir))

# Check initialization
expect_inherits(lfmcmc_model, "epiworld_lfmcmc")
Expand Down Expand Up @@ -48,7 +52,7 @@ expect_silent(set_summary_fun(lfmcmc_model, sumfun))
expect_silent(set_proposal_fun(lfmcmc_model, propfun))
expect_silent(set_kernel_fun(lfmcmc_model, kernelfun))

# Create LFMCMC simulation -----------------------------------------------------
# Run LFMCMC simulation --------------------------------------------------------
# Initial parameters
par0 <- as.double(c(0.1, 0.5))
n_samp <- 2000
Expand All @@ -65,4 +69,50 @@ expect_silent(run_lfmcmc(
expect_silent(set_stats_names(lfmcmc_model, get_states(model_sir)))
expect_silent(set_par_names(lfmcmc_model, c("Immune recovery", "Infectiousness")))

expect_stdout(print(lfmcmc_model))
expect_stdout(print(lfmcmc_model))

# Check LFMCMC using factory functions -----------------------------------------
expect_silent(use_proposal_norm_reflective(lfmcmc_model))
expect_silent(use_kernel_fun_gaussian(lfmcmc_model))

expect_silent(run_lfmcmc(
lfmcmc = lfmcmc_model,
params_init_ = par0,
n_samples_ = n_samp,
epsilon_ = epsil,
seed = model_seed
))

# Check running LFMCMC with missing parameters ---------------------------------
expect_silent(run_lfmcmc(
lfmcmc = lfmcmc_model,
params_init_ = par0,
n_samples_ = n_samp,
epsilon_ = epsil
))

expect_error(run_lfmcmc(
lfmcmc = lfmcmc_model,
params_init_ = par0,
n_samples_ = n_samp
))

expect_error(run_lfmcmc(
lfmcmc = lfmcmc_model,
params_init_ = par0,
epsilon_ = epsil
))

expect_error(run_lfmcmc(
lfmcmc = lfmcmc_model,
n_samples_ = n_samp,
epsilon_ = epsil
))

expect_error(run_lfmcmc(
params_init_ = par0,
n_samples_ = n_samp,
epsilon_ = epsil
))

expect_error(run_lfmcmc(lfmcmc = lfmcmc_model))
8 changes: 4 additions & 4 deletions inst/tinytest/test-sir.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ test_tmat_matches_expected <- function(tmat) {
}

# Create small world population SIR Model --------------------------------------
sir_0 <- ModelSIR(
expect_silent(sir_0 <- ModelSIR(
name = "COVID-19",
prevalence = .01,
transmission_rate = .9,
recovery_rate = .3
)
))

# Check model initialization
expect_inherits(sir_0,"epiworld_sir")
expect_inherits(sir_0,"epiworld_model")
expect_inherits(sir_0, "epiworld_sir")
expect_inherits(sir_0, "epiworld_model")
expect_silent(agents_smallworld(
sir_0,
n = 50000,
Expand Down

0 comments on commit 3bf57ca

Please sign in to comment.