Lab 8: Introduction To Winbugs: Goals
Lab 8: Introduction To Winbugs: Goals
Goals:
Next lab we will look at two case studies: (1) NMMAPS and (2) Hospital ranking data.
PART I
A. What is WinBUGS
BUGS = Bayesian Inference Using Gibbs Sampling
Not using Windows? Try
OpenBUGS: http://mathstat.helsinki.fi/openbugs
JAGS: http://www-fis.iarc.fr/~martyn/software/jags
(3) P ( | data):
This represents the posterior distribution of .
It describes all the information on after combining prior knowledge
on and what our data informed us about .
Since P( | data) is a probability distribution, statistical inference is
made by examining the different characteristics of this distribution.
E.g. the posterior mean, median or mode can be our estimate of .
E.g. the variance and middle 95% of the posterior distribution can tell
us about the uncertainty in our estimates.
Therefore, Bayesian data analyses typically involve the following ingredients:
(1) Specify a model that specifies the relation between the unknown parameters
and the observed data.
(2) Specify prior distributions for the unknown parameters.
(3) Obtain the posterior distributions.
(4) Make inference using the posterior distributions.
C. Why Do Bayesian Analysis
Here are some advantages of the Bayesian approach:
Here are some elements that make Bayesian analysis more complex:
Since we specify P( data | ) and P(), the only element that prevents us from
obtaining P( | data ) is the marginal distribution P (data).
However, P (data) is often difficult to evaluate, especially when the number of
parameters is large.
Note that P (data) does NOT depend on the parameter . Many methods have
been developed to draw samples from P( | data ) and WinBUGS does this for us
automatically!!!
Given samples from P( | data ), we can calculate the desired statistics such as the
mean or variance to make statistical inference. The precision of how our samples
resemble the true posterior distribution is only limited by the number of draws we
make.
PART II
WinBUGS in Action
xi ~ Normal ( 1 , 12 )
i = 1, 2, K , 20
yi ~ Normal ( 2 , 22 )
i = 1, 2, K , 20
1 ~ Normal ( 0, 100 2 )
2 ~ Normal ( 0, 100 2 )
The Normal distributions for s are flat and cover a large range of values.
We set the inverse of the variance to have a gamma prior distribution since
gamma distribution only takes positive values. Gamma (0.001,.001) has
extremely large standard deviation.
We pick the above prior distributions such that they are non-informative in that
the data will easily dominate the posterior distributions.
A typical WinBUGS program include three sections: Model, Data, and Initial Values.
Model: Translating our statistical model into a WinBUGS program:
model{
for (i in 1:20){
x[i] ~ dnorm (mu[1], prec[1])
y[i] ~ dnorm (mu[2], prec[2])
}
mu[1] ~
mu[2] ~
prec[1]
prec[2]
Model, P( | data )
Priors, P( )
4000
6000
8000
iteration
mu[2]
9.0
8.5
8.0
7.5
7.0
6.5
2000
4000
6000
8000
iteration
s2[1]
15.0
10.0
5.0
0.0
2000
4000
6000
8000
iteration
s2[2]
5.0
4.0
3.0
2.0
1.0
0.0
2000
4000
6000
8000
iteration
2.0
1.5
1.0
0.5
0.0
2.0
1.5
1.0
0.5
0.0
6.0
7.0
8.0
6.0
7.0
8.0
0.4
0.3
0.2
0.1
0.0
1.0
0.75
0.5
0.25
0.0
0.0
5.0
10.0
0.0
2.0
4.0
mean
5.051
7.957
sd
0.4654
0.2765
MC error 2.5%
0.01625 4.134
0.01001 7.429
median 97.5%
5.051
5.99
7.972
8.498
start
2000
2000
sample
800
800
node
s2[1]
s2[2]
mean
4.312
1.662
sd
1.605
0.5761
MC error 2.5%
0.04095 2.148
0.01988 0.8416
median 97.5%
3.944
8.394
1.549
3.122
start
2000
2000
sample
800
800
Parameter
Truth
1
2
2 1
2 2
5
8
4
1
Posterior
mean
5.05
7.96
4.32
1.66
95% Posterior
Interval
(4.13, 5.99)
(7.43, 8.50)
(2.15, 8.39)
(0.84, 3.12)
95% Confidence
Interval
(4.14, 5.96)
(7.38, 8.52)
(2.19, 8.08)
(0.85, 3.15)
MLE
5.05
7.95
3.79
1.48
The point estimates and 95% posterior interval for the 2 means are very similar to
the MLE estimates and its large sample 95% confidence interval.
The point estimates for the variances are a bit different. Why?
0.8
0.6
0.4
0.2
0.0
0.4
0.3
0.2
0.1
0.0
-6.0
node
mu.diff
mean
-2.909
node
mean
var.ratio 2.847
-4.0
-2.0
0.0
5.0
10.0
15.0
sd
0.5467
MC error
0.004712
2.5%
-3.965
median 97.5%
-2.913
-1.811
start
9201
sample
10800
sd
1.442
MC error
0.01527
2.5%
1.013
median 97.5%
2.555
6.47
start
10001
sample
10000
We conclude the difference between 1 and 2 is -2.91 with a 95% posterior interval of (3.96 ~ -1.81) and the ratio between 21 and 22 has a posterior median of 2.55 (95% PI:
1.01~6.47). Therefore we found evidence that 1 less then 2 and 21 is greater than 22.
7
Stata has a package that allows you to run WinBUGS in Stata (link below). It still
requires you to write WinBUGS program but you can analyze the posterior samples in
Stata. R has similar libraries (BRugs and R2WinBUGS) that call WinBUGS or
OpenBUGS. JAGS is another MCMC software that you can call from R and is not based
on BUGS.