Skip to content

Commit

Permalink
add input data plot
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Oct 21, 2024
1 parent 9842a31 commit 34502ad
Show file tree
Hide file tree
Showing 5 changed files with 2,437 additions and 6 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Imports:
data.table,
forcats,
fs,
ggplot2,
instantiate,
logger,
mosaic,
Expand All @@ -26,11 +27,11 @@ Additional_repositories:
SystemRequirements: CmdStan (https://mc-stan.org/users/interfaces/cmdstan)
Suggests:
dplyr,
ggplot2,
knitr,
lubridate,
rmarkdown,
testthat
vdiffr
VignetteBuilder: knitr
LinkingTo:
cpp11
Expand Down
5 changes: 5 additions & 0 deletions R/biokinetics.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ biokinetics <- R6::R6Class(
n_draws = n_draws,
data = private$data)
},
#' @description Plot model input data with a smoothing function.
#' @return A ggplot2 object.
plot_data = function() {
plot_data(private$data)
},
#' @description View the data that is passed to the stan model, for debugging purposes.
#' @return A list of arguments that will be passed to the stan model.
get_stan_data = function() {
Expand Down
19 changes: 14 additions & 5 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @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 t_since_last_exp and value. The raw data to compare to.
#' @importFrom ggplot2 ggplot aes geom_point geom_ribbon geom_line
plot_prior_predictive <- function(priors,
tmax = 150,
n_draws = 2000,
Expand All @@ -31,11 +32,9 @@ plot_prior_predictive <- function(priors,
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 %>%
group_by(t) %>%
summarise(me = quantile(mu, 0.5, names = FALSE),
lo = quantile(mu, 0.025, names = FALSE),
hi = quantile(mu, 0.975, names = FALSE))
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)) +
Expand All @@ -46,3 +45,13 @@ plot_prior_predictive <- function(priors,
}
plot
}

#' @importFrom ggplot2 ggplot aes facet_wrap geom_point geom_smooth guides guide_legend
plot_data <- function(data) {
validate_required_cols(data, c("t_since_last_exp", "value", "titre_type"))
ggplot(data) +
geom_point(aes(x = t_since_last_exp, y = value, colour = titre_type)) +
geom_smooth(aes(x = t_since_last_exp, y = value, colour = titre_type)) +
facet_wrap(~titre_type) +
guides(colour = guide_legend(title = "Titre type"))
}
Loading

0 comments on commit 34502ad

Please sign in to comment.