diff --git a/R/plot.R b/R/plot.R index ed11e5b..aded9bf 100644 --- a/R/plot.R +++ b/R/plot.R @@ -73,10 +73,13 @@ plot_data <- function(data, covariates) { #' a data.table containing raw data as provided to the biokinetics model. #' @export plot.biokinetics_population_trajectories <- function(x, ...) { + covariates <- attr(x, "covariates") if (!attr(x, "summarised")) { by <- setdiff(colnames(x), c("t0_pop", "tp_pop", "ts_pop", - "m1_pop", "m2_pop", "m3_pop", - ".draw", "mu")) + "m1_pop", "m2_pop", "m3_pop", + "beta_t0", "beta_tp", "beta_ts", + "beta_m1", "beta_m2", "beta_m3", + ".draw", "mu")) x <- summarise_draws( x, column_name = "mu", by = by) } @@ -101,8 +104,9 @@ plot.biokinetics_population_trajectories <- function(x, ...) { aes(x = as.integer(day - last_exp_day, units = "days"), y = value), size = 0.5, alpha = 0.5) } - plot + scale_y_continuous(trans = "log2") + - facet_wrap(eval(parse(text = facet_formula(attr(x, "covariates"))))) + + plot + + scale_y_continuous(trans = "log2") + + facet_wrap(eval(parse(text = facet_formula(covariates)))) + guides(fill = guide_legend(title = "Titre type"), colour = "none") } diff --git a/tests/testthat/_snaps/plots/multiplecovariates.svg b/tests/testthat/_snaps/plots/multiplecovariates.svg new file mode 100644 index 0000000..64d645d --- /dev/null +++ b/tests/testthat/_snaps/plots/multiplecovariates.svg @@ -0,0 +1,685 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Delta + +NA + +BNT162b2 + + + + + + + + + + +Delta + +NA + +mRNA1273 + + + + + + + + + + +Delta + +NA + +others + + + + + + + + + + + + + + + + + + + + + + + + + + + + +Ancestral + +NA + +mRNA1273 + + + + + + + + + + +Ancestral + +NA + +others + + + + + + + + + + +Delta + +Infection naive + +NA + + + + + + + + + + +Delta + +Previously infected (Pre-Omicron) + +NA + + + + + + + + + + +Delta + +NA + +AZD1222 + + + + + + + + + + +Alpha + +NA + +others + + + + + + + + + + +Ancestral + +Infection naive + +NA + + + + + + + + + + +Ancestral + +Previously infected (Pre-Omicron) + +NA + + + + + + + + + + +Ancestral + +NA + +AZD1222 + + + + + + + + + + +Ancestral + +NA + +BNT162b2 + + + + + + + + + + +Alpha + +Infection naive + +NA + + + + + + + + + + +Alpha + +Previously infected (Pre-Omicron) + +NA + + + + + + + + + + +Alpha + +NA + +AZD1222 + + + + + + + + + + +Alpha + +NA + +BNT162b2 + + + + + + + + + + +Alpha + +NA + +mRNA1273 + + + + + + +0 +50 +100 +150 + + + + +0 +50 +100 +150 + + + + +0 +50 +100 +150 + + + + +0 +50 +100 +150 + + + + +0 +50 +100 +150 +64 +2048 +65536 +2097152 + + + + +64 +2048 +65536 +2097152 + + + + +64 +2048 +65536 +2097152 + + + + +64 +2048 +65536 +2097152 + + + + +time_since_last_exp +me + +Titre type + + + + + + +Alpha +Ancestral +Delta +multiplecovariates + + diff --git a/tests/testthat/test-plots.R b/tests/testthat/test-plots.R index 32723b3..1074cc6 100644 --- a/tests/testthat/test-plots.R +++ b/tests/testthat/test-plots.R @@ -54,6 +54,10 @@ mock_model <- function(name, package) { list(sample = function(x, ...) readRDS(test_path("testdata", "testdraws.rds"))) } +mock_model_multiple_covariates <- function(name, package) { + list(sample = function(x, ...) readRDS(test_path("testdata", "testdraws_multiplecovariates.rds"))) +} + test_that("Summarised and un-summarised population trajectories give same plots", { # note that this is using a pre-fitted model with very few iterations, so the # fits won't look very good @@ -69,6 +73,21 @@ test_that("Summarised and un-summarised population trajectories give same plots" vdiffr::expect_doppelganger("populationtrajectories", plot(unsummarised_trajectories)) }) +test_that("Summarised and un-summarised population trajectories give same plots for multiple covariats", { + # note that this is using a pre-fitted model with very few iterations, so the + # fits won't look very good + local_mocked_bindings( + stan_package_model = mock_model_multiple_covariates, .package = "instantiate" + ) + mod <- biokinetics$new(file_path = system.file("delta_full.rds", package = "epikinetics"), + covariate_formula = ~0 + infection_history + last_vax_type) + mod$fit() + trajectories <- mod$simulate_population_trajectories(summarise = TRUE) + unsummarised_trajectories <- mod$simulate_population_trajectories(summarise = FALSE) + vdiffr::expect_doppelganger("multiplecovariates", plot(trajectories)) + vdiffr::expect_doppelganger("multiplecovariates", plot(unsummarised_trajectories)) +}) + test_that("Can plot population trajectories with data", { local_mocked_bindings( stan_package_model = mock_model, .package = "instantiate"