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.

  • $\begingroup$ Link is broken. Can you update it, please? $\endgroup$ – chl Oct 20 '12 at 10:48
  • $\begingroup$ Done. Looks like sourceforge rearranged $\endgroup$ – Quantitative Historian Oct 22 '12 at 17:00
  • $\begingroup$ Thanks! (I haven't found the thread myself since SF changed their relative paths.) $\endgroup$ – 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.

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]

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)

## 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

  • $\begingroup$ Thanks for the pointer--I hadn't thought about just sending my test data into JAGS, but that should do it. $\endgroup$ – Quantitative Historian Jun 6 '12 at 21:00
  • 1
    $\begingroup$ 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. $\endgroup$ – colin Jun 30 '17 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.