Skip to content

Commit

Permalink
add individual trajectories plot
Browse files Browse the repository at this point in the history
  • Loading branch information
hillalex committed Nov 15, 2024
1 parent 1c2c50a commit 730a421
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 3 deletions.
2 changes: 1 addition & 1 deletion R/biokinetics.R
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ biokinetics <- R6::R6Class(
},
#' @description Simulate individual trajectories from the model. This is
#' computationally expensive and may take a while to run if n_draws is large.
#' @return A data.table. If summarise = TRUE columns are calendar_date, titre_type, me, lo, hi, time_shift.
#' @return A data.table. If summarise = TRUE columns are calendar_day, titre_type, me, lo, hi, time_shift.
#' If summarise = FALSE, columns are pid, draw, time_since_last_exp, mu, titre_type, exposure_day, calendar_day, time_shift
#' and a column for each covariate in the regression model. See the data vignette for details:
#' \code{vignette("data", package = "epikinetics")}.
Expand Down
59 changes: 57 additions & 2 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,61 @@ plot.biokinetics_population_trajectories <- function(x, ..., data = NULL) {
shape = guide_legend(title = "Censored"))
}

#' Plot method for "biokinetics_individual_trajectories" class
#'
#' @param x An object of class "biokinetics_individual_trajectories". These are
#' generated by running biokinetics$simulate_individual_trajectories(). See
#' \href{../../epikinetics/html/biokinetics.html#method-biokinetics-simulate_individaul_trajectories}{\code{biokinetics$simulate_individual_trajectories()}}
#' @param \dots Further arguments passed to the method.
#' @export
plot.biokinetics_individual_trajectories <- function(x, ..., data = NULL,
min_date = NULL,
max_date = NULL) {

# Declare variables to suppress notes when compiling package
# https://github.com/Rdatatable/data.table/issues/850#issuecomment-259466153
calendar_day <- value <- me <- mu <- titre_type <- lo <- hi <- day <- pid <- NULL
if (is.null(min_date)) {
min_date <- min(x$calendar_day)
}
if (is.null(max_date)) {
max_date <- max(x$calendar_day)
}
if (attr(x, "summarised")) {
plot <- ggplot(x) +
geom_line(aes(x = calendar_day, y = me, group = titre_type, colour = titre_type)) +
geom_ribbon(aes(x = calendar_day,
ymin = lo,
ymax = hi,
group = titre_type), alpha = 0.5)
} else {
plot <- ggplot(x) +
geom_line(aes(x = calendar_day, y = mu,
colour = titre_type, group = pid), alpha = 0.1, linewidth = 0.1)

Check warning on line 151 in R/plot.R

View check run for this annotation

Codecov / codecov/patch

R/plot.R#L149-L151

Added lines #L149 - L151 were not covered by tests
}
if (!is.null(data)) {
validate_required_cols(data, c("day", "value"))
plot <- plot +
geom_point(data = data,
aes(x = day,
y = value), size = 0.5, alpha = 0.5)

Check warning on line 158 in R/plot.R

View check run for this annotation

Codecov / codecov/patch

R/plot.R#L154-L158

Added lines #L154 - L158 were not covered by tests
}
plot +
labs(x = "Date",
y = expression(paste("Titre (IC"[50], ")"))) +
geom_smooth(
aes(x = calendar_day,
y = me,
fill = titre_type,
colour = titre_type,
group = titre_type),
alpha = 0.5, span = 0.2) +
scale_x_date(date_labels = "%b %Y",
limits = c(min_date, max_date)) +
guides(colour = guide_legend(title = "Titre type"),
fill = "none")
}

facet_formula <- function(covariates) {
paste("~", paste(c("titre_type", covariates), collapse = "+"))
}
Expand All @@ -126,8 +181,8 @@ 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]
data[, censored := FALSE]

Check warning on line 184 in R/plot.R

View check run for this annotation

Codecov / codecov/patch

R/plot.R#L184

Added line #L184 was not covered by tests
} else {
data[, censored:= censored != 0]
data[, censored := censored != 0]
}
}
Loading

0 comments on commit 730a421

Please sign in to comment.