-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from seroanalytics/plotting
Plotting functions
- Loading branch information
Showing
23 changed files
with
18,259 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,17 @@ Title: Biomarker Kinetics Modelling | |
Version: 0.0.0.9000 | ||
Authors@R: c(person("Timothy", "Russell", email = "[email protected]", role = c("aut")), | ||
person("Alex", "Hill", email = "[email protected]", role = c("aut", "cre"))) | ||
Description: Fit kinetic curves to biomarker data, using a Bayesian hierarchical model | ||
Description: Fit kinetic curves to biomarker data, using a Bayesian hierarchical model. | ||
License: GPL (>= 3) | ||
Encoding: UTF-8 | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.3.1 | ||
RoxygenNote: 7.3.2 | ||
Imports: | ||
cmdstanr, | ||
data.table, | ||
forcats, | ||
fs, | ||
ggplot2, | ||
instantiate, | ||
logger, | ||
mosaic, | ||
|
@@ -26,11 +27,11 @@ Additional_repositories: | |
SystemRequirements: CmdStan (https://mc-stan.org/users/interfaces/cmdstan) | ||
Suggests: | ||
dplyr, | ||
ggplot2, | ||
knitr, | ||
lubridate, | ||
rmarkdown, | ||
testthat | ||
testthat, | ||
vdiffr | ||
VignetteBuilder: knitr | ||
LinkingTo: | ||
cpp11 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#' @title Simulate biomarker kinetics predicted by the given biokinetics priors | ||
#' and optionally compare to a dataset. | ||
#' @export | ||
#' @description Simulate trajectories by drawing random samples from the given | ||
#' priors for each parameter in the biokinetics model. | ||
#' @return A ggplot2 object. | ||
#' @param x A named list of type 'biokinetics_priors'. | ||
#' @param \dots Further arguments passed to the method. | ||
#' @param tmax Integer. The number of time points in each simulated trajectory. Default 150. | ||
#' @param n_draws Integer. The number of trajectories to simulate. Default 2000. | ||
#' @param data Optional data.frame with columns time_since_last_exp and value. The raw data to compare to. | ||
plot.biokinetics_priors <- function(x, | ||
..., | ||
tmax = 150, | ||
n_draws = 2000, | ||
data = NULL) { | ||
|
||
# Declare variables to suppress notes when compiling package | ||
# https://github.com/Rdatatable/data.table/issues/850#issuecomment-259466153 | ||
t0 <- tp <- ts <- m1 <- m2 <- m3 <- NULL | ||
time_since_last_exp <- me <- lo <- hi <- value <- mu <- NULL | ||
|
||
if (!is.null(data)) { | ||
validate_required_cols(data, c("time_since_last_exp", "value")) | ||
} | ||
params <- data.table( | ||
t0 = stats::rnorm(n_draws, x$mu_t0, x$sigma_t0), # titre value at t0 | ||
tp = stats::rnorm(n_draws, x$mu_tp, x$sigma_tp), # time of peak | ||
ts = stats::rnorm(n_draws, x$mu_ts, x$sigma_ts), # time of set point | ||
m1 = stats::rnorm(n_draws, x$mu_m1, x$sigma_m1), # gradient 1 | ||
m2 = stats::rnorm(n_draws, x$mu_m2, x$sigma_m2), # gradient 2 | ||
m3 = stats::rnorm(n_draws, x$mu_m3, x$sigma_m3) # gradient 3 | ||
) | ||
|
||
times <- data.table(t = 1:tmax) | ||
params_and_times <- times[, as.list(params), by = times] | ||
|
||
params_and_times[, mu := biokinetics_simulate_trajectory(t, t0, tp, ts, m1, m2, m3), | ||
by = c("t", "t0", "tp", "ts", "m1", "m2", "m3")] | ||
|
||
summary <- params_and_times[, .(me = stats::quantile(mu, 0.5, names = FALSE), | ||
lo = stats::quantile(mu, 0.025, names = FALSE), | ||
hi = stats::quantile(mu, 0.975, names = FALSE)), by = t] | ||
|
||
plot <- ggplot(summary) + | ||
geom_line(aes(x = t, y = me)) + | ||
geom_ribbon(aes(x = t, ymin = lo, ymax = hi), alpha = 0.5) | ||
|
||
if (!is.null(data)) { | ||
add_censored_indicator(data) | ||
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 | ||
} | ||
|
||
plot_sero_data <- function(data, covariates = character(0)) { | ||
validate_required_cols(data, c("time_since_last_exp", "value", "titre_type")) | ||
# Declare variables to suppress notes when compiling package | ||
# https://github.com/Rdatatable/data.table/issues/850#issuecomment-259466153 | ||
time_since_last_exp <- value <- titre_type <- NULL | ||
|
||
ggplot(data) + | ||
geom_point(aes(x = time_since_last_exp, y = value, colour = titre_type)) + | ||
geom_smooth(aes(x = time_since_last_exp, y = value, colour = titre_type)) + | ||
facet_wrap(eval(parse(text = facet_formula(covariates)))) + | ||
guides(colour = guide_legend(title = "Titre type")) | ||
} | ||
|
||
#' Plot method for "biokinetics_population_trajectories" class | ||
#' | ||
#' @param x An object of class "biokinetics_population_trajectories". These are | ||
#' generated by running biokinetics$simulate_population_trajectories(). See | ||
#' \href{../../epikinetics/html/biokinetics.html#method-biokinetics-simulate_population_trajectories}{\code{biokinetics$simulate_population_trajectories()}} | ||
#' @param \dots Further arguments passed to the method. | ||
#' @param data Optional data.table containing raw data as provided to the biokinetics model. | ||
#' @export | ||
plot.biokinetics_population_trajectories <- function(x, ..., data = NULL) { | ||
covariates <- attr(x, "covariates") | ||
|
||
# Declare variables to suppress notes when compiling package | ||
# https://github.com/Rdatatable/data.table/issues/850#issuecomment-259466153 | ||
time_since_last_exp <- value <- me <- titre_type <- lo <- hi <- NULL | ||
day <- last_exp_day <- NULL | ||
|
||
if (attr(x, "summarised")) { | ||
plot <- ggplot(x) + | ||
geom_line(aes(x = time_since_last_exp, y = me, colour = titre_type)) + | ||
geom_ribbon(aes(x = time_since_last_exp, | ||
ymin = lo, | ||
ymax = hi, | ||
fill = titre_type), alpha = 0.5) | ||
} else { | ||
plot <- ggplot(x) + | ||
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) | ||
add_censored_indicator(data) | ||
plot <- plot + | ||
geom_point(data = data, | ||
aes(x = as.integer(day - last_exp_day, units = "days"), | ||
y = value, shape = censored), size = 0.5, alpha = 0.5) | ||
} | ||
if (attr(x, "scale") == "natural") { | ||
plot <- plot + scale_y_continuous(trans = "log2") | ||
} | ||
plot + | ||
facet_wrap(eval(parse(text = facet_formula(covariates)))) + | ||
guides(fill = guide_legend(title = "Titre type"), | ||
colour = "none", | ||
shape = guide_legend(title = "Censored")) | ||
} | ||
|
||
facet_formula <- function(covariates) { | ||
paste("~", paste(c("titre_type", covariates), collapse = "+")) | ||
} | ||
|
||
add_censored_indicator <- function(data) { | ||
if (!("censored" %in% colnames(data))) { | ||
# censored is an optional column in input data | ||
# if not present, treat all points as uncensored | ||
data[, censored:= FALSE] | ||
} else { | ||
data[, censored:= censored != 0] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.