NOT_CRAN <- identical(tolower(Sys.getenv("NOT_CRAN")), "true") # nolint knitr::opts_chunk$set( collapse = TRUE, comment = "#>", eval = NOT_CRAN )
rbi.helpers is collection of helper functions to use with rbi, an R interface to LibBi, a library for Bayesian Inference.
This vignette builds on the rbi vignette, applying the higher-level functions contained in rbi.helpers to the same model introduced there. For the lower-level functions to run LibBi and read the results, please refer to the documentation and vignette that comes with rbi.
The easiest way to install the latest stable version of rbi.helpers is via CRAN. The package name is rbi.helpers
(all lowercase):
install.packages("rbi.helpers")
Alternatively, the current development version can be installed using the remotes
package
remotes::install_github("sbfnk/rbi.helpers")
Most functions in the rbi.helpers package require a working installation of LibBi. Please see the rbi vignette for how to get one via homebrew or linuxbrew.
Use
library("rbi") library("rbi.helpers")
suppressPackageStartupMessages(library("rbi")) suppressPackageStartupMessages(library("rbi.helpers"))
to load the package.
These steps are reproduced from the rbi vignette, where there is more information on the individual steps
model_file <- system.file(package = "rbi", "SIR.bi") # file included in package sir_model <- bi_model(model_file) # load model set.seed(1001912) sir_data <- bi_generate_dataset(sir_model, end_time = 16 * 7, noutputs = 16)
In the rbi vignette, a stochastic SIR model is fitted to simulated data from the same model using particle Markov-chain Monte Carlo with 32 particles.
Given a model and data, how do we know how many particles we need?
This question does not have a simple answer, as the "optimal" number of particles may depend on the state of the Markov chain.
A possible rule-of-thumb is to choose the number of particles such that the variance of the log-likelihood near the mode is approximately one.
This suggests a strategy by which first and approximate location of the mode or mean of the posterior distribution is obtained in a trial run, before the numer of particles is adjusted by monitoring the variance of the log-likelihood while keeping the parameters fixed.
rbi.helpers implements the second part of this strategy (adjusting the number of particles at a given location in parameter space) with the adapt_particles
method.
For the first part (finding the mode), a crude method is to take a fixed number of samples from the prior distribution and choose the one that maximises the posterior.
In rbi, this can be achieved with
bi_prior <- sample( proposal = "prior", sir_model, nsamples = 1000, end_time = 16 * 7, nparticles = 4, obs = sir_data, seed = 1234 )
This runs particle MCMC with the prior distribution as proposal distribution (because proposal = "prior"
), in this case with 4 particles.
In other words, when sampling from the posterior the proposals will be drawn independently from the prior distribution.
Note that we set a seed to make the results reproducible.
It is worth trying the commands with a different seed and seeing the difference to the results obtained below.
The location in parameters of the sampler at the end of the 1000 iterations will give an approximation of the mode of the posterior distribution.
This can then be used to adjust the number of particles using
adapted <- adapt_particles(bi_prior)
This will take the last sample of the output file contained in the libbi
object bi_prior
, and use it to adjust the number of particles by starting with 1 particle (or a given min
) and doubling it until the variance of the loglikelihood crosses 1.
The number of particles is then saved in the adapted
object:
adapted$options$nparticles
Having adjusted the number of particles, the second important information to give the posterior sampler is the proposal distribution.
This can, again, be obtained using a sequence of trial runs, whereby the proposal distribution is sequentially adjusted from previous samples to be proportional to the empirical covariance of the posterior samples.
The way this is implemented in the adapt_proposal
function in rbi.helpers is that the proposal distribution is adjusted to come from a multivariate normal taking into account the covariance of samples obtained so far, until the acceptance rate lies between the given minimum and maximumad.
For example, to adjust the proposal distribution for an acceptance rate between 0.05 and 0.4, we can run:
adapted <- adapt_proposal(adapted, min = 0.05, max = 0.4)
The covariance matrices for parameters and initial conditions are stored in the input file contained in the libbi
object adapted
bi_read(adapted, file = "input")
To compute the Deviance Information Criterion (DIC), use DIC
:
posterior <- sample(adapted) DIC(posterior)
This samples from the posterior distribution using the adapted number of particles and proposal distribution and then calculates the DIC.
LibBi uses real-valued times. To convert these to time or date objects for use in R, use the numeric_to_time
function:
res <- numeric_to_time(posterior, unit = "day", origin = as.Date("2018-04-01")) head(res$Z)
The function time_to_numeric
does the converse, converting R times or dates into numeric values for use by LibBi:
orig <- time_to_numeric(res, unit = "day", origin = as.Date("2018-04-01")) head(orig$Z)
With the pipe operator available since R version 4.1, it is possible to construct inference chains more elegantly. For example, to get posterior samples from adapted Metropolis-Hastings including sampled observations, we could have written
posterior <- sample( proposal = "prior", sir_model, nsamples = 1000, end_time = 16 * 7, nparticles = 4, obs = sir_data, seed = 1234 ) |> adapt_particles() |> adapt_proposal(min = 0.05, max = 0.4) |> sample(nsamples = 5000) |> sample_obs()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.