Skip to content

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);
}

By

  • Health economics, statistics, and health services research at the University of Warwick. Also like rock climbing and making noise on the guitar.

close

We now have a newsletter!

Sign up to receive updates about the blog and the wider health economics world.

0 0 votes
Article Rating
Subscribe
Notify of
guest

2 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Mario
Mario
1 year ago

Small correction: “Stan” is not an acronym, so it should not be all in uppercase. 🙂

trackback
5 years ago

[…] Monte Carlo sampler that adaptively tunes its step parameters. We have mentioned Stan before on this blog. I think it’s great and have been adapting most of my models to it. The documentation, on the […]

2
0
Would love your thoughts, please comment.x
()
x
%d bloggers like this: