Bayesian applications

Stats Week 5 lecture 2
lecture_notes_week5_2.pdf

In R

We are using the following packages for Bayesian inference

RStan

RStan is an implementation of the Stan software used for Bayesian Analysis into R

Stan gives tools which builds (compiles) a C++ program into your computer each time you run it. Unlike with most regression packages which just load the functions and let you run it. But with Stan the program needs to be built every time you generate a new model. This usually takes a couple minutes so be patient.

BRMS

BRMS implements "Bayesian Regression Models using Stan" - used for our regression analysis.

When you call a brms function, it starts by building the Stan program, then implements an algorithm to do the Markov chain Monte Carlo sampling.

Example

After first loading rstan and brms, we implement a Bayesian regression model with brm() on some data. After compiling the Stan program, it begins the Markov chain Monte Carlo sampling to get a posterior distribution sample for each model.

Sampling process:

1. Warmup

Metaphor for warmup process

Think of it like a salad bowl placed upside down. The north-south and east-west directions represent your parameters. While looking for the optimal value, the algorithm is trying to climb to the top of the salad bowl. It moves a random direction, but it can tell if it got higher after each random movement. Since it can remember that it got higher, it will try to continue like this until it reaches the top.

Then he explained something about turning the bowl upside down and putting a marble on the bowl so it rolls around the edge and in to the middle. Every once and a while we give it a poke in a random direction, to make sure it continues going down. Once it reaches the bottom of the bowl, that's when it starts getting into the sampling stage. Once it's in this stage, it can roll around and the number of times it rolls over the same point is the Bayesian probability value of that point.

2. Sampling

This is the process of one "chain" in the Markov Chain. By default, it samples 4 chains to make sure they converge to the same place. This is a check to make sure none of the chain is falling into some local mean (there are also other complex processes to prevent this).

summary(brm)

We will compare the outputs of this summary to the linear model summary values in R:

Start

Draws

Inference results

Coefficients

Sigma

Prior summary

Using the prior_summary() function we can find the prior distribution for each of our parameters.

We don't need to worry too much about this - but it tells us what the default priors used in our function is, and you might want to tweak these if you know what you're doing (not required for this course)

Tweaking the model

Some things we might want to tweak if we are running it again:

Assessing the model

Assessing the posterior samples

We need to determine if the Markov chain Monte Carlo (MCMC) algorithm has converged well. We need to:

Visual inspection

Assessing the model fit

Applying to Poisson observation model