Skip to contents

This class represents a Bayesian borrowing model using MCMC sampling. It inherits from the Model class.

Super class

Model -> MCMCModel

Public fields

stan_model_code

Code of the Stan model

stan_model

The compiled Stan model

stan_model_name

Name the Stan model is compiled under, for models that compile it only when they first sample

summary_variables

Variables to summarise from the posterior draws

fit

The MCMC fit object

fit_summary

Summary of the Stan fit

treatment_effect_summary

Summary statistics of the treatment effect posterior distribution

credible_interval_97.5

The upper bound of the credible interval

credible_interval_2.5

The lower bound of the credible interval

mcmc_config

The MCMC configuration parameters

mcmc_ess

MCMC ESS

n_divergences

Number of divergences in MCMC inference

rhat

r-hat statistics

draws_dir

Directory where to store MCMC draws (used by Stan)

prior_draws

Draws from the prior

prior_pdf_approx

Approximation to the prior probability density function

prior_cdf_approx

Approximation to the prior cumulative density function

posterior_pdf_approx

Approximation to the posterior probability density function

posterior_cdf_approx

Approximation to the posterior cumulative density function

Methods

Inherited methods


MCMCModel$new()

Initialize the MCMCModel object

A subclass that can also compute its posterior by quadrature declares quadrature_available = TRUE and implements quadrature_posterior(). For such a class, mcmc_config$engine chooses between the two: "quadrature", the default, or "stan". Every other subclass samples with Stan whatever the setting.

Usage

MCMCModel$new(prior, mcmc_config)

Arguments

prior

The prior object

mcmc_config

The MCMC configuration parameters


MCMCModel$stan_sampler()

The compiled Stan model, compiled on first use

A model that names its Stan program in stan_model_name rather than compiling it in initialize() is compiled here, the first time it samples. Models whose simulations never sample, because their replicates go through a quadrature path, therefore never compile at all.

Usage

MCMCModel$stan_sampler()

Returns

The compiled Stan model.


MCMCModel$uses_quadrature()

Whether the posterior is computed by quadrature

Usage

MCMCModel$uses_quadrature()

Returns

TRUE under the quadrature engine, FALSE when sampling.


MCMCModel$quadrature_posterior()

The posterior as a grid, under the quadrature engine. Subclasses with quadrature_available = TRUE must implement it.

Usage

MCMCModel$quadrature_posterior(target_data)

Arguments

target_data

The target data for inference

Returns

A grid_posterior() list.


MCMCModel$quadrature_prior()

The prior as a grid, under the quadrature engine. Only needed by subclasses that sample their prior with Stan otherwise.

Usage

MCMCModel$quadrature_prior()

Returns

A grid_posterior() list.


MCMCModel$check_mcmc_config()

Check validity of the MCMC configuration

Usage

MCMCModel$check_mcmc_config(mcmc_config)

Arguments

mcmc_config

MCMC configuration


MCMCModel$summary_rows()

Rows of the model summary, with the MCMC diagnostics of the last fit

The diagnostics are left out when the posterior is computed by quadrature.

Usage

MCMCModel$summary_rows()

Returns

A data frame with columns Attribute and Value.


MCMCModel$prepare_data()

Prepare the data for inference. Subclasses must implement the 'prepare_data' method.

Usage

MCMCModel$prepare_data(target_data)

Arguments

target_data

The target data for inference


MCMCModel$inference()

Perform inference, by quadrature or by MCMC sampling

Usage

MCMCModel$inference(target_data)

Arguments

target_data

The target data for inference


MCMCModel$credible_interval()

Calculate the credible interval

Usage

MCMCModel$credible_interval(level = 0.95)

Arguments

level

The confidence level for the credible interval (default is 0.95)

Returns

The credible interval as a numeric vector


MCMCModel$posterior_ess()

Effective sample sizes of the current posterior

The single summary pass over the draws already produced the posterior standard deviation and the credible interval bounds, so both effective sample sizes read straight off it. The inherited route would resample the draws and fit a mixture to the resample, which costs a mixture fit per replicate and adds a second layer of Monte Carlo error on top of the one the sampler already carries.

Usage

MCMCModel$posterior_ess(target_data, ...)

Arguments

target_data

Target study data

...

Unused, kept so that the simulation can call every model the same way.

Returns

A list with the moment and precision effective sample sizes.


MCMCModel$posterior_median()

Get the posterior median

Usage

MCMCModel$posterior_median(...)

Arguments

...

Optional argument

Returns

The posterior median as a numeric value


MCMCModel$sample_posterior()

Sample from the posterior distribution

Usage

MCMCModel$sample_posterior(n_samples)

Arguments

n_samples

The number of samples to draw from the posterior distribution

Returns

The sampled treatment effect values as a numeric vector


MCMCModel$compute_posterior_parameters()

Compute the posterior parameters. If there are posterior borrowing parameters, the following method must be overriden in the subclass.

Usage

MCMCModel$compute_posterior_parameters()


MCMCModel$draw_mcmc_prior()

Draw samples from the prior using MCMC

Usage

MCMCModel$draw_mcmc_prior()


MCMCModel$posterior_pdf()

Posterior PDF

Usage

MCMCModel$posterior_pdf(target_treatment_effect)

Arguments

target_treatment_effect

Point at which to evaluate the posterior PDF


MCMCModel$posterior_cdf()

Calculates the posterior cumulative distribution function (CDF) for a given target treatment effect.

Usage

MCMCModel$posterior_cdf(target_treatment_effect)

Arguments

target_treatment_effect

The target treatment effect.

Returns

The posterior CDF.


MCMCModel$prior_pdf()

Prior PDF

Usage

MCMCModel$prior_pdf(
  target_treatment_effect,
  n_samples_quantile_estimation = 10000
)

Arguments

target_treatment_effect

Point at which to evaluate the prior PDF

n_samples_quantile_estimation

Number of samples used to estimate the quantiles of the distribution


MCMCModel$prior_cdf()

Prior CDF

Usage

MCMCModel$prior_cdf(
  target_treatment_effect,
  n_samples_quantile_estimation = 10000
)

Arguments

target_treatment_effect

Point at which to evaluate the prior CDF

n_samples_quantile_estimation

Number of samples used to estimate the quantiles of the distribution


MCMCModel$sample_prior()

Sample from the prior distribution

Usage

MCMCModel$sample_prior(n_samples)

Arguments

n_samples

Number of samples to draw

Returns

A vector of samples


MCMCModel$clone()

The objects of this class are cloneable with this method.

Usage

MCMCModel$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.