### R2WinBUGS: Using R for Bayesian Analysis

```R2WinBUGS:
Using R for Bayesian Analysis
Matthew Russell
Rongxia Li
2 November 2010
2010 Northeastern Mensurationists Meeting
Bayesian ideologies
8
(aka what UMaine students needed to learn to
pass Bill Halteman’s MAT500 course)
• P(H|Y), not P(Y|H)
• Probability is the likelihood of an event occurring
• Prior knowledge can be incorporated
• Model parameters are random variables
Benefits
• Posterior distributions generated for model
parameters
• Statistics can be computed (e.g. mean, median, mode)
• A formal distribution does not need to be
assumed
• Uncertainty of model parameters can be
directly assessed
• Models easily updated with new data
• Consider old model as a prior
bayes* AND [forestry OR silvic*]
Web of Science results:
10
Number of journal articles
9
8
7
6
5
4
3
2
1
0
Year
WinBUGS
• Software for Bayesian analysis using
Markov chain Monte Carlo methods
• Standard GUI
• http://www.mrc-bsu.cam.ac.uk/bugs/
• OpenBUGS
– Open source version of BUGS
– Future development will be with OpenBUGS
– www.openbugs.info
R packages
• R2WinBUGS
– Writes data and scripts in R and calls WinBUGS
• Useful for:
– running multiple datasets
– changing model specifications
– Results can be provided in R
• BRugs
– Collection of functions that allow graphical
analysis
Example: Penobscot Experimental Forest snag data
n=1,009
Decay class 4
Decay class 3
Decay class 2
Decay class 1
1.2
1
P(survival)
0.8
0.6
0.4
0.2
0
0
2
4
6
8
10
12
Time since death (yrs)
14
16
18
20
Snag survival
• Probability of snag survival a function of time
since tree death (Garber et al. 2005)
• Seven species examined
– BF, RS, EH, WS, WC, PB, RM
1
P(surv) 
b1  b2(t ) b 3
.bug file
model{
for(i in 1:3) {b[i]~dnorm(0,1.0E-6)}
prec~dgamma(0.001,0.001)
Specify priors
for(i in 1:numTrees) {
preds[i]<-1/(b[1]+b[2]*pow((t[i]),b[3]))
Loop through trees
Psurv[i]~dnorm(preds[i],prec)
}
}
samplesHistory (‘*’)
samplesDensity(‘*’)
samplesStats (‘*’)
Assessing results from BUGS
(from McCarthy 2007)
• samplesHistory
– If it is not white noise, it might be autocorrelation
• Are samplesDensity truncated?
• Bumpy samplesDensity?
– Consider more samples
• Specify different initial values… do you get the
same results?
– Consider Gelman-Rubin statistic
• Measures the influence of specifying initial values
Other OS and other programs
• WinBUGS can be run on Linux/Unix/Mac
through Wine
– But JAGS (Just Another Gibbs Sampler) might
work better for Linux users
• http://www-fis.airc.fr/~martyn/software/jags/
• Macros available for SAS/Excel
• Matlab-WinBUGS GUI available
Sources
http://www.mrc-bsu.cam.ac.uk/bugs/
WinBUGS
R2WinBUGS
OpenBUGS
BRugs
JAGS
McCarthy, M.A. 2007. Bayesian methods for ecology. Cambridge Univ. Press.
296 pp.
Gelman et al. 2004. Bayesian data analysis. Chapman Hall/CRC. 696 pp.
Albert, J. 2009. Bayesian computation with R. Springer. 300 pp.
Summary
• Plenty of tools available for Bayesian analysis
with R
– All are open source
• Coding is not terribly complex
• Lots of measures for assessing results
– Graphical
– Empirical
• R2WinBUGS as a learning tool for Bayesian
methods
```