# How to generate predictions with rjags?

I've used rjags to run MCMC on a model, specified in the JAGS language. Is there a good way to extract that model and perform predictions with it (using the posterior distributions of my parameters)? I can re-specify the model in R and plug in the modes of my parameter posteriors; I'm just wondering if there's a less redundant way of doing this.

• Link is broken. Can you update it, please? – chl Oct 20 '12 at 10:48
• Done. Looks like sourceforge rearranged – Quantitative Historian Oct 22 '12 at 17:00
• Thanks! (I haven't found the thread myself since SF changed their relative paths.) – chl Oct 22 '12 at 17:39

Usually you can do the predictions in JAGS. Below is a regression example with FEV (something to do with lung capacity) as the dependent variable and age and smoking indicator as predictors.

FEV20s and FEV20ns are the predicted FEV values for a 20 year old smoker and a 20 year old non-smoker.

model
{
for(i in 1:n){
FEV[i] ~ dnorm(mu[i],tau)
mu[i] <- beta[1] + beta[2]*Age[i] + beta[3]*Smoke[i]  + beta[4]*Age[i]*Smoke[i]
}

#priors
beta[1] ~ dnorm(0,0.001)
beta[2] ~ dnorm(0,0.001)
beta[3] ~ dnorm(0,0.001)
beta[4] ~ dnorm(0,0.001)
tau ~ dgamma(0.001,0.001)
sigma<-1/sqrt(tau)

## Predict the FEV for a 20 year old smoker and for a 20 year old nonsmoker
mu20s <-  beta[1] + (beta[2]+beta[4])*20 + beta[3]
mu20ns <-  beta[1] + beta[2]*20
FEV20s ~ dnorm(mu20s,tau)
FEV20ns ~ dnorm(mu20ns,tau)
}


Example from: Bayesian Ideas and Data Analysis

• Thanks for the pointer--I hadn't thought about just sending my test data into JAGS, but that should do it. – Quantitative Historian Jun 6 '12 at 21:00
• Is there a way to generate these predictions without having the refit the entire model? If there were it would be easy enough to massively parallelize generating predictions, however, if the whole model needs to be refit, this is not possible. – colin Jun 30 '17 at 18:25