Learn R Programming

greta (version 0.1.7)

greta-inference: Statistical Inference on Greta Models

Description

Define a greta_model object and carry out statistical inference on parameters of interest by MCMC

Usage

define_model(...)

mcmc(model, method = c("hmc"), n_samples = 1000, thin = 1, warmup = 100, verbose = TRUE, control = list(), initial_values = NULL)

Arguments

greta_array objects to be tracked by the model (i.e. those for which samples will be retained during mcmc). If not provided, all of the non-data greta_array objects defined in the calling environment will be tracked.
model
greta_model object
method
the method used to sample values. Currently only hmc is implemented
n_samples
the number of samples to draw (after any warm-up, but before thinning)
thin
the thinning rate; every thin samples is retained, the rest are discarded
warmup
the number of samples to spend warming up the sampler. During this phase the sampler moves toward the highest density area and may tune sampler hyperparameters.
verbose
whether to print progress information to the console
control
an optional named list of hyperparameters and options to control behaviour of the sampler. See Details.
initial_values
an optional named vector of initial values for the free parameters in the model

Value

define_model - a greta_model object. See greta-model for details.

mcmc - an mcmc.list object that can be analysed using functions from the coda package. This will on contain mcmc samples of the parameters of interest, as defined in model.

Details

Currently, the only implemented mcmc procedure is 'vanilla' Hamiltonian Monte Carlo (method = 'hmc'). Unlike variants such as NUTS (which Stan uses) HMC does require some manual tuning of sampler hyperparameters. The main thing to consider is the leapfrog stepsize hyperparameter epsilon. If you're getting a lot of rejected proposals, you might want to reduce epsilon, or if the sampler is moving too slowly you might want to increase epsilon.

epsilon can be controlled via the control argument, along with two other hyprparameters: Lmin and Lmax; positive integers (with Lmax > Lmin) giving the upper and lower limits to the number of leapfrog steps per iteration (from which the number is selected uniformly at random)

The default control options for HMC are: control = list(Lmin = 10, Lmax = 20, epsilon = 0.005)

Examples

Run this code

## Not run: ------------------------------------
# 
# # define a simple model
# mu = free()
# sigma = lognormal(1, 0.1)
# x = rnorm(10)
# distribution(x) = normal(mu, sigma)
# 
# m <- define_model(mu, sigma)
# 
## ---------------------------------------------
## Not run: ------------------------------------
# # carry out mcmc on the model
# draws <- mcmc(m,
#               n_samples = 100,
#               warmup = 10)
## ---------------------------------------------

Run the code above in your browser using DataLab