WinBUGS UBC Summer School Outline Goals and Scope Preliminaries Lung cancer data Gibbs sampling The WinBUGS Software Graphical models WinBUGS syntax Data Navigating WinBUGS Specification Tool Update & Sample Monitor Tools Convergence and analysis Additional Topics Errors and traps Miscellaneous notes .1 An Introduction to WinBUGS * PIMS Collaborative Research Group * Summer School on Bayesian Modeling and Computation 14 th July, 2008 Sebastien Haneuse, PhD Group Health Center for Health Studies Department of Biostatistics, University of Washington
111
Embed
An Introduction to WinBUGS · Errors and traps Miscellaneous notes.3 Goals and Scope Bayesian analyses • Formulate the scientific question ∗ understanding the association between
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.1
An Introduction to WinBUGS∗ PIMS Collaborative Research Group∗ Summer School on Bayesian Modeling and Computation
14th July, 2008
Sebastien Haneuse, PhDGroup Health Center for Health Studies
Department of Biostatistics, University of Washington
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.2
Outline
• Goals and Scope
• Preliminaries
• The WinBUGS Software
• Navigating the WinBUGS Menu System
• Additional Topics
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.3
Goals and Scope
Bayesian analyses
• Formulate the scientific question
∗ understanding the association between two variables∗ prediction of some future event
• Identify a corresponding parameterization
∗ ‘translation’ of the scientific question into statistical termsand quantities
• Specify prior distributions for the unknown parameters
∗ translation of substantive knowledge∗ identification of ‘non-informative’ priors∗ establish several options towards a sensitivity analyses
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.4
Goals and Scope
Bayesian analyses (cont)
• Characterize linkage between the parameters and the observeddata
∗ specification of the likelihood
• Apply Bayes Theorem
∗ posterior ∝ prior × likelihood∗ turn the Bayesian ‘handle’∗ examine features of the posterior
• Interpret the results and refine the scientific question
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.5
Goals and Scope
Where does WinBUGS fit in?
• We’ve seen that there are many steps in a Bayesian analysis
• WinBUGS facilitates turning the Bayesian handle
∗ computational tool∗ via Markov Chain Monte Carlo (MCMC)
• Cuts out much of the hard work for a very general class ofmodels
∗ model: likelihood/prior combinations
• Sole purpose is to implement MCMC-based analyses
∗ perform tasks with a few lines of code that would take(potentially) hundreds of lines of ‘home-grown’ code
∗ highly efficient, both in terms of model specification but alsoin the MCMC implementation
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.6
Goals and Scope
Caveats
• Underlying WinBUGS is a language
∗ even within a single language, there are often many ways of‘saying’ the same thing!
∗ many idiosycracies
• Cannot accommodate all analyses
∗ OpenBUGS is an open source version
• WinBUGS User Manual:
Beware: MCMC sampling can be dangerous!
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.7
Goals and Scope
Scope
• A good way to learn, and implement your own analyses withinWinBUGS, is to build on work by others
∗ hundreds of examples out there∗ WinBUGS manual and website∗ various books
• A reasonable strategy is to adapt existing code to your specificsetting
• The main purpose here is to provide an overview of WinBUGSfunctionality and syntax
∗ understanding models and adapting them to your ownpurposes
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.8
Goals and Scope
Scope (cont)
• Aimed at a new WinBUGS user
∗ assume some familiarity with the Bayesian philosophy andtheory
∗ underlying premise of using draws from a distribution as ameans to approximate its features
• Not a tutorial on the intricacies of Bayesian reasoning or MCMC
∗ model and prior specification∗ convergence, optimal sampling strategies, etc∗ plenty of good texts for this
• An introductory WinBUGS book is on the way
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.9
Outline
• Goals and Scope
• Preliminaries
∗ Lung cancer data from Ohio∗ Gibbs sampling algorithm
• The WinBUGS Software
• Navigating the WinBUGS Menu System
• Additional Topics
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.10
Preliminaries
Lung cancer data from Ohio
• Population estimates and lung cancer death counts in 1988
∗ pooled over the selected chains∗ ‘MC error’ is the Monte Carlo standard error of the mean
• ‘density’ returns a smoothed kernel density estimate forcontinuous nodes and a histogram for discrete nodes
∗ not particularly good looking though∗ may want a little more graphical sophistication for journal
articles or presentations
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.94
Navigating WinBUGS
CODA
• Move beyond the diagnostic and graphical options of WinBUGS
• Press to ‘coda’ dump out an ascii representation of the samples
∗ coda: Convergence Diagnostics and Output Analysis
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.95
Navigating WinBUGS
CODA
• Generates separate files for each chain, as well as a fileindicating the indices for each node
∗ save each as a ‘.txt’ file from the File Menu
• Exploit software specifically written to assess convergence andgenerate interesting figures
∗ e.g., coda, an S-Plus and R package written by MartynPlummer and colleagues
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.96
Outline
• Goals and Scope
• Preliminaries
• The WinBUGS Software
• Navigating the WinBUGS Menu System
• Additional Topics
∗ Errors and traps∗ Miscellaneous notes
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.97
Errors and traps
Error messages
• If an error is encountered, a message is displayed at the bottomof the WinBUGS window
∗ if it can, WinBUGS will place the cursor somewhere afterthe offending location
∗ change the preferences to make the cursor more visible∗ also recorded in a log which can be accessed via the Info
Menu
• WinBUGS error and trap messages are fairly notorious
∗ some are self-explanatory∗ many are not
• Worth taking into context the breadth of models that WinBUGScan accomodate
∗ given the flexibility of WinBUGS, it’s challenging thinking upall the ways in which an error can be generated
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.98
Errors and traps
Error messages (cont)
• Errors can be the result of improper syntax
∗ Use of an ‘=’ sign
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.99
Errors and traps
Error messages (cont)
• Others become evident at compilation
• For example, consider attempting to model observed data as asum of two random variables
∗ syntactically OK
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.100
Errors and traps
Error messages (cont)
• Error at compilation
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.101
Errors and traps
Error messages (cont)
• Other, similar errors include
∗ ‘undefined variable’; variable is in the data but not definedin the model
∗ note that WinBUGS is case-sensitive∗ ‘index out of range’
• Limitations and restrictions
∗ ‘logical expression too complex’; too manyparameters/constants or too many operators
∗ linear predictors can take advantage of the inprod(,)function
• WinBUGS manual has greater detail
• Debugging can often take longer than actually performing theMCMC.
∗ phenomenon not restricted to WinBUGS though!
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.102
Errors and traps
Trap messages
• Traps that occur during the MCMC process can be particularlydifficult to diagnose
∗ ‘undefined real result’ indicates numerical overflow∗ can happen if all the initial values are equal
• Can sometimes restart the simulation by pressing the ‘update’button twice.
• If this doesn’t work, possible solutions include
∗ better initial values∗ more informative priors∗ re-parameterization to improve orthogonality∗ standardization of covariates to have mean 0 and standard
deviation 1
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.103
Miscellaneous notes
Specifying new distributions
• In some instances one might need to specify a distribution notyet implemented
∗ e.g., a truncated distribution
• ‘ones’ trick for specifying a new sampling distribution
C <- 10000 # ensure all p[i]’s < 1for(i in 1:N){ones ~ dbern(pi[i])p[i] <- L[i] / Cones[i] <- 1
}
∗ L[i] is the likelihood contribution for the new samplingdistribution
∗ function of observed data and parameters of interest
• WinBUGS manual also has tricks for specifying a new priordistribution as well as a discrete prior on a set of values
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.104
Miscellaneous notes
Compound documents
• WinBUGS allows documents to be embedded in a compunddocument
• Convenient way to
∗ save a doodle, the code and data∗ store output, including posterior summaries and graphics
• All in a single document
∗ helpful for keeping a track of analyses
• The use of ‘folds’ is helpful in making the document concise
∗ ‘hide’ sections of the document (including graphics), to berevealed when needed.
∗ WinBUGS manual has documentation
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.105
Gibbs sampling algorithm
Choice of sampling method
• The Gibbs sampling algorithm is the underlying framework forMCMC within WinBUGS
∗ cycling through a series of full conditional posteriordistributions
• When WinBUGS can identify a full conditional as being a knowndistribution, it will sample directly
∗ modules implemented for a range of distributions
• When the full conditional is not of a known form, WinBUGSemploys a Metropolis step
∗ Metropolis-within-Gibbs
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.106
Gibbs sampling algorithm
Choice of sampling method (cont)
• Towards this, WinBUGS has a variety of MCMC methods at itsdisposal
∗ vary according to characteristics of the parameter, and its’posterior distribution
• WinBUGS tries increasingly complex methods, until one works
∗ if it is unable to identify a method, an error message isreturned
∗ introduction to the WinBUGS manual has more detail
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.108
Miscellaneous notes
Software resources
• Add-ons specifically written for WinBUGS
∗ GeoBUGS for spatial analyses∗ PKBUGS for pharmacokinetic analyses
• Packages written for other software programs
∗ facilitate exporting data into a WinBUGS format∗ convergence and analysis (e.g., coda, boa)∗ setting up simulations/batches
• WinBUGS website
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.109
Miscellaneous notes
Mac OS X
• It is possible to directly run WinBUGS on a Mac
∗ OS X 10.4 or higher∗ Intel-based architecture
• Requires installation of
∗ X11 windowing system∗ Darwine; a pre-compiled version is available at
http://www.kronenberg.org/darwine/
• Download the zip archive installation of WinBUGS
• More detail is available at
http://idiom.ucsd.edu/∼rlevy/winbugsonmacosx.pdf
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.110
Miscellaneous notes
Mac OS X screenshot
WinBUGS
UBC Summer School
Outline
Goals and Scope
PreliminariesLung cancer data
Gibbs sampling
The WinBUGSSoftwareGraphical models
WinBUGS syntax
Data
Navigating WinBUGSSpecification Tool
Update & Sample MonitorTools
Convergence and analysis
Additional TopicsErrors and traps
Miscellaneous notes
.111
Miscellaneous notes
Additional references and resources
• Good introductory papers
∗ Scollnik, D.P. Actuarial modeling with MCMC and BUGS.North American Actuarial Journal, Vol 5, No 2, (2001),pp.96-125.
∗ Fryback, D.G., Stout, N.K., and Rosenberg, M.A. Anelementary introduction to Bayesian computing usingWinBUGS. International Journal of Technology Assessmentin Health Care, Vol 17, No 1 (Winter 2001), pp. 98-113.
• WinBUGS manual is very comprehensive and worth reading
∗ doesn’t provide guidance on Bayesian analyses
• WinBUGS website has links to a huge number of resources
∗ additional worked examples∗ slides for other short courses/presentations/classes∗ software