WinBUGS

advertisement
“WinBUGS”
by Haitao Chu
Presented as part of the Statistical Software Interest
Group’s seminar series. Visit
www.sph.emory.edu/bios/ssig/ for more information.
April 15, 2003
Outline
•
•
•
•
•
What is BUGS/WinBUGS?
Why do we want to use WinBUGS?
Where do we get WinBUGS?
How do we install WinBUGS?
How do we use WinBUGS?
– Example
• What kind of output can WinBUGS give us?
• What kind of sampling method does WinBUGS
implement?
• Summary
What is BUGS?
What is BUGS?
Bayesian inference Using Gibbs Sampling
• is a piece of computer software for the Bayesian
analysis of complex statistical models using
Markov chain Monte Carlo (MCMC) methods.
• It grew from a statistical research project at the
MRC BIOSTATISTICAL UNIT in Cambridge, but
now is developed jointly with the Imperial College
School of Medicine at St Mary’s, London.
What is BUGS?
Classic BUGS
BUGS
WinBUGS (Windows Version)
GeoBUGS (spatial models)
PKBUGS (pharmokinetic modelling)
• The Classic BUGS program uses text-based
model description and a command-line
interface, and versions are available for major
computer platforms (Sparc, Dos). However, it is
not being further developed.
What is WinBUGS?
• WinBUGS, a windows program with an option of a
graphical user interface, the standard ‘point-and-click’
windows interface, and on-line monitoring and
convergence diagnostics. It also supports Batch-mode
running (version 1.4).
• GeoBUGS, an add-on to WinBUGS that fits spatial
models and produces a range of maps as output.
• PKBUGS, an efficient and user-friendly interface for
specifying complex population pharmacokinetic and
pharmacodynamic (PK/PD) models within WinBUGS
software.
Why do we want to use WinBUGS?
• Its ability to fit complex statistical models using
MCMC methods.
• Its flexibility to program, two different ways to
specify model
 DoodleBUGS: Direct graphics
 BUGS language
• Free to download
• A lot of examples in WinBUGS
• A lot of online resources
http://www.mrc-bsu.cam.ac.uk/bugs/weblinks/webresource.shtml
Where do we get WinBUGS
• Free download
http://www.mrc-bsu.cam.ac.uk/bugs.
• Package needs to be downloaded
– WinBUGS14.exe
• Potential useful packages for convergence
diagnostics
– CODA (Convergence Diagnostic and Output
Analysis) for S+ or R
– BOA (Bayesian Output Analysis) for S+ or R
How do we install WinBUGS?
•
•
•
•
•
•
•
•
Downloading the file WinBUGS14.exe (also available in
S:/Haitao Chu/WinBUGs)
Exit all other programs currently running (particularly if
using Windows XP)
Go into Explore and double click on WinBUGS14.exe
Follow the instructions in the dialog box
You should have a new directory called WinBUGS14 within
Program Files
Inside the WinBUGS14 directory is a program called
WinBUGS14.exe
Drag the pretty icon to your desktop to create a shortcut.
Double click on WinBUGS14.exe to run WinBUGS14.
How do we install WinBUGS?
• Obtaining the key for unrestricted use by
registration at
http://www.mrc-bsu.cam.ac.uk/bugs/winbugs/register.shtml
• Following the instructions from automatic response
of your registration to remove the restrictions in
WinBUGS 1.4
• check that the date of Keys.ocf file in
..\WinBUGS14\Bugs\Code\ has been updated.
How do we use WinBUGS?
Educational Example:
Pumps: conjugate Gamma-Poisson hierarchical model
• The pumps data
• Gamma-Poisson hierarchical model
• Model specification in WinBUGS
–
–
•
•
DoodleBUGS, Direct graphics
BUGS language
Format data and specify initial values
Tutorial
The pumps data
•
•
PUMP
ti
xi
1
94.5
5
2
15.7
1
3
62.9
5
4
126
14
5
5.24
3
6
31.4
19
7
1.05
1
8
1.05
1
9
2.1
4
10
10.5
22
ti: the length of operation time of the pump (in 1000s of hours).
xi: the number of failures
Gamma-Poisson hierarchical model
for i  1, 2, ..., 10
xi ~ Poisson(tii )
i ~ Gamma ( ,  )
 ~ Exponential (1.0)
 ~ Gamma(0.1, 1.0)
Model specification through DoodleBUGS
• Nodes
– Constants, denoted by rectangles
– Stochastic nodes, denoted by ellipses
– Deterministic nodes, logical function of other nodes
• Edges
– Directed links
• Solid arrow: stochastic dependence
• Hollow arrow: logical function
– Undirected links, dashed line
• representing an upper or lower bound
• Plates, repeated parts of the graph
Read the User Manual & Doodle help at the Help menu…
Model specification through DoodleBUGS
Stochastic node
Stochastic
dependence
alpha
beta
Constant node
logical
function
theta[i]
t[i]
Deterministic node
Plate
lambda[i]
x[i]
for(i IN 1 : N)
Model specification through BUGS language
model
{
for (i in 1 : N) {
theta[i] ~ dgamma(alpha, beta)
lambda[i] <- theta[i] * t[i]
x[i] ~ dpois(lambda[i])
}
alpha ~ dexp(1)
beta ~ dgamma(0.1, 1.0)
}
Format data and specify initial values
• Data format
– S-Plus format
list(t = c(94.3, 15.7, 62.9, 126, 5.24, 31.4, 1.05, 1.05, 2.1, 10.5), x = c(5,
1, 5, 14, 3, 19, 1, 1, 4, 22), N = 10)
– Rectangular format
Please see formatting of data at model specification at User
Manual
• Initial values
list(alpha = 1, beta = 1)
list(alpha = 10, beta = 10)
Tutorial: Interactive submission
• Standard windows “point and click” method
• Example
Pumps — conjugate Gamma-Poisson hierarchical
model in Example Volume I at Help menu
S:/Haitao Chu/WinBUGS/pumps/pumps.odc
Tutorial: Batch mode submission
•
Files required
–
–
–
–
The script itself, S:/Haitao Chu/WinBUGS/pumps/pumpsbatch.odc
Model file, S:/Haitao Chu/WinBUGS/pumps/pumpsmodel.txt
Data file, S:/Haitao Chu/WinBUGS/pumps/pumpsdata.txt
Initial values file
•
•
•
S:/Haitao Chu/WinBUGS/pumps/pumpsin1.txt
S:/Haitao Chu/WinBUGS/pumps/pumpsin2.txt
Format of the files
– native WinBUGS format with .odc extension
– text format with .txt extension
Tutorial: running WinBUGS from R
and other programs
• From R
– http://www.stat.columbia.edu/~gelman/bugsR
– S:/Haitao Chu/WinBUGS/bugsR
• From other programs
– http://www.mrcbsu.cam.ac.uk/bugs/winbugs/remote14.shtml
What kind of output
can WinBUGS give us?
Samples at Inference Menu
 trace: plots the variable value against iteration number.
 history: plots out a complete trace for the variable.
 density: smoothed kernel density estimate for
continuous variable or a histogram for discrete variable.
 auto cor: auto correlation, up to lag-50
 stats: Summary statistics, pooling over the chains
selected.
 coda: output the monitored values to CODA or BOA
What kind of output
can WinBUGS give us?
Samples at Inference Menu
• quantiles: plots out the running mean with running 95%
confidence intervals against iteration number.
• bgr diag: Brooks-Gelman-Rubin convergence statistic
Brooks and Gelman (1998)
 Green: the width of the central 80% interval of the pooled runs
 Blue: the average width of the 80% intervals within the
individual runs
 Red: their ratio R (= pooled / within).
 Convergence: R to 1, and with convergence of both the pooled
and within interval widths to stability.
What kind of output
can WinBUGS give us?
Compare at Inference Menu
Box Plot
Caterpillar Plot
box plot: theta
caterpillar plot: theta
4.0
[1]
[9]
3.0
[2]
[3]
[10]
[7]
[8]
[4]
[5]
2.0
[6]
[5]
[7]
[6]
1.0
[9]
[2]
[1]
[8]
[3]
[4]
[10]
0.0
0.0
1.0
2.0
3.0
4.0
What kind of output
can WinBUGS give us?
Rank at Inference Menu
• stats: the distribution of the ranks of each component
of the variable.
• histogram: the empirical distribution of the
simulated rank for each component
DIC at Inference Menu
Deviance Information Criterion
What kind of sampling method
does WinBUGS implement?
Sampling methods are used in the following hierarchies
Continuous target distribution
Conjugate
Direct sampling using standard algorithms
Log-concave Derivative-free adaptive rejection sampling
Non-log-concave (restricted range)
Slice sampling
Non-log-concave (unrestricted range)
Metropolis
Discrete target distribution (I have no experience)
Finite upper bound Inversion
Shifted Poisson
Direct sampling using standard algorithm
Summary
•
•
•
•
•
•
•
•
What is BUGS/WinBUGS?
Why do we want to use WinBUGS?
Where do we get WinBUGS?
How do we install WinBUGS?
How do we use WinBUGS?
What kind of output can WinBUGS give us?
What kind of sampling method does WinBUGS
implement?
MCMC can be dangerous
The End
Thank you very
much for attending
and participating in
this Statistical
Software Interest
Group seminar…
More volunteers!
Download