---
title: "post-hoc MCMC with glmmTMB"
author: "Ben Bolker"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{post-hoc MCMC}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

```{r setup, include=FALSE, message=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = TRUE)
```

```{r libs,message=FALSE}
library(glmmTMB)
library(MCMCpack)
library(coda)
library(lattice)
library(ggplot2); theme_set(theme_bw())
library(reshape2)
```

Fit basic model:
```{r fit1}
data("sleepstudy",package="lme4")
fm1 <- glmmTMB(Reaction ~ Days + (Days|Subject),
               sleepstudy)
```

Set up for MCMC: extract coefficients and variance-covariance matrices as starting points.
```{r mcmcsetup}
## FIXME: better way for user to extract full coefs?
rawcoef <- with(fm1$obj$env,last.par[-random])
stopifnot(all.equal(c(fm1$obj$fn(rawcoef)),
                    -c(logLik(fm1)),
          tolerance=1e-7))
## log-likelihood function 
## (MCMCmetrop1R wants *positive* log-lik)
fn <- function(x) -fm1$obj$fn(x)
V <- vcov(fm1,full=TRUE)
```

```{r do_mcmc,echo=FALSE}
fn <- system.file("vignette_data","mcmc.rda",package="glmmTMB")
if (length(fn)==0) {
    s1 <- system.time(m1 <- MCMCmetrop1R(fn,rawcoef,V=V))
    save("s1","m1",file="mcmc.rda")
} else {
    load(fn)
}
```
```{r show_mcmc,eval=FALSE}
s1 <- system.time(m1 <- MCMCmetrop1R(fn,rawcoef,V=V))
```
(full run takes `r round(s1["elapsed"],1)` seconds)

Post-process for convenience:
```{r}
colnames(m1) <- c(names(fixef(fm1)[[1]]),
                        "log(sigma)",
                  c("log(sd_Intercept)","log(sd_Days)","cor"))
```

```{r traceplot,fig.width=7}
xyplot(m1,layout=c(2,3))
```

The trace plot for the correlation is extremely problematic; the effective sample size backs this up, as would any other diagnostics we did.

```{r effsize}
print(effectiveSize(m1),digits=3)
```

Leaving out the intercept because it's on a very different scale (an alternative would be to facet to give each variable its own scale, but if we want violin plots + faceting + horizontal layout we would need to pull in the `ggstance` package ...)

```{r violins,echo=FALSE}
ggplot(melt(as.matrix(m1[,-1])),aes(x=Var2,y=value))+
         geom_violin(fill="gray")+coord_flip()+labs(x="")
```

We can do a little better, e.g. by setting the `tune` argument of `MCMCmetrop1R` to `tune=c(1,1,1,1,1,100)`, but the parameter may simply be very poorly determined.

```{r echo=FALSE,eval=FALSE}
s2 <- system.time(m2 <- MCMCmetrop1R(fn,rawcoef,V=V,
                                     tune=c(1,1,1,1,1,100)))
```
## To do

- diagnose and solve mixing for cor parameter
- better description/tools for end users to get at raw parameters and understand their meanings (especially variance-covariance parameters)
- more complex example - e.g. Owls
- multi-chain/Gelman-Rubin example? (Don't think `MCMCmetrop1R` can do this; could easily cook up a simple multivariate M-H sampler, but wouldn't want to reimplement all the bells and whistles, e.g. adaptive tuning ...)

