Skip to content

Commit

Permalink
use dfault method for priors
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Oct 23, 2024
1 parent b3c8bd8 commit 76ef11a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Generated by roxygen2: do not edit by hand

S3method(plot,biokinetics_population_trajectories)
S3method(plot,biokinetics_priors)
export(add_exposure_data)
export(biokinetics)
export(biokinetics_priors)
export(convert_log2_scale_inverse)
export(plot_prior_predictive)
importFrom(R6,R6Class)
importFrom(data.table,":=")
importFrom(data.table,.BY)
Expand Down
8 changes: 4 additions & 4 deletions R/biokinetics.R
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,10 @@ biokinetics <- R6::R6Class(
#' @param n_draws Integer. The number of trajectories to simulate. Default 2000.
plot_prior_predictive = function(tmax = 150,
n_draws = 2000) {
plot_prior_predictive(private$priors,
tmax = tmax,
n_draws = n_draws,
data = private$data)
plot(private$priors,
tmax = tmax,
n_draws = n_draws,
data = private$data)
},
#' @description Plot model input data with a smoothing function. Note that
#' this plot is on a log scale, regardless of whether data was provided on a
Expand Down
25 changes: 13 additions & 12 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
#' @description Simulate trajectories by drawing random samples from the given
#' priors for each parameter in the biokinetics model.
#' @return A ggplot2 object.
#' @param priors A named list of type 'biokinetics_priors'.
#' @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_prior_predictive <- function(priors,
tmax = 150,
n_draws = 2000,
data = NULL) {
validate_priors(priors)
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
Expand All @@ -23,12 +24,12 @@ plot_prior_predictive <- function(priors,
validate_required_cols(data, c("time_since_last_exp", "value"))
}
params <- data.table(
t0 = stats::rnorm(n_draws, priors$mu_t0, priors$sigma_t0), # titre value at t0
tp = stats::rnorm(n_draws, priors$mu_tp, priors$sigma_tp), # time of peak
ts = stats::rnorm(n_draws, priors$mu_ts, priors$sigma_ts), # time of set point
m1 = stats::rnorm(n_draws, priors$mu_m1, priors$sigma_m1), # gradient 1
m2 = stats::rnorm(n_draws, priors$mu_m2, priors$sigma_m2), # gradient 2
m3 = stats::rnorm(n_draws, priors$mu_m3, priors$sigma_m3) # gradient 3
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)
Expand Down
10 changes: 6 additions & 4 deletions man/plot_prior_predictive.Rd → man/plot.biokinetics_priors.Rd

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

6 changes: 3 additions & 3 deletions tests/testthat/test-plots.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
test_that("Can plot prior prediction up to tmax", {
priors <- biokinetics_priors()
plot <- plot_prior_predictive(priors, tmax = 100, n_draws = 500)
plot <- plot(priors, tmax = 100, n_draws = 500)
expect_equal(nrow(plot$data), 100)
expect_equal(length(plot$layers), 2)
})

test_that("Can plot prior prediction with data points", {
data <- data.table::fread(system.file("delta_full.rds", package = "epikinetics"))
priors <- biokinetics_priors()
expect_error(plot_prior_predictive(priors, data = data), "Missing required columns: time_since_last_exp")
expect_error(plot(priors, data = data), "Missing required columns: time_since_last_exp")
data[, `:=`(time_since_last_exp = as.integer(day - last_exp_day, units = "days"))]
plot <- plot_prior_predictive(priors, data = data, n_draws = 500)
plot <- plot(priors, data = data, n_draws = 500)
expect_equal(length(plot$layers), 3)
})

Expand Down

0 comments on commit 76ef11a

Please sign in to comment.