Title: | Tools to Visualize, Manipulate, and Summarize MCMC Output |
---|---|
Description: | Performs key functions for MCMC analysis using minimal code - visualizes, manipulates, and summarizes MCMC output. Functions support simple and straightforward subsetting of model parameters within the calls, and produce presentable and 'publication-ready' output. MCMC output may be derived from Bayesian model output fit with 'Stan', 'NIMBLE', 'JAGS', and other software. |
Authors: | Casey Youngflesh [aut, cre] , Christian Che-Castaldo [aut] , Tyler Hardy [ctb] |
Maintainer: | Casey Youngflesh <[email protected]> |
License: | GPL-3 |
Version: | 0.16.4 |
Built: | 2024-10-18 05:17:00 UTC |
Source: | https://github.com/caseyyoungflesh/mcmcvis |
'MCMCvis' is an R package used to visualize, manipulate, and summarize MCMC output. MCMC output may be derived from Bayesian model output fit with JAGS, Stan, or other MCMC samplers.
The following functions are currently available:
-MCMCsummary
- summarize MCMC output for particular parameters of interest
-MCMCpstr
- summarize MCMC output for particular parameters of interest while preserving original parameter structure
-MCMCtrace
- create trace and density plots of MCMC chains for particular parameters of interest
-MCMCchains
- easily extract posterior chains from MCMC output for particular parameters of interest
-MCMCplot
- create caterpillar plots from MCMC output for particular parameters of interest)
-MCMCdiag
- create a .txt file and save specified objects that summarize model inputs, outputs, and diagnostics
Example data can be loaded using data(MCMC_data)
.
‘MCMCvis' was designed to perform key functions for MCMC analysis using minimal code, in order to free up time/brainpower for interpretation of analysis results. Functions support simple and straightforward subsetting of model parameters within the calls, and produce presentable, ’publication-ready' output.
The vignette can be run using vignette('MCMCvis')
if vignette is built when installing package.
Casey Youngflesh <[email protected]> Christian Che-Castaldo <[email protected]>
Maintainer: Casey Youngflesh [email protected] (ORCID)
Authors:
Christian Che-Castaldo [email protected] (ORCID)
Other contributors:
Tyler Hardy [contributor]
Useful links:
Report bugs at https://github.com/caseyyoungflesh/MCMCvis/issues
Sample MCMC output containing 12 parameters - alpha[1]
, ... , alpha[6]
, beta[1]
, ... , beta[6]
.
MCMC_data
MCMC_data
mcmc.list
object with 3 chains for each parameter, 6000 iterations for each chain.
Sample MCMC output containing 12 parameters - alpha[1]
, ... , alpha[6]
, beta[1]
, ... , beta[6]
.
MCMC_data2
MCMC_data2
mcmc.list
object with 3 chains for each parameter, 6000 iterations for each chain.
Extract posterior chains from MCMC output for specific parameters of interest.
MCMCchains( object, params = "all", excl = NULL, ISB = TRUE, exact = TRUE, mcmc.list = FALSE, chain_num = NULL )
MCMCchains( object, params = "all", excl = NULL, ISB = TRUE, exact = TRUE, mcmc.list = FALSE, chain_num = NULL )
object |
Object containing MCMC output. See DETAILS below. |
params |
Character string (or vector of character strings) denoting parameters of interest. Default |
excl |
Character string (or vector of character strings) denoting parameters to exclude. Used in conjunction with |
ISB |
Ignore Square Brackets (ISB). Logical specifying whether square brackets should be ignored in the |
exact |
Logical specifying whether input from |
mcmc.list |
Logical specifying whether to return an mcmc.list. If |
chain_num |
Numeric - specifies posterior chain number. When a value is specified, posterior for only that chain is output. Useful for determining the last iteration for each parameter, to be used as initial values in a subsequent model, to effectively 'continue' a model run. |
Function returns matrix with one parameter per column (for specified parameters). Each iteration is represented as a row. Multiple chains for each parameter are combined to one posterior chain (unless chain_num
is specified, in which case only the specified chain will be returned). Parameters are arranged in columns alphabetically.
object
argument can be a stanfit
object (rstan
package), a CmdStanMCMC
object (cmdstanr
package), a stanreg
object (rstanarm
package), a brmsfit
object (brms
package), an mcmc.list
object (coda
and rjags
packages), mcmc
object (coda
and nimble
packages), list
object (nimble
package), an R2jags
model object (R2jags
package), a jagsUI
model object (jagsUI
package), or a matrix containing MCMC chains (each column representing MCMC output for a single parameter, rows representing iterations in the chain). The function automatically detects the object type and proceeds accordingly.
#Load data data(MCMC_data) #Extract MCMC chains ex <- MCMCchains(MCMC_data) apply(ex, 2, mean) #Extract MCMC chains for just 'beta' parameters ex2 <- MCMCchains(MCMC_data, params = 'beta') apply(ex2, 2, mean) #Just 'beta[1]', 'beta[4]', and 'alpha[3]' ex3 <- MCMCchains(MCMC_data, params = c('beta[1]', 'beta[4]', 'alpha[3]'), ISB = FALSE, exact = TRUE) apply(ex3, 2, sd)
#Load data data(MCMC_data) #Extract MCMC chains ex <- MCMCchains(MCMC_data) apply(ex, 2, mean) #Extract MCMC chains for just 'beta' parameters ex2 <- MCMCchains(MCMC_data, params = 'beta') apply(ex2, 2, mean) #Just 'beta[1]', 'beta[4]', and 'alpha[3]' ex3 <- MCMCchains(MCMC_data, params = c('beta[1]', 'beta[4]', 'alpha[3]'), ISB = FALSE, exact = TRUE) apply(ex3, 2, sd)
Model diagnostics and summary. Function reads information embedded in model fit object. Output varies by model fit object type but includes model run inputs, diagnostic information, and parameter summary. See DETAILS below for more information.
MCMCdiag( object, file_name, dir = getwd(), mkdir, add_field, add_field_names, save_object = FALSE, params = "all", excl = NULL, ISB = TRUE, exact = TRUE, obj_name, add_obj, add_obj_names, cp_file, cp_file_names, open_txt = TRUE, summary = TRUE, ... )
MCMCdiag( object, file_name, dir = getwd(), mkdir, add_field, add_field_names, save_object = FALSE, params = "all", excl = NULL, ISB = TRUE, exact = TRUE, obj_name, add_obj, add_obj_names, cp_file, cp_file_names, open_txt = TRUE, summary = TRUE, ... )
object |
Object containing MCMC output. See DETAILS below. |
file_name |
Character string with name of .txt file to be saved to |
dir |
Character string with directory where file(s) (or directory is argument for |
mkdir |
Character string with name of directory to be created. If specified, a directory will be created within the directory specified by |
add_field |
Object (or vector of objects) to be added to the .txt file. |
add_field_names |
Character string (or vector of character strings) specifying the name(s) of the |
save_object |
Logical specifying whether the model output provided to the function ( |
params |
Character string (or vector of character strings) denoting parameters of interest for summary. Object to be saved out will be a subset of parameters and will be an |
excl |
Character string (or vector of character strings) denoting parameters to exclude. Used in conjunction with |
ISB |
Ignore Square Brackets (ISB). Logical specifying whether square brackets should be ignored in the |
exact |
Logical specifying whether input from |
obj_name |
Character string specifying the file name of the |
add_obj |
List with additional object(s) to be saved as |
add_obj_names |
Character string (or vector of character strings) specifying the name(s) of the objects to be saved as |
cp_file |
Character string (or vector of character strings) specifying file(s) to be copied to |
cp_file_names |
Character string (or vector of character strings) specifying new names for files to be copied specified by |
open_txt |
Logical - if |
summary |
Logical specifying whether or not to output summary information from MCMCsummary at the bottom of the .txt file. |
... |
Arguments to be passed to MCMCsummary when generating summary if |
Some diagnostic information is only provided for models fit with particular pieces of software. For example, rstan
output includes additional diagnostics related to the NUTS sampler. Output from jagsUI
includes runtime information, but output from rjags
does not. Note that this information could be fed manually to the function using the add_field
argument.
object
argument can be a stanfit
object (rstan
package), a CmdStanMCMC
object (cmdstanr
package), a stanreg
object (rstanarm
package), a brmsfit
object (brms
package), an mcmc.list
object (coda
and rjags
packages), mcmc
object (coda
and nimble
packages), list
object (nimble
package), an R2jags
model object (R2jags
package), a jagsUI
model object (jagsUI
package), or a matrix containing MCMC chains (each column representing MCMC output for a single parameter, rows representing iterations in the chain). The function automatically detects the object type and proceeds accordingly.
Output presented in .txt file varies by model fit object type but includes: model run time, number of warmup/burn-in iterations, total iterations, thinning rate, number of chains, specified adapt delta, specified max tree depth, specific initial step size, mean accept stat, mean tree depth, mean step size, number of divergent transitions, number max tree depth exceeds, number of chains with BFMI warnings, max Rhat (maximum Rhat of any parameter printed), min n.eff (minimum n.eff of any parameter printed), parameter summary information (passed from MCMCsummary
), and any additional information fed to the add_field
argument. See documentation for specific software used to fit model for more information on particular diagnostics.
#Load data data(MCMC_data) # MCMCdiag(MCMC_data, # #name of .txt file to be saved # file_name = 'blog-model-summary-2021-01-15', # #directory within which to save .txt file # dir = '~/Desktop', # #round MCMCsummary output in .txt file to two digits # round = 2, # #add two fields to .txt file # add_field = c(50, '1.0'), # #names of two additional fields to add to .txt file # add_field_names = c('Time (min)', 'Data version'))
#Load data data(MCMC_data) # MCMCdiag(MCMC_data, # #name of .txt file to be saved # file_name = 'blog-model-summary-2021-01-15', # #directory within which to save .txt file # dir = '~/Desktop', # #round MCMCsummary output in .txt file to two digits # round = 2, # #add two fields to .txt file # add_field = c(50, '1.0'), # #names of two additional fields to add to .txt file # add_field_names = c('Time (min)', 'Data version'))
Visualize posterior distributions from MCMC output for specific parameters of interest using caterpillar plots. Color of median dot represents the overlap of the posterior distribution with 0 (or other specified value).
MCMCplot( object, object2 = NULL, params = "all", HPD = FALSE, ci = c(50, 95), excl = NULL, ISB = TRUE, exact = TRUE, ref = 0, ref_ovl = FALSE, col = "black", col2 = "red", offset = 0.1, rank = FALSE, horiz = TRUE, xlim, ylim, xlab, ylab, main, labels, guide_lines = FALSE, guide_axis = TRUE, sz_labels = 1.2, sz_med = 1.5, sz_thick = 5, sz_thin = 2, sz_ax = 3, sz_ax_txt = 1.3, sz_tick_txt = 1.2, sz_main_txt = 1.2, pos_tick, mar = c(5.1, 4.1, 4.1, 2.1) )
MCMCplot( object, object2 = NULL, params = "all", HPD = FALSE, ci = c(50, 95), excl = NULL, ISB = TRUE, exact = TRUE, ref = 0, ref_ovl = FALSE, col = "black", col2 = "red", offset = 0.1, rank = FALSE, horiz = TRUE, xlim, ylim, xlab, ylab, main, labels, guide_lines = FALSE, guide_axis = TRUE, sz_labels = 1.2, sz_med = 1.5, sz_thick = 5, sz_thin = 2, sz_ax = 3, sz_ax_txt = 1.3, sz_tick_txt = 1.2, sz_main_txt = 1.2, pos_tick, mar = c(5.1, 4.1, 4.1, 2.1) )
object |
Object containing MCMC output. See DETAILS below. |
object2 |
Optional second object containing MCMC output. If specified, parameter estimates from each model will be displayed in a paired manner. Parameter names for |
params |
Character string (or vector of character strings) denoting parameters to be plotted. Default |
HPD |
Logical specifying whether to calculate equal-tailed credible intervals ( |
ci |
Numeric vector of length 2, where each element is (0,100] and represents the width of an equal-tailed ( |
excl |
Character string (or vector of character strings) denoting parameters to exclude. Used in conjunction with |
ISB |
Ignore Square Brackets (ISB). Logical specifying whether square brackets should be ignored in the |
exact |
Logical specifying whether input from |
ref |
Value indicating where vertical reference line should be created and what value to use a reference for caterpillar median coloration. Default is Argument |
ref_ovl |
Logical specifying whether the style/color of plotted median dots and CI should be changed based on whether the specified credible intervals (50 % and 95 % by default) overlap the reference line. See DETAILS for more information. |
col |
Character string (or vector of character strings) specifying which color to render estimates on plot. When |
col2 |
Character string (or vector of character strings) specifying which color to render estimates on plot for |
offset |
Value indicating how much to offset plotted posteriors when |
rank |
Logical specifying whether output should be ranked. If |
horiz |
Logical specifying orientation of plot. If |
xlim |
Numerical vector of length 2, indicating range of x-axis. Only applicable if |
ylim |
Numerical vector of length 2, indicating range of y-axis. Only applicable if |
xlab |
Character string labeling x-axis. Only applicable if Default label is 'Parameter Estimate'. Option |
ylab |
Character string labeling y-axis. Only applicable if Default label is 'Parameter Estimate'. Option |
main |
Character string indicating title of plot. |
labels |
Character string (or vector of character strings if plotting > 1 parameter) labeling parameter estimates along y-axis (if Default option will use parameter names from Option |
guide_lines |
Logical specifying whether to plot reference lines for each parameter in order to better visualize which parameter names correspond to each posterior. |
guide_axis |
Logical specifying whether a second axis should be plotted (x-axis if |
sz_labels |
Number specifying size of text for parameter labels on axis. |
sz_med |
Number specifying size of points represents posterior medians. |
sz_thick |
Number specifying thickness of 50 percent CI line (thicker line). |
sz_thin |
Number specifying thickness of 95 percent CI line (thinner line). |
sz_ax |
Number specifying thickness of axis and ticks. |
sz_ax_txt |
Number specifying size of text for axis label. |
sz_tick_txt |
Number specifying size of text for tick labels on axis. |
sz_main_txt |
Number specifying size of text for main title. |
pos_tick |
Numeric vector specifying where ticks on axis should be placed. |
mar |
Numerical vector of length 4 specifying plot margins - (BOTTOM, LEFT, TOP, RIGHT). Changes to the margin should be made within the function rather than using the Default is c(5.1, 4.1, 4.1, 2.1) - the R plot default. |
Points represent posterior medians. Parameters where the smaller specified credible intervals (first value in vector provided to ci
argument; 50% by default) overlap 0 (or other value specified by the ref
argument) are indicated by 'open' circles and a lighter color than that specified (e.g., gray will be displayed with the default black is specified for col
). Parameters where the specific smaller credible intervals DO NOT overlap 0 (or other specified value) AND the larger specified credible intervals (second value in vector provided to ci
argument; 95% by default) DO overlap 0 (or other specified value) are indicated by 'closed' circles and a lighter color that that specified. Parameters where the larger specified credible intervals DO NOT overlap 0 (or other specified value) are indicated by 'closed' circles and the color specified (black by default). Thick lines represent the smaller specified credible intervals percent credible intervals (50% by default) while thin lines represent the larger specified credible intervals (95 % by default). ref_ovl = TRUE
can be used to enable this feature. When two model objects are supplied to the function (with object
and object2
) and no argument is supplied to col
or col2
, light red lines can be interpreted as analogous to gray lines. When ref_ovl = TRUE
and a color (or colors) other than the default are specified (with the col
and col2
arguments) lighter versions of the color specified are used in black of the light gray and/or light red lines.
When object2
is specified, paired caterpillar plots of each parameter are produced. For this reason, parameter names of object
and object2
specified with the params
argument must be identical (to be used for comparing posterior estimates of similar models). col
and col2
arguments can be specified to change the color of output from object
and object2
, respectively. By default, output from object
is plotted in black and object2
is plotted in red. The ref_ovl
argument can also be specified.
object
argument can be a stanfit
object (rstan
package), a CmdStanMCMC
object (cmdstanr
package), a stanreg
object (rstanarm
package), a brmsfit
object (brms
package), an mcmc.list
object (coda
and rjags
packages), mcmc
object (coda
and nimble
packages), list
object (nimble
package), an R2jags
model object (R2jags
package), a jagsUI
model object (jagsUI
package), or a matrix containing MCMC chains (each column representing MCMC output for a single parameter, rows representing iterations in the chain). The function automatically detects the object type and proceeds accordingly.
When specifying rank = TRUE
and specifying labels for labels
, labels will be applied to parameters before they are ranked.
Thanks to Cinner et al. 2016, whose Fig. 1 inspired this plot.
Cinner, J. E., C. Huchery, M. A. MacNeil, N. A. J. Graham, T. R. McClanahan, J. Maina, E. Maire, J. N. Kittinger, C. C. Hicks, C. Mora, E. H. Allison, S. D'Agata, A. Hoey, D. A. Feary, L. Crowder, I. D. Williams, M. Kulbicki, L. Vigliola, L. Wantiez, G. Edgar, R. D. Stuart-Smith, S. A. Sandin, A. L. Green, M. J. Hardt, M. Beger, A. Friedlander, S. J. Campbell, K. E. Holmes, S. K. Wilson, E. Brokovich, A. J. Brooks, J. J. Cruz-Motta, D. J. Booth, P. Chabanet, C. Gough, M. Tupper, S. C. A. Ferse, U. R. Sumaila, and D. Mouillot. 2016. Bright spots among the world's coral reefs. Nature 535:416-419.
#Load data data(MCMC_data) #Plot MCMC output MCMCplot(MCMC_data, labels = NULL) #Just 'beta' parameters MCMCplot(MCMC_data, params = 'beta') #Just 'beta' parameters using highest posterior density intervals MCMCplot(MCMC_data, params = 'beta', HPD = TRUE) #Just 'beta[1]', 'beta[4]', and 'alpha[3]' MCMCplot(MCMC_data, params = c('beta[1]', 'beta[4]', 'alpha[3]'), ISB = FALSE, exact = TRUE) #Just 'beta[1]', 'beta[4]', and 'alpha[3]' and change the credible interval widths MCMCplot(MCMC_data, ci = c(50, 89), params = c('beta[1]', 'beta[4]', 'alpha[3]'), ISB = FALSE, exact = TRUE) #Rank parameters by posterior mean MCMCplot(MCMC_data, params = 'beta', rank = TRUE) #Create vertical plot MCMCplot(MCMC_data, params = 'beta', horiz = FALSE)
#Load data data(MCMC_data) #Plot MCMC output MCMCplot(MCMC_data, labels = NULL) #Just 'beta' parameters MCMCplot(MCMC_data, params = 'beta') #Just 'beta' parameters using highest posterior density intervals MCMCplot(MCMC_data, params = 'beta', HPD = TRUE) #Just 'beta[1]', 'beta[4]', and 'alpha[3]' MCMCplot(MCMC_data, params = c('beta[1]', 'beta[4]', 'alpha[3]'), ISB = FALSE, exact = TRUE) #Just 'beta[1]', 'beta[4]', and 'alpha[3]' and change the credible interval widths MCMCplot(MCMC_data, ci = c(50, 89), params = c('beta[1]', 'beta[4]', 'alpha[3]'), ISB = FALSE, exact = TRUE) #Rank parameters by posterior mean MCMCplot(MCMC_data, params = 'beta', rank = TRUE) #Create vertical plot MCMCplot(MCMC_data, params = 'beta', horiz = FALSE)
Extract summary information and posterior chains from MCMC output (specific function specified) for specific parameters of interest while preserving original parameter structure (i.e., scalar, vector, matrix, array). Function outputs a list
with calculated values or posterior chains for each specified parameter.
MCMCpstr( object, params = "all", excl = NULL, ISB = TRUE, exact = TRUE, func = mean, type = "summary" )
MCMCpstr( object, params = "all", excl = NULL, ISB = TRUE, exact = TRUE, func = mean, type = "summary" )
object |
Object containing MCMC output. See DETAILS below. |
params |
Character string (or vector of character strings) denoting parameters to be returned in output. Default |
excl |
Character string (or vector of character strings) denoting parameters to exclude. Used in conjunction with |
ISB |
Ignore Square Brackets (ISB). Logical specifying whether square brackets should be ignored in the |
exact |
Logical specifying whether input from |
func |
Function to be performed on MCMC output. When output of specified function is greater than length 1, an extra dimension is added. For instance, output of length 3 for a parameter with dimensions 2x2 results in a 2x2x3 output. Functions that produce output with dimensionality greater than 1 are not permitted. |
type |
Character string specifying whether to return summary information (calculated based on |
object
argument can be a stanfit
object (rstan
package), a CmdStanMCMC
object (cmdstanr
package), a stanreg
object (rstanarm
package), a brmsfit
object (brms
package), an mcmc.list
object (coda
and rjags
packages), mcmc
object (coda
and nimble
packages), list
object (nimble
package), an R2jags
model object (R2jags
package), a jagsUI
model object (jagsUI
package), or a matrix containing MCMC chains (each column representing MCMC output for a single parameter, rows representing iterations in the chain). The function automatically detects the object type and proceeds accordingly.
#Load data data(MCMC_data) MCMCpstr(MCMC_data, func = function(x) quantile(x, probs = c(0.01, 0.99)))
#Load data data(MCMC_data) MCMCpstr(MCMC_data, func = function(x) quantile(x, probs = c(0.01, 0.99)))
Extract summary information from MCMC output (mean, median, quantiles, Gelman-Rubin convergence statistic, number of effective samples, and specified custom metrics) for specific parameters of interest.
MCMCsummary( object, params = "all", excl = NULL, ISB = TRUE, exact = TRUE, probs = c(0.025, 0.5, 0.975), hpd_prob = 0.95, HPD = FALSE, pg0 = FALSE, digits = NULL, round = NULL, Rhat = TRUE, n.eff = TRUE, func = NULL, func_name = NULL )
MCMCsummary( object, params = "all", excl = NULL, ISB = TRUE, exact = TRUE, probs = c(0.025, 0.5, 0.975), hpd_prob = 0.95, HPD = FALSE, pg0 = FALSE, digits = NULL, round = NULL, Rhat = TRUE, n.eff = TRUE, func = NULL, func_name = NULL )
object |
Object containing MCMC output. See DETAILS below. |
params |
Character string (or vector of character strings) denoting parameters to be returned in summary output. Default |
excl |
Character string (or vector of character strings) denoting parameters to exclude. Used in conjunction with |
ISB |
Ignore Square Brackets (ISB). Logical specifying whether square brackets should be ignored in the |
exact |
Logical specifying whether input from |
probs |
Numeric vector where each element in (0,1) representing probabilities used to calculate posterior sample quantiles for the selected parameters. Default is c(0.025, 0.5, 0.975). |
hpd_prob |
Scalar in (0,1) representing probability used to calculate highest posterior density intervals for the selected parameters. Default is 0.95. |
HPD |
Logical specifying whether to calculate equal-tailed credible intervals ( |
pg0 |
Logical specifying whether to calculate the proportion of the posterior that is greater than 0, rounded to 2 digits. |
digits |
Number of significant digits to include for posterior summary. All computed digits will be included by default. Note that Rhat is always rounded to 2 decimal places. |
round |
Number of decimal places to round to for posterior summary. Cannot be used in conjunction with |
Rhat |
Logical specifying whether to calculate and display the potential scale reduction statistic (Rhat). Values near 1 suggest convergence (Brooks and Gelman 1998). |
n.eff |
Logical specifying whether to calculate and display the number of effective samples for each parameter. |
func |
Function to be performed on MCMC output. If a function is specified, it will be evaluated on posteriors for each specified parameter and returned as a column in the summary output (or multiple columns if the function returns more than one value). |
func_name |
Character string (or vector of character strings) specifying labels for output from |
Function returns summary information (including parameter posterior mean, posterior sd, quantiles, potential scale reduction statistic (Rhat), number of effective samples, and other specified metrics) for specified parameters.
object
argument can be a stanfit
object (rstan
package), a CmdStanMCMC
object (cmdstanr
package), a stanreg
object (rstanarm
package), a brmsfit
object (brms
package), an mcmc.list
object (coda
and rjags
packages), mcmc
object (coda
and nimble
packages), list
object (nimble
package), an R2jags
model object (R2jags
package), a jagsUI
model object (jagsUI
package), or a matrix containing MCMC chains (each column representing MCMC output for a single parameter, rows representing iterations in the chain). The function automatically detects the object type and proceeds accordingly.
For mcmc.list
, mcmc
, and list
objects, the potential scale reduction statistic statistic (Rhat) is calculated using the gelman.diag
function in the coda
package (what is typically displayed in the summary output from models fit with JAGS). For stanfit
(as well as CmdStanMCMC
, stanreg
, and brmsfit
objects) and jagsUI
objects, Rhat is calculated using a 'split chain' Rhat (in their respective packages), which is thought to be a more conservative diagnostic (Stan Development Team 2018).
For mcmc.list
, mcmc
, and list
objects, the number of effective samples is calculated using the effectiveSize
function in the coda
package. For stanfit
(as well as CmdStanMCMC
, stanreg
, and brmsfit
objects) and jagsUI
objects, n.eff is calculated using a slightly different method of computation for the number of effective samples (Stan Development Team 2018). For CmdStanMCMC
objects, both bulk and tail n.eff is calculated.
Brooks, S. P., and A. Gelman. 1998. General methods for monitoring convergence of iterative simulations. Journal of Computational and Graphical Statistics 7:434.
Stan Development Team. 2018. Stan Modeling Language Users Guide and Reference Manual, Version 2.18.0. https://mc-stan.org
#Load data data(MCMC_data) #Summary information for MCMC output - display 2 significant digits MCMCsummary(MCMC_data, digits = 2) #Just 'beta' parameters - round to 2 decimal places MCMCsummary(MCMC_data, params = 'beta', round = 2) #Just 'beta[1]', 'beta[4]', and 'alpha[3]' MCMCsummary(MCMC_data, params = c('beta[1]', 'beta[4]', 'alpha[3]'), ISB = FALSE, exact = TRUE)
#Load data data(MCMC_data) #Summary information for MCMC output - display 2 significant digits MCMCsummary(MCMC_data, digits = 2) #Just 'beta' parameters - round to 2 decimal places MCMCsummary(MCMC_data, params = 'beta', round = 2) #Just 'beta[1]', 'beta[4]', and 'alpha[3]' MCMCsummary(MCMC_data, params = c('beta[1]', 'beta[4]', 'alpha[3]'), ISB = FALSE, exact = TRUE)
Trace and density plots of MCMC chains for specific parameters of interest. Print plots to pdf by default.
MCMCtrace( object, params = "all", excl = NULL, ISB = TRUE, exact = TRUE, iter = 5000, gvals = NULL, priors = NULL, post_zm = TRUE, PPO_out = FALSE, Rhat = FALSE, n.eff = FALSE, ind = FALSE, pdf = TRUE, plot = TRUE, open_pdf = TRUE, filename, wd = getwd(), type = "both", ylim = NULL, xlim = NULL, xlab_tr, ylab_tr, xlab_den, ylab_den, main_den = NULL, main_tr = NULL, lwd_den = 1, lwd_pr = 1, lty_den = 1, lty_pr = 1, col_den, col_pr, col_txt, sz_txt = 1.2, sz_ax = 1, sz_ax_txt = 1, sz_tick_txt = 1, sz_main_txt = 1.2, pos_tick_x_tr = NULL, pos_tick_y_tr = NULL, pos_tick_x_den = NULL, pos_tick_y_den = NULL )
MCMCtrace( object, params = "all", excl = NULL, ISB = TRUE, exact = TRUE, iter = 5000, gvals = NULL, priors = NULL, post_zm = TRUE, PPO_out = FALSE, Rhat = FALSE, n.eff = FALSE, ind = FALSE, pdf = TRUE, plot = TRUE, open_pdf = TRUE, filename, wd = getwd(), type = "both", ylim = NULL, xlim = NULL, xlab_tr, ylab_tr, xlab_den, ylab_den, main_den = NULL, main_tr = NULL, lwd_den = 1, lwd_pr = 1, lty_den = 1, lty_pr = 1, col_den, col_pr, col_txt, sz_txt = 1.2, sz_ax = 1, sz_ax_txt = 1, sz_tick_txt = 1, sz_main_txt = 1.2, pos_tick_x_tr = NULL, pos_tick_y_tr = NULL, pos_tick_x_den = NULL, pos_tick_y_den = NULL )
object |
Object containing MCMC output. See DETAILS below. |
params |
Character string (or vector of character strings) denoting parameters of interest. Default |
excl |
Character string (or vector of character strings) denoting parameters to exclude. Used in conjunction with |
ISB |
Ignore Square Brackets (ISB). Logical specifying whether square brackets should be ignored in the |
exact |
Logical specifying whether input from |
iter |
Number of iterations to plot for trace and density plots. The default value is 5000, meaning the last 5000 iterations of the chain will be plotted. |
gvals |
Vector containing generating values if simulated data was used to fit model. These values will be plotted as vertical lines on the density plots to compare posterior distributions with the true parameter values used to generate the data. No line will be apparent if the generating value is outside the plotted range of the posterior distribution. |
priors |
Matrix containing random draws from prior distributions corresponding to parameters of interest. If specified, priors are plotted along with posterior density plots. Percent overlap between prior and posterior (PPO) is also calculated and displayed on each plot. Each column of the matrix represents a prior for a different parameter. Parameters are plotted alphabetically - priors should be sorted accordingly. If |
post_zm |
Logical - if |
PPO_out |
Logical - if |
Rhat |
Logical - if |
n.eff |
Logical - if |
ind |
Logical - if |
pdf |
Logical - if |
plot |
Logical - if |
open_pdf |
Logical - if |
filename |
Name of pdf file to be printed. Default is 'MCMCtrace'. |
wd |
Working directory for pdf output. Default is current directory. |
type |
Type of plot to be output. |
ylim |
Vector of length two specifying limits for y-axis on density plots only. If specified, overrides argument |
xlim |
Vector of length two specifying limits for x-axis on density plots only. If specified, overrides argument |
xlab_tr |
Character string specifying label for x-axis on trace plots. |
ylab_tr |
Character string specifying label for x-axis on trace plots. |
xlab_den |
Character string specifying label for x-axis on density plots. |
ylab_den |
Character string specifying label for x-axis on density plots. |
main_den |
Character string (or vector of character strings if plotting > 1 parameter) specifying title(s) of density plot(s). |
main_tr |
Character string (or vector of character strings if plotting > 1 parameter) specifying title(s) of trace plot(s). |
lwd_den |
Number specifying thickness of density line on density plots. |
lwd_pr |
Number specifying thickness of prior line on density plots. |
lty_den |
Number specifying the line type for the density line on density plots. |
lty_pr |
Number specifying the line type for the prior line on density plots. |
col_den |
Character string specifying color of density line on density plots. Does not specify color if |
col_pr |
Character string specifying color of prior line on density plots. |
col_txt |
Character string specifying color of text (denoting PPO) on plot when value specified for |
sz_txt |
Number specifying size of text (denoting PPO) when value specified for |
sz_ax |
Number specifying thickness of axes and ticks. |
sz_ax_txt |
Number specifying size of text for axes labels. |
sz_tick_txt |
Number specifying size of text for tick labels on axis. |
sz_main_txt |
Number specifying size of text for main title. |
pos_tick_x_tr |
Numeric vector specifying where ticks on x-axis should be placed for trace plots. |
pos_tick_y_tr |
Numeric vector specifying where ticks on y-axis should be placed for trace plots. |
pos_tick_x_den |
Numeric vector specifying where ticks on x-axis should be placed for density plots. |
pos_tick_y_den |
Numeric vector specifying where ticks on y-axis should be placed for density plots. |
object
argument can be a stanfit
object (rstan
package), a stanreg
object (rstanarm
package), a brmsfit
object (brms
package), an mcmc.list
object (coda
and rjags
packages), mcmc
object (coda
and nimble
packages), list
object (nimble
package), an R2jags
model object (R2jags
package), a jagsUI
model object (jagsUI
package), or a matrix containing MCMC chains (each column representing MCMC output for a single parameter, rows representing iterations in the chain). The function automatically detects the object type and proceeds accordingly.
Matrices for the priors
argument can be generated using commands such as rnorm, rgamma, runif, etc. Distributions not supported by base R can be generated by using the appropriate packages. It is important to note that some discrepancies between MCMC samplers and R may exist regarding the parameterization of distributions - one example of this is the use of precision in JAGS but standard deviation in R for the 'second parameter' of the normal distribution. If the number of draws for each prior distribution is greater than the total number used for the density plot (iter
times the number of chains), the function will use a subset of the prior draws. If the number of draws for each prior distribution is less than the total number used for the density plot, the function will resample (with replacement) from the prior to obtain the appropriate number of draws.
#Load data data(MCMC_data) #Traceplots for all 'beta' parameters - pdf is generated by default MCMCtrace(MCMC_data, params = 'beta', pdf = FALSE) #Traceplots (individual density lines for each chain) just for 'beta[1]' MCMCtrace(MCMC_data, params = 'beta[1]', ISB = FALSE, exact = TRUE, ind = TRUE, pdf = FALSE) #Plot prior on top of posterior, calculate prior/posterior overlap (PPO) #just for 'beta[1]' #Add Rhat and n.eff values to density plots PR <- rnorm(15000, 0, 32) MCMCtrace(MCMC_data, params = 'beta[1]', ISB = FALSE, exact = TRUE, priors = PR, pdf = FALSE, Rhat = TRUE, n.eff = TRUE) #Output PPO to R object without plotting trace plots PR <- rnorm(15000, 0, 32) PPO <- MCMCtrace(MCMC_data, params = 'beta[1]', ISB = FALSE, exact = TRUE, priors = PR, plot = FALSE, PPO_out = TRUE)
#Load data data(MCMC_data) #Traceplots for all 'beta' parameters - pdf is generated by default MCMCtrace(MCMC_data, params = 'beta', pdf = FALSE) #Traceplots (individual density lines for each chain) just for 'beta[1]' MCMCtrace(MCMC_data, params = 'beta[1]', ISB = FALSE, exact = TRUE, ind = TRUE, pdf = FALSE) #Plot prior on top of posterior, calculate prior/posterior overlap (PPO) #just for 'beta[1]' #Add Rhat and n.eff values to density plots PR <- rnorm(15000, 0, 32) MCMCtrace(MCMC_data, params = 'beta[1]', ISB = FALSE, exact = TRUE, priors = PR, pdf = FALSE, Rhat = TRUE, n.eff = TRUE) #Output PPO to R object without plotting trace plots PR <- rnorm(15000, 0, 32) PPO <- MCMCtrace(MCMC_data, params = 'beta[1]', ISB = FALSE, exact = TRUE, priors = PR, plot = FALSE, PPO_out = TRUE)