Title: | Infectious Disease Modelling Using brms |
---|---|
Description: | Infectious disease modelling using brms. |
Authors: | Sam Abbott [aut, cre] , Sebastian Funk [aut] |
Maintainer: | Sam Abbott <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.0.9000 |
Built: | 2024-11-23 04:18:54 UTC |
Source: | https://github.com/epiforecasts/idbrms |
This function exposes internal stan functions in R from a user supplied list of target files. Allows for testing of stan functions in R and potentially user use in R code.
expose_idbrms_stan_fns(files, dir, ...)
expose_idbrms_stan_fns(files, dir, ...)
files |
A character vector indicating the target files |
dir |
A character string indicating the directory for the file |
... |
Additional arguments passed to |
Define a formula for the convolution model
Define a model specific formula
## S3 method for class 'idbrms_convolution' id_formula(data, scale = ~1, cmean = ~1, lcsd = ~1, ...) id_formula(data, ...)
## S3 method for class 'idbrms_convolution' id_formula(data, scale = ~1, cmean = ~1, lcsd = ~1, ...) id_formula(data, ...)
data |
A data.frame as produced by |
scale |
Formula for the scaling of primary observations to secondary observations. |
cmean |
Formula for the convolution mean. Defaults to intercept only. |
lcsd |
Formula for the logged convolution standard deviation. Defaults to intercept only. |
... |
Additional arguments for method. |
Sam Abbott
Sam Abbott
Define model specific priors
id_priors(data, ...)
id_priors(data, ...)
data |
A data frame as prepared for modelling using |
... |
Additional arguments for method. |
Sam Abbott
Define priors for the delay convolution model
## S3 method for class 'idbrms_convolution' id_priors( data, scale = c(round(log(0.1), 2), 1), cmean = c(2, 1), lcsd = c(-0.5, 0.25), ... )
## S3 method for class 'idbrms_convolution' id_priors( data, scale = c(round(log(0.1), 2), 1), cmean = c(2, 1), lcsd = c(-0.5, 0.25), ... )
data |
A data.frame as produced by |
scale |
Vector of length two defining the mean and the standard deviation of the normal prior for the scaling factor. |
cmean |
Vector of length two defining the mean and standard deviation of the log mean of the delay distribution. |
lcsd |
Vector of length two defining the mean and standard deviation of the log standard deviation logged. the standard deviation to be greater than 0 on the unconstrained scale. |
... |
Additional arguments for method. |
Sam Abbott
Define model specific stancode
id_stancode(data, ...)
id_stancode(data, ...)
data |
A data frame as prepared for modelling using |
... |
Additional arguments for method. |
Sam Abbott
Define stan code for a delay convolution model
## S3 method for class 'idbrms_convolution' id_stancode(data, ...)
## S3 method for class 'idbrms_convolution' id_stancode(data, ...)
data |
A data.frame as produced by |
... |
Additional arguments for method. |
Sam Abbott
Interface for infectious disease modelling using brms.
idbrm(data, formula, family, priors, custom_stancode, dry = FALSE, ...)
idbrm(data, formula, family, priors, custom_stancode, dry = FALSE, ...)
data |
A data frame as prepared for modelling using |
formula |
A formula as defined using |
family |
A observation model family as defined in |
priors |
A list of priors as defined using |
custom_stancode |
A list of |
dry |
Logical, defaults to TRUE. For testing purposes should just the
|
... |
Additional arguments to pass to |
Sam Abbott
# define some example data library(data.table) dt <- data.table( region = "France", cases = seq(10, 500, by = 10), date = seq(as.Date("2020-10-01"), by = "days", length.out = 50) ) dt[, deaths := as.integer(shift(cases, 5) * 0.1)] dt[is.na(deaths), deaths := 0] dt <- prepare( dt, model = "convolution", location = "region", primary = "cases", secondary = "deaths", ) # fit the convolution model using a Poisson observation model fit <- idbrm(data = dt, family = poisson(link = "identity"))
# define some example data library(data.table) dt <- data.table( region = "France", cases = seq(10, 500, by = 10), date = seq(as.Date("2020-10-01"), by = "days", length.out = 50) ) dt[, deaths := as.integer(shift(cases, 5) * 0.1)] dt[is.na(deaths), deaths := 0] dt <- prepare( dt, model = "convolution", location = "region", primary = "cases", secondary = "deaths", ) # fit the convolution model using a Poisson observation model fit <- idbrm(data = dt, family = poisson(link = "identity"))
A model that assumes that a secondary observations can be predicted using a convolution of a primary observation multipled by some scaling factor. An example use case of this model is to estimate the case fatality rate (with the primary observation being cases and the secondary observation being deaths) and then explore factors that influence it.
## S3 method for class 'idbrms_convolution' idbrm( data, formula = id_formula(data), family = negbinomial(link = "identity"), priors = id_priors(data), custom_stancode = id_stancode(data), dry = FALSE, ... )
## S3 method for class 'idbrms_convolution' idbrm( data, formula = id_formula(data), family = negbinomial(link = "identity"), priors = id_priors(data), custom_stancode = id_stancode(data), dry = FALSE, ... )
data |
A data.frame as produced by |
formula |
A formula as defined using |
family |
A observation model family as defined in |
priors |
A list of priors as defined using |
custom_stancode |
A list of |
dry |
Logical, defaults to TRUE. For testing purposes should just the
|
... |
Additional parameters passed to |
A "brmsfit" object or stan code (if dry = TRUE
).
Sam Abbott
# define some example data library(data.table) dt <- data.table( region = "France", cases = seq(10, 500, by = 10), date = seq(as.Date("2020-10-01"), by = "days", length.out = 50) ) dt[, deaths := as.integer(shift(cases, 5) * 0.1)] dt[is.na(deaths), deaths := 0] dt <- prepare( dt, model = "convolution", location = "region", primary = "cases", secondary = "deaths", ) # fit the convolution model using a Poisson observation model fit <- idbrm(data = dt, family = poisson(link = "identity"))
# define some example data library(data.table) dt <- data.table( region = "France", cases = seq(10, 500, by = 10), date = seq(as.Date("2020-10-01"), by = "days", length.out = 50) ) dt[, deaths := as.integer(shift(cases, 5) * 0.1)] dt[is.na(deaths), deaths := 0] dt <- prepare( dt, model = "convolution", location = "region", primary = "cases", secondary = "deaths", ) # fit the convolution model using a Poisson observation model fit <- idbrm(data = dt, family = poisson(link = "identity"))
Infectious disease modelling wrapper for brm
idbrmfit(formula, data, family, priors, custom_stancode, dry = FALSE, ...)
idbrmfit(formula, data, family, priors, custom_stancode, dry = FALSE, ...)
formula |
A |
data |
A data frame. |
family |
An observation model family as defined in |
priors |
A list of priors as defined using |
custom_stancode |
A list of |
dry |
Logical, defaults to TRUE. For testing purposes should just the
|
... |
Additional arguments to pass to |
Sam Abbott
Read in a idbrms Stan code chunk
idbrms_stan_chunk(path)
idbrms_stan_chunk(path)
path |
The path within the "stan" folder of the installed idbrms package to the stan code chunk of interest. |
A character string containing the stan code chunk of interest.
idbrms_stan_chunk("functions/idbrms_convolve.stan")
idbrms_stan_chunk("functions/idbrms_convolve.stan")
Label a idbrms stan model with a version indicator
idbrms_version_stanvar()
idbrms_version_stanvar()
A brms stanvar chunk containing the package version used to build the stan code.
Prepare data for modelling with idbrm
Default method used when preparing data
prepare(data, ...) ## Default S3 method: prepare(data, model, ...)
prepare(data, ...) ## Default S3 method: prepare(data, model, ...)
data |
A dataframe to be used for modelling |
... |
Additional arguments passed to model specific prepare functions |
model |
Character string, model type to prepare to use. Supported options are "convolution". |
Sam Abbott
Sam Abbott
# define some example data library(data.table) dt <- data.table( region = "France", cases = seq(10, 500, by = 10), date = seq(as.Date("2020-10-01"), by = "days", length.out = 50) ) dt <- dt[, deaths := as.integer(shift(cases, 5) * 0.1)] dt <- dt[is.na(deaths), deaths := 0] dt <- prepare( dt, model = "convolution", location = "region", primary = "cases", secondary = "deaths", ) dt[]
# define some example data library(data.table) dt <- data.table( region = "France", cases = seq(10, 500, by = 10), date = seq(as.Date("2020-10-01"), by = "days", length.out = 50) ) dt <- dt[, deaths := as.integer(shift(cases, 5) * 0.1)] dt <- dt[is.na(deaths), deaths := 0] dt <- prepare( dt, model = "convolution", location = "region", primary = "cases", secondary = "deaths", ) dt[]
Prepare data for fitting using a convolution model
## S3 method for class 'idbrms_convolution' prepare( data, location, primary, secondary, initial_obs = 14, max_convolution = 30, ... )
## S3 method for class 'idbrms_convolution' prepare( data, location, primary, secondary, initial_obs = 14, max_convolution = 30, ... )
data |
A data frame containing at least two integer observations and a date variable. |
location |
Character string, variable to use as the spatial location. |
primary |
Character string, variable to use as the primary observation. |
secondary |
Character string, variable to use as the secondary observation. |
initial_obs |
Integer, number of observations to hold out from the likelihood. This is useful as initially the outcome will depend on primary data outside of the range of the training set and including this could bias the estimated delay distribution. Defaults to 14 days. |
max_convolution |
Integer defining the maximum index to use for the convolution. Defaults to 30 days. |
... |
Additional arguments passed to model specific prepare functions |
Sam Abbott
# define some example data library(data.table) dt <- data.table( region = "France", cases = seq(10, 500, by = 10), date = seq(as.Date("2020-10-01"), by = "days", length.out = 50) ) dt <- dt[, deaths := as.integer(shift(cases, 5) * 0.1)] dt <- dt[is.na(deaths), deaths := 0] dt <- prepare( dt, model = "convolution", location = "region", primary = "cases", secondary = "deaths", ) dt[]
# define some example data library(data.table) dt <- data.table( region = "France", cases = seq(10, 500, by = 10), date = seq(as.Date("2020-10-01"), by = "days", length.out = 50) ) dt <- dt[, deaths := as.integer(shift(cases, 5) * 0.1)] dt <- dt[is.na(deaths), deaths := 0] dt <- prepare( dt, model = "convolution", location = "region", primary = "cases", secondary = "deaths", ) dt[]