Skip to content

Commit 37446ed

Browse files
committed
Fix LFMCMC unit tests
1 parent f582ba7 commit 37446ed

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

Diff for: inst/tinytest/test-lfmcmc.R

+10-9
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ model_seed <- 122
55

66
# Create and run SIR Model for LFMCMC simulation -------------------------------
77
model_sir <- ModelSIR(name = "COVID-19", prevalence = .1,
8-
transmission_rate = .9, recovery_rate = .3)
8+
transmission_rate = .3, recovery_rate = .3)
99
agents_smallworld(model_sir, n = 1000, k = 5, d = FALSE, p = 0.01)
1010
verbose_off(model_sir)
1111
run(model_sir, ndays = 50, seed = model_seed)
1212

13+
# Check init of LFMCMC model without epiworld model ----------------------------
14+
expect_silent(lfmcmc_nomodel <- LFMCMC())
15+
1316
# Check bad init of LFMCMC model -----------------------------------------------
14-
expect_error(lfmcmc_bad <- LFMCMC(), 'argument "model" is missing')
1517
expect_error(lfmcmc_bad <- LFMCMC(c("not_a_model")), "model should be of class 'epiworld_model'")
1618

1719
# Create LFMCMC model ----------------------------------------------------------
@@ -22,7 +24,7 @@ expect_inherits(lfmcmc_model, "epiworld_lfmcmc")
2224
expect_length(class(lfmcmc_model), 1)
2325

2426
# Extract observed data from the model
25-
obs_data <- unname(as.integer(get_today_total(model_sir)))
27+
obs_data <- get_today_total(model_sir)
2628

2729
expect_silent(set_observed_data(lfmcmc_model, obs_data))
2830

@@ -31,20 +33,19 @@ simfun <- function(params) {
3133
set_param(model_sir, "Recovery rate", params[1])
3234
set_param(model_sir, "Transmission rate", params[2])
3335
run(model_sir, ndays = 50)
34-
res <- unname(as.integer(get_today_total(model_sir)))
36+
res <- get_today_total(model_sir)
3537
return(res)
3638
}
3739

3840
sumfun <- function(dat) { return(dat) }
3941

4042
propfun <- function(params_prev) {
41-
res <- params_prev + rnorm(length(params_prev), )
43+
res <- plogis(qlogis(params_prev) + rnorm(length(params_prev)))
4244
return(res)
4345
}
4446

4547
kernelfun <- function(stats_now, stats_obs, epsilon) {
46-
ans <- sum(mapply(function(v1, v2) (v1 - v2)^2, stats_obs, stats_now))
47-
return(ifelse(sqrt(ans) < epsilon, 1.0, 0.0))
48+
dnorm(sqrt(sum((stats_now - stats_obs)^2)))
4849
}
4950

5051
# Check adding functions to LFMCMC
@@ -72,8 +73,8 @@ expect_silent(set_par_names(lfmcmc_model, c("Immune recovery", "Infectiousness")
7273

7374
expect_stdout(print(lfmcmc_model))
7475

75-
expect_equal(get_stats_mean(lfmcmc_model), c(4.45, 2.6135, 992.4365))
76-
expect_equal(get_params_mean(lfmcmc_model), c(11.58421, 18.96851), tolerance = 0.00001)
76+
expect_equal(get_stats_mean(lfmcmc_model), c(284.7140, 0.8485, 713.9375))
77+
expect_equal(get_params_mean(lfmcmc_model), c(0.3132901, 0.2782186), tolerance = 0.00001)
7778

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

0 commit comments

Comments
 (0)