diff --git a/NAMESPACE b/NAMESPACE index 577ae7a..ffe2550 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -5,6 +5,7 @@ S3method(plot,biokinetics_priors) export(add_exposure_data) export(biokinetics) export(biokinetics_priors) +export(convert_log2_scale) export(convert_log2_scale_inverse) importFrom(R6,R6Class) importFrom(data.table,":=") diff --git a/R/inspect-model.R b/R/inspect-model.R index 317a5f3..50b0c0b 100644 --- a/R/inspect-model.R +++ b/R/inspect-model.R @@ -135,7 +135,7 @@ inspect_model <- function(mod, private) { if (is.null(selected) || selected == "None") { selected <- character(0) } - plot_data(data(), ncol = cols(), covariates = selected) + + plot_sero_data(data(), ncol = cols(), covariates = selected) + theme(plot.margin = unit(c(1, 0, 0, 0), "cm")) }) diff --git a/R/shiny-utils.R b/R/shiny-utils.R index c221ffc..dddba9f 100644 --- a/R/shiny-utils.R +++ b/R/shiny-utils.R @@ -9,7 +9,7 @@ raw_numeric_input <- function(inputId, value, min = NA, max = NA, step = NA) { raw_text_input <- function(inputId, value = "", placeholder = NULL) { value <- shiny::restoreInput(id = inputId, default = value) - tags$input(id = inputId, type = "text", class = "shiny-input-text form-control", value = value, placeholder = placeholder) + shiny::tags$input(id = inputId, type = "text", class = "shiny-input-text form-control", value = value, placeholder = placeholder) } raw_select_input <- function(inputId, choices, selected = NULL, multiple = FALSE) { @@ -18,7 +18,7 @@ raw_select_input <- function(inputId, choices, selected = NULL, multiple = FALSE if (is.null(selected)) { if (!multiple) selected <- shiny:::firstChoice(choices) } else selected <- as.character(selected) - tags$select(id = inputId, class = "shiny-input-select", class = "form-control", shiny:::selectOptions(choices, selected, inputId)) + shiny::tags$select(id = inputId, class = "shiny-input-select", class = "form-control", shiny:::selectOptions(choices, selected, inputId)) } diff --git a/R/utils.R b/R/utils.R index 532f9c8..fe55f16 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,3 +1,11 @@ +#' @title Base 2 log scale conversion +#' +#' @description Natural scale data is converted to a base 2 log scale before model fitting using +#' this function. This function does not modify the provided data.table in-place, but returns a transformed copy. +#' @return A data.table, identical to the input data but with specified columns transformed. +#' @param dt_in data.table containing data to be transformed from natural to base 2 log scale. +#' @param vars_to_transform Names of columns to apply the transformation to. +#' @export convert_log2_scale <- function( dt_in, vars_to_transform = "value", simplify_limits = TRUE) { diff --git a/man/convert_log2_scale.Rd b/man/convert_log2_scale.Rd new file mode 100644 index 0000000..1812e87 --- /dev/null +++ b/man/convert_log2_scale.Rd @@ -0,0 +1,20 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/utils.R +\name{convert_log2_scale} +\alias{convert_log2_scale} +\title{Base 2 log scale conversion} +\usage{ +convert_log2_scale(dt_in, vars_to_transform = "value", simplify_limits = TRUE) +} +\arguments{ +\item{dt_in}{data.table containing data to be transformed from natural to base 2 log scale.} + +\item{vars_to_transform}{Names of columns to apply the transformation to.} +} +\value{ +A data.table, identical to the input data but with specified columns transformed. +} +\description{ +Natural scale data is converted to a base 2 log scale before model fitting using +this function. This function does not modify the provided data.table in-place, but returns a transformed copy. +} diff --git a/vignettes/diagnostics.Rmd b/vignettes/diagnostics.Rmd new file mode 100644 index 0000000..d4e1a7b --- /dev/null +++ b/vignettes/diagnostics.Rmd @@ -0,0 +1,64 @@ +--- +title: "Model diagnostics and plotting" +output: rmarkdown::html_vignette +vignette: > + %\VignetteIndexEntry{Model diagnostics and plotting} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +--- + +```{r, include = FALSE} +knitr::opts_chunk$set( + collapse = TRUE, + comment = "#>" +) +``` + +## Plotting model inputs + +### Priors +Calling `plot` on an object of type [biokinetics_priors](../reference/biokinetics_priors.html) will generate +a plot of the kinetics predicted by the given priors. + +```{r} +priors <- epikinetics::biokinetics_priors() +plot(priors) +``` + +You can optionally pass a dataset to compare against the predicted kinetics. Required columns are +`time_since_last_exp` and `value` and values should be on the scale required by the model. + +```{r} +data <- data.table::fread(system.file("delta_full.rds", package = "epikinetics")) +data[, time_since_last_exp := as.integer(day - last_exp_day, units = "days")] +data <- epikinetics::convert_log2_scale(data) +priors <- epikinetics::biokinetics_priors() +plot(priors, data = data) +``` + +If you have an instance of the [biokinetics](../reference/biokinetics.html) class, the method `plot_prior_predictive` +generates this plot for the priors and data given to the model. + +```{r} +data <- data.table::fread(system.file("delta_full.rds", package = "epikinetics")) +priors <- epikinetics::biokinetics_priors() +mod <- epikinetics::biokinetics$new(priors = priors, data = data) +mod$plot_prior_predictive() +``` + +### Data + +If you have an instance of the [biokinetics](../reference/biokinetics.html) class, the method `plot_model_inputs` +plots the input data used to fit the model, disaggregated by the covariates in the covariate formula. + +```{r} +mod <- epikinetics::biokinetics$new(priors = priors, data = data, covariate_formula = ~0 + infection_history) +mod$plot_model_inputs() +``` + +## Interactive data exploration + +To play around with different priors and visualise input data filtered and disaggregated in different ways, +the function [biokinetics$inspect](reference/biokinetics.html#method-biokinetics-inspect) runs a local RShiny app with interactive plots. + +![RShiny demonstration](./shiny.webm) diff --git a/vignettes/shiny.webm b/vignettes/shiny.webm new file mode 100644 index 0000000..75e2122 Binary files /dev/null and b/vignettes/shiny.webm differ