Title: | tidy mcmc.list using data.table |
---|---|
Description: | Tidy up, diagnose, and visualise your mcmc samples quickly and easily so you can get on with your analysis. |
Authors: | Nicholas Tierney [aut, cre] , Sam Clifford [aut] |
Maintainer: | Nicholas Tierney <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.0.9.9000 |
Built: | 2024-10-31 20:18:50 UTC |
Source: | https://github.com/njtierney/mmcc |
Diagnostic: Calculate the autocorrelation for each chain and parameter
diag_autocorr(x, lags = NULL)
diag_autocorr(x, lags = NULL)
x |
mcmc.list object or dataframe creatd by |
lags |
integer the lag value that you want to use |
a data.table data frame with the autocorrelation at each lag for each chain and parameter columns lag
, acf
, chain
, and parameter
library(coda) data(line) line_acf <- diag_autocorr(line) line_acf
library(coda) data(line) line_acf <- diag_autocorr(line) line_acf
Diagnostic: Calculate the effective sample size for each chain and parameter
diag_ess(x)
diag_ess(x)
x |
mcmc.list object or dataframe creatd by |
a data.table data frame with the effective sample size for each chain and parameter
library(coda) data(line) line_ess <- diag_ess(line) line_ess
library(coda) data(line) line_ess <- diag_ess(line) line_ess
Calculate Monte Carlo Standard Errors Using Batch Means
diag_mc_stderr(x, b_size = "sqroot", warn = TRUE)
diag_mc_stderr(x, b_size = "sqroot", warn = TRUE)
x |
'mcmc.list' or 'data.table' object |
b_size |
character, method for determining size of batch (see Details) |
warn |
logical, give a warning of there are too few samples in the MCMC output |
For the batch size, the options are 'sqroot', for using the using the square root of the MCMC output length, or 'cuberoot', for using the cube root of the output length. The default is 'sqroot'.
Galin L. Jones, Murali Haran, Brian S. Caffo, and Ronald Neath (2006). "Fixed-Width Output Analysis for Markov Chain Monte Carlo," Journal of the American Statistical Association, 101, 1537–1547
library(coda) data(line) r <- diag_mc_stderr(line) r
library(coda) data(line) r <- diag_mc_stderr(line) r
This is an example JAGS model to use for examples in the mmcc
package. The
model is fit as a basic linear regression, with uniform priors on beta0
and beta1, where y is assumed to be normal, with mean mu and precision
tau. The model is drawn from the vignette, "Model summaries for a
Bayesian linear regression", and the code to generate it can be found in
the data-raw
folder.
data(example_jags_model)
data(example_jags_model)
An object of class jags
of length 8.
library(rjags) example_jags_model$recompile() model_dic <- dic.samples(example_jags_model, n.iter = 1000) glance(model_dic)
library(rjags) example_jags_model$recompile() model_dic <- dic.samples(example_jags_model, n.iter = 1000) glance(model_dic)
This is an example STAN model to use for examples in the mmcc
package. It
was created with the example code given below in examples.
data(example_stan_model)
data(example_stan_model)
An object of class stanfit
of dimension 5 x 4 x 3.
mcmc_to_dt(example_stan_model)
mcmc_to_dt(example_stan_model)
This provides a one-row dataframe (data.table
) with information on the
overall deviance, effective number of parameters (when type
was pD) or
optimism (when type
was popt), and resulting penalised deviance.
## S3 method for class 'dic' glance(x, ...)
## S3 method for class 'dic' glance(x, ...)
x |
a dic object generated by rjags::dic.samples |
... |
(optional) additional arguments to pass |
a one-row dataframe of summary information of the dic samples
Sam Clifford, [email protected]
library(rjags) example_jags_model$recompile() model_dic <- dic.samples(example_jags_model, n.iter = 1000) glance(model_dic)
library(rjags) example_jags_model$recompile() model_dic <- dic.samples(example_jags_model, n.iter = 1000) glance(model_dic)
This provides a one-row dataframe with information on number of chains, the number of variables, the number of iterations, and the lower and upper values for effective sample size (ess), and rhat.
## S3 method for class 'mcmc.list' glance(x, ...)
## S3 method for class 'mcmc.list' glance(x, ...)
x |
an mcmc.list object |
... |
(optional) additional arguments to pass |
a one-row dataframe of summary information of the mcmc model
library(coda) data(line) glance(line)
library(coda) data(line) glance(line)
mcmc.list
to a tidy data.table objectmcmc_to_dt
use data.table to return a tidy dataframe from an "mcmc.list",
or "stan" object.
mcmc_to_dt(mcmc_object, ...)
mcmc_to_dt(mcmc_object, ...)
mcmc_object |
an object of class "mcmc.list", as you would find with
fitting a model using |
... |
additional arguments |
a tidy data.table dataframe of MCMC sample
mcmc.list
to a tidy data.table objectmcmc_to_dt
use data.table to return a tidy dataframe from an "mcmc.list",
or "stan" object.
## S3 method for class 'mcmc.list' mcmc_to_dt(mcmc_object, ..., colnames = NULL)
## S3 method for class 'mcmc.list' mcmc_to_dt(mcmc_object, ..., colnames = NULL)
mcmc_object |
an object of class "mcmc.list", as you would find with
fitting a model using |
... |
additional arguments |
colnames |
which parameters we want from |
library(coda) data(line) mcmc_to_dt(line)
library(coda) data(line) mcmc_to_dt(line)
MCMC tidiers (draft) for STAN
## S3 method for class 'stanfit' mcmc_to_dt(mcmc_object, ...)
## S3 method for class 'stanfit' mcmc_to_dt(mcmc_object, ...)
mcmc_object |
an object of class "mcmc.list", as you would find with
fitting a model using |
... |
additional arguments |
mcmc_to_dt(example_stan_model)
mcmc_to_dt(example_stan_model)
Retrieve dimensions of an MCMC object:
n_chain(x)
the number of chains
n_var(x)
the number of variables
n_iter(x)
the number of iterations
n_chain(x) n_iter(x) n_var(x)
n_chain(x) n_iter(x) n_var(x)
x |
an mcmc object, see "Details" for a list of supported mcmc objects. |
This is similar to coda::nchain()
but more general, working for
the following classes:
* mcmc
* mcmc.list
* data.table
(generated by mcmc_to_dt
)
* stanfit
* jags
Return the number of simulations
n_sims(x)
n_sims(x)
x |
mcmc list |
integer of number of simulations
Nicholas Tierney
post-hoc thinning of MCMC chains which have been converted to a data.table
thin_dt(dt, thin = 1)
thin_dt(dt, thin = 1)
dt |
an object of class "data.table" returned from |
thin |
thinning interval |
a data.table dataframe
library(coda) data(line) mcmc_dt <- mcmc_to_dt(line) thin_dt(mcmc_dt, thin = 10) thin_dt(mcmc_dt, thin = 2) thin_dt(mcmc_dt, thin = 20)
library(coda) data(line) mcmc_dt <- mcmc_to_dt(line) thin_dt(mcmc_dt, thin = 10) thin_dt(mcmc_dt, thin = 2) thin_dt(mcmc_dt, thin = 20)
tidy.mcmc.list
is a function that behaves like those from broom
. It takes
an mcmc.list object from coda.samples
and return a data frame that
summarises each parameters with its mean and quantiles and returns the output
as a data.table object. This can be called as tidy
. Currently summarises
over all chains.
## S3 method for class 'mcmc.list' tidy(x, conf_level = c(0.95), chain = FALSE, colnames = NULL, ...)
## S3 method for class 'mcmc.list' tidy(x, conf_level = c(0.95), chain = FALSE, colnames = NULL, ...)
x |
object of class "mcmc.list", as you would find with fitting a model
using |
conf_level |
level of the credible interval to be calculated. Can be multiple values. |
chain |
whether or not to summarise each parameter for each chain |
colnames |
which parameters we want from |
... |
extra arguments |
a data.table containing parameter summaries
Sam Clifford, [email protected]
library(coda) data(line) tidy(line) # Optionally ask for a subset of parameters with a vector of `colnames`, # and summarise for each chain: tidy(line, chain = TRUE, colnames=c("alpha")) # can provide two levels of confidence: tidy(line, conf_level = c(0.95, 0.50)) tidy(line, conf_level = c(0.95)) tidy(line, conf_level = c(0.89, 0.25))
library(coda) data(line) tidy(line) # Optionally ask for a subset of parameters with a vector of `colnames`, # and summarise for each chain: tidy(line, chain = TRUE, colnames=c("alpha")) # can provide two levels of confidence: tidy(line, conf_level = c(0.95, 0.50)) tidy(line, conf_level = c(0.95)) tidy(line, conf_level = c(0.89, 0.25))