Bursts modelling Using WinBUGS
Tim Watson
 May 2012
ICS_logo_white_blue type.png
:diagnostics/
:transformation/
:investment planning/
:portfolio optimisation/
:investment economics/
:regulation and economics/
:training/
Page : 2
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
Bayesian analysis software Using Gibbs Sampling for Windows.
DoodleBugs – a graphical directed acyclic graph (DAG)
Many add-ons, utilities, and variations of the package are alsoavailable (e.g. GeoBUGS-for spatial modelling, and PKBUGS-forpharmacokinetic)
Can be run from other software packages, e.g. R.
library(R2WinBUGS)
Connecting to data
Business as usual
What is WinBUGS?
Page : 3
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
Exploratory Data Analysis
Linear Model
Log-LinearModel
Maybe somenon-linearity?
Page : 4
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
Define the model
Load the data
Specify the number of MCMC chains
Compile the model
Load values to initialise the algorithm
Update the MCMC
Sample parameters of interest
Running a statistical model in WinBUGS
Page : 5
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
Note: WinBUGS uses precision instead ofvariance to specify a normal distribution!
Model 1Linear Regression in WinBUGS
model {
#specify likelihood
   for( i in 1 : N ) {
      Bursts[i] ~ dnorm(mu[i],tau)
   }
   for( i in 1 : N ) {
      mu[i] <- alpha + beta * frost[i]
   }
#specify priors
   alpha ~ dnorm( 0.0,1.0E-6)
   beta ~ dnorm( 0.0,1.0E-6)
   tau ~ dgamma(0.001,0.001)
   sigma <- sqrt(1 / tau)
}
Page : 6
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
Results
R output
Estimate Std. Error t value Pr(>|t|)
(Intercept)        460.460     26.686  17.255  < 2e-16 ***
Nr.Air.Frost.Days   25.616      3.504   7.312 2.19e-09 ***
Page : 7
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
Model 2Poisson Regression in WinBUGS
 model {
for( i in 1 : N ) {
      Bursts[i] ~ dpois(mu[i])
   }
 for( i in 1 : N ) {
  log(mu[i]) <- alpha + beta * frost[i]
   }
   alpha ~ dnorm( 0.0,1.0E-6)
  beta ~ dnorm( 0.0,1.0E-6)
}
R output
Estimate Std. Error z value Pr(>|z|)
(Intercept)       6.155015   0.007951  774.10   <2e-16 ***
Nr.Air.Frost.Days 0.038524   0.000861   44.74   <2e-16 ***
Page : 8
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
Real word data (missing data) - Model 2b
Missing data are easily handled
Missing observations are treated asunknown parameters
Missing values are denoted by “NA”
Missing responses are no problem;they are simply estimated
A missing explanatory variablemust either be replaced, or elsegiven some prior distribution
Elicitation
model;
{
   for( i in 1 : N ) {
      Bursts[i] ~ dpois(mu[i])
   }
   for( i in 1 : N ) {
      log(mu[i]) <- alpha + beta * frost[i]
   }
   alpha ~ dnorm( 0.0,1.0E-6)
   beta ~ dnorm( 0.0,1.0E-6)
   frost[1]~dunif(0,20)
   frost[2]~dunif(0,20)
   frost[4]~dunif(0,20)
}
list(Bursts = c(581, 504, NA, 403, 395, 484, 531,
338, 599, 611, 396, 1038, 990, 887, 722, 446, 510,
666, NA, 453, 300, 435, 532, NA, 1433, 626, 792,
499, 339, 247, 646, 367, 494, 372, 752, 924, 781, 599,
482, 438, 570, 533, 494, 715, 555, 370, 614, 524, 501,
851, 472), frost = c(NA, NA, 5.7, NA, 0.1, 0.1, 0, 0, 0, 2,
3.8, 15.3, 14.8, 11, 6.3, 2.5, 0.1, 0, 0, 0, 0, 0.9, 0.5, 13.2,
18.9, 15.4, 12.3, 5.1, 2.3, 0, 0, 0, 0.2, 2.7, 9.4, 23.1, 15.2,
3.8, 9.9, 0.3, 0.2, 0.2, 0, 0, 0, 0.4, 1.1, 4.1, 7.8, 11.1, 2.8
),N=51)
Page : 9
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
Poisson Regression with some Missing observations inWinBUGS
Frost missing
Bursts missing
frost  =c(4.1,10.1,5.8)
burst=c(504,664,1433)
Page : 10
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
Month effect - Model 4
Page : 11
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
BUGS language quite straightforward to learn.
Doodle Bugs even easier.
Handles missing data easily.
Very fast especially when the initialising values are closer to thestationary distribution.
Can fit a wide variety of statistical models.
Convenient in running multiple MCMC chains.
Automated in R and other tools (including Excel)
library(R2WinBUGS)
library(Rexcel)
Why WinBUGS?
Page : 12
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
Need to understand what you are doing
Saving the data in the appropriate format can be very challengingespecially large data set.
Algorithm can easily crash especially when priors are not wellspecified or due to unrealistic initialising values.
Algorithm can be very slow especially when dealing withhierarchical model.
Needs to know the model to be fitted as every distribution have tobe defined.
Negatives about WinBUGS
Page : 13
© ICS Consulting Ltd 2012
ICS_logo_white_blue type.png
Limitation in plotting capability.
Learning BUGS syntax  (though very similar to other packages suchas R, S-PLUS).
Using extreme priors especially non-informative priors can causethe algorithm to crash.
Challenges with WinBUGS