STAN: A tool for Bayesian inference and MCMC

This a short post just to bring to your attention STAN, a tool for Bayesian statistical inference. It’s been around for a few years now but like many of these things it can be slow to percolate down to the end users. Like many people who use MCMC and do Bayesian statistical inference I was a user of BUGS, either OpenBUGS or WinBUGS, which is a great piece of kit. But it could be slow, it throws up indecipherable error messages, and sometimes struggled with convergence. Then I discovered STAN, which is named after Stanislaw Ulam the inventor of the Monte Carol method. It’s generally faster than BUGS, especially if you use vectors rather than loops; the error messages make it easy to debug code; and, it’s easy to install and use with R. The code is also simple to pick up – the website has equivalent STAN code for many BUGS models and there is a detailed manual. There’s a simple example at the bottom of the page for a random effects meta-analysis.

STAN is not better than BUGS is every respect. If BUGS works it works, why change? But for complex multilevel models, handling large amounts of data, and more flexibility with prior distributions it’s definitely worth making the change. Hopefully, I’ll post some more health economic examples in the future.

Random Effects Meta-analysis model example

BUGS
I’ve omitted the data, params, and inits part as BUGS users should be familiar with that.

model{ 
   for(i in 1:N) { 
      y[i] ~ dnorm(theta[i], prec2[i]) 
      theta[i] ~ dnorm (mu, prec.tau2) 
      prec2[i] <-(1/se[i])*(1/se[i]) 
   }
 
   prec.tau2 ~dgamma(0.001,0.001) 
   theta ~ dnorm(0.0,1.0E-6) 
   tau <- sqrt(1 / prec.tau2) 
}

STAN
When calling STAN from R, you put the data in a list and then pass it to the stan() function along with the code.

data {
 int N; //number of studies
 vector[N] y; //estimated treatment effects
 vector<lower=0>[N] se; //s.e. of treatment effects
}
parameters {
 vector[N] theta; 
 real mu; //pooled mean
 real<lower=0> tau; //between study variance
}
model {
 y ~ normal(theta, sigma);
 theta ~ normal(mu, tau);
 mu ~ normal(0, 1000);
}
Advertisements

One thought on “STAN: A tool for Bayesian inference and MCMC

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s