Solved – How to generate predictions with rjags

jagsr

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.

I believe http://sourceforge.net/p/mcmc-jags/discussion/610037/thread/0ecab41c is asking the same question.

Best Answer

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

Related Question