Skip to content

Commit

Permalink
Add tests to test-sir.R
Browse files Browse the repository at this point in the history
  • Loading branch information
apulsipher committed Nov 13, 2024
1 parent 5b646c4 commit 4cebbf4
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions inst/tinytest/test-sir.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Run just this file with command:
# - tinytest::run_test_file("inst/tinytest/test-sir.R")
# Adding a Small world population without queuing ------------------------------
sir_0 <- ModelSIR(
name = "COVID-19",
Expand All @@ -6,23 +8,45 @@ sir_0 <- ModelSIR(
recovery_rate = .3
)

agents_smallworld(
# Check model initialized correctly
expect_inherits(sir_0,"epiworld_sir")
expect_inherits(sir_0,"epiworld_model")
# Check can't plot before model is run
# TODO: Is this actually expected behavior?
expect_error(plot(sir_0))

expect_silent(agents_smallworld(
sir_0,
n = 50000,
k = 5,
d = FALSE,
p = .01
)
))

# Initializing
queuing_off(sir_0)

# Running and printing
# Running
verbose_off(sir_0)
run(sir_0, ndays = 50, seed = 1912)

# Check plots without errors or warnings
# TODO: Fix this check, it produces warnings but succeeds
expect_silent(plot(sir_0))

tmat_0 <- get_transition_probability(sir_0)

expected_dimnames <- list(
c("Susceptible", "Infected", "Recovered"),
c("Susceptible", "Infected", "Recovered")
)
expected_dim <- c(3L, 3L)

expect_equal(dimnames(tmat_0), expected_dimnames)
expect_equal(dim(tmat_0), expected_dim)

# Creating a SIR model with queuing --------------------------------------------
# TODO: Can we simply reset the model and run again?
sir_1 <- ModelSIR(
name = "COVID-19",
prevalence = .01,
Expand All @@ -40,16 +64,17 @@ agents_smallworld(
)

# Running and printing
verbose_off(sir_1)
run(sir_1, ndays = 50, seed = 1912)

tmat_1 <- get_transition_probability(sir_1)

# Expected
tmat_expected <- structure(
c(
0.962139427661896, 0, 0,
0.0378605611622334, 0.70049238204956,
0, 0, 0.299507558345795, 1
0.961843, 0, 0,
0.03815696, 0.69985167, 0,
0, 0.3001483, 1
),
dim = c(3L, 3L),
dimnames = list(
Expand All @@ -58,8 +83,8 @@ tmat_expected <- structure(
)
)

expect_equivalent(tmat_0, tmat_1)
expect_equivalent(tmat_0, tmat_expected)
expect_equivalent(tmat_1, tmat_expected)


# Check matches expected output
expect_equal(tmat_0, tmat_expected, tolerance = 0.0000001)
# Check SIR produces same output with and without queuing
# TODO: why is this the case?
expect_equal(tmat_0, tmat_1)

0 comments on commit 4cebbf4

Please sign in to comment.