# What is going on in an MCMC chain?

I'm currently running some data through the MCMCglmm package in R. I was wandering what actually happens in the chain that is created? I have a multiresponse model (as a quick overview of the data: I'm trying to estimate variance and covariance within and between 6 traits - e.g. body size, these traits are measured on multiple individuals of 80 families, and there are some random effects such as experimental block).

I would like to know what is actually happening inside that chain making process - what is going on and how are the prior distributions and the data involved? It's obviously not just plucking numbers of thin air, it's using the prior and data in some way. What are the calculations or processes underlying this?

• Your question is too general. You ask how does a whole family of estimation methods "work", it is a very broad topic. – Tim Dec 1 '14 at 8:58
• I understand, so I posted below some resources where you can find answers on your question. First you should start with understanding how MCMC works in general, the implementation for some concrete application is another thing - and it gets complicated. So my recommendation is to check the books referenced - the one with examples in R gives many clear examples and a step-by-step tour on MCMC. – Tim Dec 1 '14 at 9:12
• I find it extremely odd that you can ask about the prior and the data after running the algorithm!!! An MCMC algorithm provides an approximation of a posterior distribution, but you should first try to make sense of this notion, instead of using a black box and try to guess at the concept behind... – Xi'an Dec 5 '14 at 21:43
• I know that it is the right tool for what I want to do, I only need to have an in depth knowledge of the inner workings if I'm trying to write the program from scratch. I find it odd that you feel the need to down vote because I'm trying to improve my knowledge of the subject @xi'an, dont be so naive - there's plenty of people happily using these tools as a black box so maybe you should try to help people understand rather than putting them down for trying to improve their knowledge! – rg255 Dec 6 '14 at 10:03

The "CourseNotes" vignette of the MCMCglmm package* explains:

MCMCglmm uses a combination of Gibbs sampling, slice sampling and Metropolis-Hastings updates

*(which file you already have as a result of downloading the package, and would hopefully have read before now, as with any vignettes of a package you rely on to do calculations. See ?vignettes)

Exactly how the sampler behaves is different in each case. These three approaches to sampling are detailed in the linked vignette.

I'll briefly outline a simple version of them; in practice things can be more complex.

All of these samplers move from place to place in the parameter space over time, each point depending on previous points - as a Markov Chain - in such a way that ultimately a sequence of generated parameter vectors converge converge to dependent samples from the desired joint posterior distribution. The different samplers can be mixed, so that some parameters are sampled by Gibbs or slice sampling and others by Metropolis-Hastings.

Gibbs sampling relies on sampling full conditional distributions, $[\theta_i|\theta_1, ...\theta_{i-1},\theta_{i+1},\ldots,\theta_p,\underline{y}]$, though there are variants that do things differently. This is convenient when full conditionals are easy to write down, evaluate and sample from.

Metropolis-Hastings is more general. The particular form used in MCMCglmm is known as Random-Walk Metropolis Hastings, and involves attempting to move from the most recent parameter value(s) by a random amount (leaving out details here) and at each step either moves to that new value or remains where it was with a certain probability that results in the right stationary distribution (in contrast to the Gibbs sampler, which always moves). Metropolis Hastings is relatively simple to program, and may be useful when Gibbs is difficult or may not perform well.

Slice sampling is different again. Consider being at some point in parameter space, where we are trying to sample one of the parameters $\theta_i$ from its univariate conditional density, $f$ which is on some bounded interval. We sample uniformly from $[0,f(\theta_i^\text{old})]$ (giving $u$ say), and then uniformly from the values of $\theta_i$ where $f(\theta_i)>u$. This results in sampling from the correct conditional.

Your question is very general. For information on what is MCMC I recommend you two great books by Robert and Casella, one with examples in R and the second, very detailed, handbook.

More information on MCMCglmm package you can find in its documentation and vignettes.

You can find examples of Metropolis-Hastings - one of the "classic" MCMC algoruthms - on CV, e.g. here, the same with Gibbs sampling. Those are the basic examples.