13
$\begingroup$

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.

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

1 Answer 1

8
$\begingroup$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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