Skip to content

Commit

Permalink
add input data plot
Browse files Browse the repository at this point in the history
imports from ggplot2
  • Loading branch information
hillalex committed Oct 23, 2024
1 parent a4e479a commit da15415
Show file tree
Hide file tree
Showing 8 changed files with 2,459 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
9 changes: 9 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,13 @@ importFrom(data.table,.N)
importFrom(data.table,.NGRP)
importFrom(data.table,.SD)
importFrom(data.table,data.table)
importFrom(ggplot2,aes)
importFrom(ggplot2,facet_wrap)
importFrom(ggplot2,geom_line)
importFrom(ggplot2,geom_point)
importFrom(ggplot2,geom_ribbon)
importFrom(ggplot2,geom_smooth)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,guide_legend)
importFrom(ggplot2,guides)
useDynLib(epikinetics, .registration = TRUE)
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
1 change: 1 addition & 0 deletions R/epikinetics-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#' @importFrom data.table .NGRP
#' @importFrom data.table .SD
#' @importFrom data.table data.table
#' @importFrom ggplot2 aes facet_wrap geom_point geom_ribbon geom_line geom_smooth ggplot guides guide_legend
#' @useDynLib epikinetics, .registration = TRUE
## usethis namespace: end

Expand Down
17 changes: 12 additions & 5 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,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 +44,12 @@ plot_prior_predictive <- function(priors,
}
plot
}

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"))
}
14 changes: 14 additions & 0 deletions man/biokinetics.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2,408 changes: 2,408 additions & 0 deletions tests/testthat/_snaps/plots/inputdata.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions tests/testthat/test-plots.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ test_that("Prior predictions from model are the same", {
plot <- mod$plot_prior_predictive(tmax = 400, n_draws = 500)
vdiffr::expect_doppelganger("priorpredictive", plot)
})

test_that("Plotted data is are the same", {
skip_on_ci()
data <- data.table::fread(system.file("delta_full.rds", package = "epikinetics"))
mod <- biokinetics$new(data = data)
plot <- mod$plot_data()
vdiffr::expect_doppelganger("inputdata", plot)
})

0 comments on commit da15415

Please sign in to comment.