Skip to content

Commit

Permalink
condiitonal log transform and shape based on censoring
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Oct 30, 2024
1 parent cd21e6e commit ea4ed85
Show file tree
Hide file tree
Showing 8 changed files with 7,111 additions and 4,547 deletions.
1 change: 1 addition & 0 deletions R/biokinetics.R
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ biokinetics <- R6::R6Class(

class(dt_out) <- append("biokinetics_population_trajectories", class(dt_out))
attr(dt_out, "summarised") <- summarise
attr(dt_out, "scale") <- private$scale
attr(dt_out, "covariates") <- private$all_formula_vars
dt_out
},
Expand Down
29 changes: 24 additions & 5 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,18 @@ plot.biokinetics_priors <- function(x,
geom_ribbon(aes(x = t, ymin = lo, ymax = hi), alpha = 0.5)

if (!is.null(data)) {
plot <- plot + geom_point(data = data, aes(x = time_since_last_exp, y = value))
if (!("censored" %in% colnames(data))) {
data$censored <- FALSE

Check warning on line 51 in R/plot.R

View check run for this annotation

Codecov / codecov/patch

R/plot.R#L51

Added line #L51 was not covered by tests
} else {
data$censored <- data$censored != 0
}
dat <- data[time_since_last_exp <= tmax,]
plot <- plot +
geom_point(data = dat, size = 0.5,
aes(x = time_since_last_exp,
y = value,
shape = censored)) +
guides(shape = guide_legend(title = "Censored"))
}
plot
}
Expand Down Expand Up @@ -93,18 +104,26 @@ plot.biokinetics_population_trajectories <- function(x, ..., data = NULL) {
geom_line(aes(x = time_since_last_exp, y = mu,
colour = titre_type, group = .draw), alpha = 0.1, linewidth = 0.1)
}

if (!is.null(data)) {
validate_required_cols(data)
if (!("censored" %in% colnames(data))) {
data$censored <- FALSE

Check warning on line 110 in R/plot.R

View check run for this annotation

Codecov / codecov/patch

R/plot.R#L110

Added line #L110 was not covered by tests
} else {
data$censored <- data$censored != 0
}
plot <- plot +
geom_point(data = data,
aes(x = as.integer(day - last_exp_day, units = "days"),
y = value), size = 0.5, alpha = 0.5)
y = value, shape = censored), size = 0.5, alpha = 0.5)
}
if (attr(x, "scale") == "natural") {
plot <- plot + scale_y_continuous(trans = "log2")
}
plot +
scale_y_continuous(trans = "log2") +
facet_wrap(eval(parse(text = facet_formula(covariates)))) +
guides(fill = guide_legend(title = "Titre type"), colour = "none")
guides(fill = guide_legend(title = "Titre type"),
colour = "none",
shape = guide_legend(title = "Censored"))
}

facet_formula <- function(covariates) {
Expand Down
1 change: 0 additions & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
convert_log2_scale <- function(
dt_in, vars_to_transform = "value",
simplify_limits = TRUE) {

dt_out <- data.table::copy(dt_in)
for (var in vars_to_transform) {
if (simplify_limits == TRUE) {
Expand Down
2 changes: 1 addition & 1 deletion src/stan/antibody_kinetics_main.stan
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ model {
// Likelihood for observations at lower limit: log2(value/5) = 0
target += normal_lcdf(0 | mu[cens_lo_idx], sigma);

// Censoring at log2(value/5) = 7, originally value = 2560
// Censoring at log2(value/5) = 9, originally value = 2560
target += normal_lccdf(9 | mu[cens_hi_idx], sigma);

// Covariate-level mean priors, parameterised from previous studies
Expand Down
4,540 changes: 2,274 additions & 2,266 deletions tests/testthat/_snaps/plots/populationtrajectories-data.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2,512 changes: 2,512 additions & 0 deletions tests/testthat/_snaps/plots/populationtrajectories-logscale.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4,551 changes: 2,278 additions & 2,273 deletions tests/testthat/_snaps/plots/priorpredictive.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 21 additions & 1 deletion tests/testthat/test-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,25 @@ test_that("Can plot population trajectories with data", {
mod <- biokinetics$new(data = data, covariate_formula = ~0 + infection_history)
fit <- mod$fit()
trajectories <- mod$simulate_population_trajectories()
vdiffr::expect_doppelganger("populationtrajectories_data", plot(trajectories, data = data))
plot <- plot(trajectories, data = data)
expect_equal(length(plot$scales$scales), 1)
vdiffr::expect_doppelganger("populationtrajectories_data", plot)
})

test_that("Can plot population trajectories with log scale input data", {
local_mocked_bindings(
stan_package_model = mock_model, .package = "instantiate"
)
# note that this is using a pre-fitted model with very few iterations, so the
# fits won't look very good
data <- data.table::fread(system.file("delta_full.rds", package = "epikinetics"))
data <- convert_log2_scale(data)
mod <- biokinetics$new(data = data,
covariate_formula = ~0 + infection_history,
scale = "log")
fit <- mod$fit()
trajectories <- mod$simulate_population_trajectories()
plot <- plot(trajectories, data = data)
expect_equal(length(plot$scales$scales), 0)
vdiffr::expect_doppelganger("populationtrajectories_logscale", plot)
})

0 comments on commit ea4ed85

Please sign in to comment.