Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmbaazam committed Mar 8, 2024
1 parent e77d3fa commit 060f580
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/testthat/test-likelihood.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Example using hypothetical data
chains <- c(1, 1, 4, 7)
set.seed(12)
test_that(
Expand Down Expand Up @@ -91,6 +92,54 @@ test_that(
}
)

test_that("likelihood() works with epichains and epichains_summary objects", {
# Simulate an <epichains_tree> object
set.seed(32)
chains_tree_eg <- simulate_chains(
index_cases = 10,
pop = 100,
percent_immune = 0,
statistic = "size",
offspring_dist = rpois,
stat_max = 10,
generation_time = function(n) rep(3, n),
lambda = 0.9
)
# Simulate an <epichains_summary> object
set.seed(32)
chains_summary_eg <- simulate_summary(
index_cases = 10,
pop = 100,
percent_immune = 0,
statistic = "size",
offspring_dist = rpois,
stat_max = 10,
lambda = 0.9
)
# Use the simulated <epichains_tree> object to calculate likelihood
expect_equal(
likelihood(
chains = chains_tree_eg,
statistic = "size",
offspring_dist = rpois,
lambda = 0.9,
stat_max = 10
),
-23.538996774
)
# Use the simulated <epichains_summary> object to calculate likelihood
expect_equal(
likelihood(
chains = chains_summary_eg,
statistic = "size",
offspring_dist = rpois,
lambda = 0.9,
stat_max = 10
),
-23.538997
)
})

test_that("Likelihoods are numerically correct", {
expect_identical(
round(
Expand Down Expand Up @@ -192,4 +241,13 @@ test_that("Errors are thrown", {
),
"Must be of type"
)
expect_error(
likelihood(
chains = as.factor(chains),
statistic = "size",
offspring_dist = rpois,
lambda = 0.5
),
"Must be of type"
)
})

0 comments on commit 060f580

Please sign in to comment.