Stat 534: Fall 2015. Introduction to the BUGS language and rjags The file grizz1.r fits an exponential growth model with process and measurement error to the post1980 grizzly bear data. That model is defined in the exp.bug file. The steps are: 1. Write the desired model in the BUGS language and store it in a file. I use the .bug extension but .txt is fine. 2. In R, store information about the model, the data, and jags control variables in a jags object. This is done by the jags() function. 3. Iterate the Markov Chain quite a few times. Iterating the MC is done by update(). You want to repeat this step until the chain has converged to a stationary distribution. I usually use multiple chains (e.g. 3 or more) to check convergence. Convergence is checked in step 6. 4. Iterate the Markov Chain quite a few more times, saving samples of all desired variables. I use the coda.samples() function to do this. 5. My experience is that hierarchical models often don’t mix well, so there is a considerable autocorrelation in the chain. I sample a long chain that is heavily thinned (e.g. thin=10) to reduce the amount of data that is stored. 6. Check that the chain has converged using trace plots and/or the Gelman-Rubin statistic (or some other diagnostic). plot() draws trace plots, gelman.diag() computes the GR statistic for each variable and also overall. 7. If it hasn’t converged, use update() and coda.samples() again. 8. Extract means, medians, and other summary statistics from the samples. The BUGS language provides a way to write models that describe the relationships between what is observed and what is to be inferred. Some things to note: 1. Statements can be in any order. You are describing relationships, not the sequence of computations. 2. I usually put prior distributions in a separate block of code. 3. I usually put the observation model in a separate block of code. 4. Assignments are done by <-; random variables are defined by ~. 5. The arguments for definitions of random variables must be variables. No computations allowed. • y[i] ~ dpois(exp(b0 + b1*x[i])) is not allowed. 1 • You have to define the mean of the Poisson separately: my[i] <- exp(b0 + b1*x[i]); y[i] ~ dpois(my[i]) 6. All random variables must either be data or given prior distributions. 7. The parameterization of random variables in BUGS is not the same as in R. Some things that I need to be careful about: • Normal distributions are specified as (mean, precision), where precision = 1 / variance. That means a vague prior for a normal like N(0, 1000) is specified as dnorm(0, 0.001) • Gamma distributions are specified as (shape, rate), so the mean is shape/rate and the variance is shape/rate2 . That means a gamma with mean 1 and variance 1000 is dgamma(0.001, 0.001). 8. Variables for intermediate computations can not be reused. You must use a new variable for each intermediate computation. • In R: for (i in 1:n) { temp <- exp(l[i] + b[i]); p[i] <- dpois(temp)} is just fine because statements are sequentially executed • In BUGS: you need to define a separate variable for each value of i. A vector is fine: for (i in 1:n) { temp[i] <- exp(l[i] + b[i]); p[i] <- dpois(temp[i])} After fitting the exponential growth model and being (at least somewhat) comfortable with the output, feel free to modify the code (grizz1.r, exp.bug, or both) so that: • Observations conditional on the latent population size have a Poisson distribution • Observations conditional on the latent population size have an overdispersed Poisson distribution • The process model allows the population growth rate to vary smoothly over time, i.e., a local linear trend model. • The process model allows the population growth rate to depend on current population size, i.e., a Ricker density-dependence model or something like it. Questions to answer after fitting the model(s) with alternative observation models: 1. Does the choice of observation model influence posterior distribution of the population growth rate (b0 in my BUGS model)? 2. Does the choice of observation model influence the trajectory of mean population size? Questions to answer after fitting the model with a time-varying population growth rate: 1. How might you decide whether or not to include a time-varying growth rate? 2