fit: Fit force of infection models to serological data

View source: R/fit.R

fitR Documentation

Fit force of infection models to serological data

Description

Runs a Bayesian MCMC using rstan to draw samples of a model of the class FOImodel, with data of the class SeroData. It is an adaptation of the function sampling in the rstan package.

Usage

fit(
  model,
  data = data,
  iter = 5000,
  chains = 4,
  warmup = floor(iter/2),
  thin = 1,
  seed = sample.int(.Machine$integer.max, 1),
  init = "random",
  check_data = TRUE,
  sample_file = NULL,
  diagnostic_file = NULL,
  verbose = FALSE,
  algorithm = c("NUTS", "HMC", "Fixed_param"),
  control = NULL,
  include = TRUE,
  cores = getOption("mc.cores", 1L),
  open_progress = interactive() && !isatty(stdout()) && !identical(Sys.getenv("RSTUDIO"),
    "1"),
  show_messages = TRUE,
  ...
)

## S3 method for class 'FOIfit'
print(x, digits_summary = 2, ...)

## S3 method for class 'FOIfit'
summary(x, ...)

Arguments

model

A FOImodel object, generated by the function FOImodel.

data

A SeroData object, generated by the function SeroData.

iter

integer. Number of iterations for each chain, including warmup. Default = 5000.

chains

integer. Number of independent Markov chains. Default = 4.

warmup

A positive integer specifying the number of warmup (aka burnin) iterations per chain. If step-size adaptation is on (which it is by default), this also controls the number of iterations for which adaptation is run (and hence these warmup samples should not be used for inference). The number of warmup iterations should not be larger than iter and the default is iter/2.

thin

A positive integer specifying the period for saving samples. The default is 1, which is usually the recommended value.

seed

The seed for random number generation. The default is generated from 1 to the maximum integer supported by R on the machine. Even if multiple chains are used, only one seed is needed, with other chains having seeds derived from that of the first chain to avoid dependent samples. When a seed is specified by a number, as.integer will be applied to it. If as.integer produces NA, the seed is generated randomly. The seed can also be specified as a character string of digits, such as "12345", which is converted to integer.

init

Initial values specification. See the detailed documentation for the init argument in stan.

check_data

Logical, defaulting to TRUE. If TRUE the data will be preprocessed; otherwise not. See the Note section in stan.

sample_file

An optional character string providing the name of a file. If specified the draws for all parameters and other saved quantities will be written to the file. If not provided, files are not created. When the folder specified is not writable, tempdir() is used. When there are multiple chains, an underscore and chain number are appended to the file name prior to the .csv extension.

diagnostic_file

An optional character string providing the name of a file. If specified the diagnostics data for all parameters will be written to the file. If not provided, files are not created. When the folder specified is not writable, tempdir() is used. When there are multiple chains, an underscore and chain number are appended to the file name prior to the .csv extension.

verbose

TRUE or FALSE: flag indicating whether to print intermediate output from Stan on the console, which might be helpful for model debugging.

algorithm

One of sampling algorithms that are implemented in Stan. Current options are "NUTS" (No-U-Turn sampler, Hoffman and Gelman 2011, Betancourt 2017), "HMC" (static HMC), or "Fixed_param". The default and preferred algorithm is "NUTS".

control

A named list of parameters to control the sampler's behavior. See the details in the documentation for the control argument in stan.

include

Logical scalar defaulting to TRUE indicating whether to include or exclude the parameters given by the pars argument. If FALSE, only entire multidimensional parameters can be excluded, rather than particular elements of them.

cores

Number of cores to use when executing the chains in parallel, which defaults to 1 but we recommend setting the mc.cores option to be as many processors as the hardware and RAM allow (up to the number of chains).

open_progress

Logical scalar that only takes effect if cores > 1 but is recommended to be TRUE in interactive use so that the progress of the chains will be redirected to a file that is automatically opened for inspection. For very short runs, the user might prefer FALSE.

show_messages

Either a logical scalar (defaulting to TRUE) indicating whether to print the summary of Informational Messages to the screen after a chain is finished or a character string naming a path where the summary is stored. Setting to FALSE is not recommended unless you are very sure that the model is correct up to numerical error.

Value

A list with the class FOIfit, which contains the following items:

  • fit: The results of the fit, of class stanfit.

  • data: The input data.

  • model: The input model.

Author(s)

Nathanael Hoze nathanael.hoze@gmail.com

See Also

SeroData Define the format of the serological data.

FOImodel Define a model.

Examples


data <- simulate_SeroData(number_samples = 1000,
  age_class = 1,
  epidemic_years = c(1976,1992),
  foi = c(0.2,0.3))
model <- FOImodel('outbreak', K = 2)
F1 <- fit(model = model, data = data)
seroprevalence.fit(F1)
plot(F1)


nathoze/Rsero documentation built on Feb. 3, 2024, 9:58 p.m.