From 060f5805200ea3bd24453edc38e887e7776bd121 Mon Sep 17 00:00:00 2001 From: James Azam Date: Thu, 7 Mar 2024 17:50:27 +0000 Subject: [PATCH] Add tests --- tests/testthat/test-likelihood.R | 58 ++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/tests/testthat/test-likelihood.R b/tests/testthat/test-likelihood.R index 8844085e..16a93832 100644 --- a/tests/testthat/test-likelihood.R +++ b/tests/testthat/test-likelihood.R @@ -1,3 +1,4 @@ +# Example using hypothetical data chains <- c(1, 1, 4, 7) set.seed(12) test_that( @@ -91,6 +92,54 @@ test_that( } ) +test_that("likelihood() works with epichains and epichains_summary objects", { + # Simulate an 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 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 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 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( @@ -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" + ) })