---
title: "'MCMCvis' package"
author: "Casey Youngflesh"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{MCMCvis}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

## Intro

`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 package contains four functions:

- `MCMCsummary` - summarize MCMC output for particular parameters of interest
- `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

`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 and 'publication-ready' output.


&nbsp;


## MCMCsummary

`MCMCsummary` is used to output summary information from MCMC output. This function accepts `stanfit` objects, `mcmc.list` objects, `R2jags` model output, and matrices of MCMC output (one chain per column). The function automatically detects the object type and proceeds accordingly. Two decimal places are reported by default. This can be changed using the `digits` argument.

```{r, message=FALSE}
library(MCMCvis)

data(MCMC_data)

MCMCsummary(MCMC_data)
```

&nbsp;

Specific parameters can be specified to subset summary information. Partial names may be used to specify any parameter containing that name. For instance, for all `alpha` parameters.

```{r}
MCMCsummary(MCMC_data, 
          params = 'alpha')
```

&nbsp;

Individual parameters can also be specified.

```{r}
MCMCsummary(MCMC_data, 
          params = 'alpha[1]')
```

&nbsp;

The `excl` argument can be used to exclude any parameters. This can be used in conjunction with the `params` argument. For instance, if all `alpha` parameters are desired **except** for `alpha[1]`, `params = 'alpha', excl = 'alpha[1]'` can be used. These arguments can be used in any of the functions in the package.

```{r}
MCMCsummary(MCMC_data, 
          params = 'alpha',
          excl = 'alpha[1]')
```


&nbsp;

## MCMCtrace

`MCMCtrace` is used to create trace and density plots for MCMC output. This is useful for diagnostic purposes. Particular parameters can also be specified, as with `MCMCsummary`.

```{r, fig.width=5, fig.height=6}
MCMCtrace(MCMC_data, 
        params = c('beta[1]', 'beta[2]', 'beta[3]'))
```

&nbsp;

Just trace plot can be plotted with `type = 'trace'`. Just density plots can be plotted with `type = 'density'`. Default is `type = 'both'` which outputs both trace and density plots. Individual chains for the density plot can be output using the `ind` argument.

```{r, fig.width=5, fig.height=6}
MCMCtrace(MCMC_data, 
        params = c('beta[1]', 'beta[2]', 'beta[3]',
                'beta[4]', 'beta[5]', 'beta[6]'),
        type = 'density',
        ind = TRUE)
```

&nbsp;

`iter` denotes how many iterations shoudl be plotted for the chain the trace and density plots. The default is 2000, meaning that the last 2000 iterations of each chain are plotted. Remember, this is the final posterior chain, not including the specified burn-in (specified when the model was run).

```{r, fig.width=5, fig.height=6}
MCMCtrace(MCMC_data, 
        params = c('beta[1]', 'beta[2]', 'beta[3]'),
        iter = 1800,
        ind = TRUE)
```

&nbsp;

Trace plots can also be output to PDF format. This is recommended for large numbers of parameters, as output to PDF makes for quicker browsing compared to browsing within the R GUI. PDF document will be output to the current working directory by default, but another directory can be specified.

```{r, eval=FALSE}
MCMCtrace(MCMC_data, 
        pdf = TRUE, 
        filename = 'MYpdf', 
        wd = 'DIRECTORY HERE')
```

&nbsp;


## MCMCchains

`MCMCchains` is used to extract MCMC chains from MCMC objects. Chains can then be manipulated directly. Particular parameters can be specified as with other functions.

```{r}
ex <- MCMCchains(MCMC_data, 
               params = 'beta')

#extract mean values for each parameter
apply(ex, 2, mean)
```

&nbsp;


## MCMCplot

`MCMCplot` is used to create caterpillar plots from MCMC output. Points represent posterior medians. For parameters where 50% credible intervals overlap 0 are indicated by 'open' circles. For parameters where 50 percent credible intervals DO NOT overlap 0 AND 95 percent credible intervals DO overlap 0 are indicated by 'closed' grey circles. For parameters where 95 percent credible intervals DO NOT overlap 0 are indicated by 'closed' black circles. Thick lines represent 50 percent credible intervals while thin lines represent 95 percent credible intervals.

As with the other functions in the package, particular parameters of interest can be specified.

```{r, fig.width=5, fig.height=6}
MCMCplot(MCMC_data, 
       params = 'beta')
```

&nbsp;

`ref_ovl = FALSE` can be used to disable this feature. All median dots will be represented as 'closed' black circles. A vertical reference at 0 is plotted by default. The position of this reference line can be modified with the `ref` argument. `ref = NULL` removes the reference line altogether.

```{r, fig.width=5, fig.height=6}
MCMCplot(MCMC_data, 
       params = 'beta',
       ref_ovl = FALSE, 
       ref = NULL)
```

&nbsp;

Parameters can be ranked by posterior median estimates using the `rank` argument. `xlab` can be used to create an alternative label for the x-axis.

```{r, fig.width=5, fig.height=6}
MCMCplot(MCMC_data, 
       params = 'beta', 
       rank = TRUE,
       xlab = 'ESTIMATE')
```

&nbsp;

The orientation of the plot can also be change using the `horiz` argument. `ylab` is then used to specify an alternaive label on the 'estimate axis'.

```{r, fig.width=5, fig.height=6}
MCMCplot(MCMC_data, 
       params = 'beta', 
       rank = TRUE,
       horiz = FALSE,
       ylab = 'ESTIMATE')
```


Graphical parameters for x and y-axis limitation, row labels, title, median dot size, CI line thickness, axis and tick thickness, text size, and margins can be specified.

```{r, fig.width=5, fig.height=6}
MCMCplot(MCMC_data, 
       params = 'beta', 
       xlim = c(-60, 25),
       xlab = 'My x-axis label',
       main = 'MCMCvis plot',
       labels = c('First param', 'Second param', 'Third param', 
                  'Fourth param', 'Fifth param', 'Sixth param', 
                  'Seventh param', 'Eighth param', 'Nineth param', 
                  'Tenth param'), 
       labels_sz = 1.5,
       med_sz = 2,
       thick_sz = 7,
       thin_sz = 3,
       ax_sz = 4,
       main_text_sz = 2)
```

**For more information see `?MCMCplot`**


&nbsp;

&nbsp;
