6

I have created a bayesian model that estimates 6 parameters using rjags from R. Now i want to do some predictions based on new data in R. Can anyone help me with an example.

model {
   B.m[1] <- Kb # mean B[1]
   B[1] ~ dnorm( B.m[1], tau_B)
   for(t in 2 : 10) { # Process model
      B.m[t]<- max( B[t-1] + rB[t-1](1 -B[t-1]/K) - C[t-1] , 0.0)
      B[t] ~ dnorm(B.m[t], tau_B) } # Observation mode
      for (t in 1: 10) {
         LogC_mean[t] <- log( max(qsegB[t]Eff[t], 0.000001))
         C[t] ~ dlnorm(LogC_mean[t], tau_c) } # Prior
         b~dbeta(1, 1)
         cv <- 1
         K.mean <- 500000
         K.v <- pow(cvK.mean, 2)
         K.tau <- 1/K.v
         K ~ dnorm(K.mean, K.tau)
         r ~ dlnorm(0.0, 1.4)
         qseg ~ dlnorm(-15.4, 1.44)
         tau_c ~ dgamma(0.001,0.001)
         tau_B ~ dgamma(0.01,0.01)
       }
    }
Tim
  • 108,699
  • 20
  • 212
  • 390

1 Answers1

5

Generally, in Bayesian model you do predictions on new data the same way as you do with non-Bayesian models. As your example is complicated I will provide a simplified one to make things easier to illustrate. Say you want to estimate linear regression model

$$ y_i = \beta_0 + \beta_1 x_i + \varepsilon_i $$

and based on the model you want to predict $y_\text{new}$ values given $x_\text{new}$ data. In this case you plug in the $x_\text{new}$ into estimated model and JAGS samples $y_\text{new}$ values based on your model. The code would look similar to this:

 beta0 ~ dnorm(0, 10)
 beta1 ~ dnorm(0, 10)
 sigma ~ dunif(0, 50)
 for (i in 1:N) {
    y[i] ~ dnorm(beta0 + beta1 * x[i], sigma)
 }
 for (j in 1:Nnew) {
    ynew[j] ~ dnorm(beta0 + beta1 * xnew[j], sigma)
 }

where y, x and xnew are data vectors and ynew is a variable for storing predictions. What you get is a distribution of values that are plausible given your estimated model. Since the model is probabilistic, the prediction is also probabilistic, i.e. we get the whole distribution of possible ynew values. For point-values take the average of ynew, you can also make prediction intervals taking the highest density intervals from ynew values.

Tim
  • 108,699
  • 20
  • 212
  • 390
  • but in bugs, when you use the symbol ~ you have to provide initial values for this node. – Abdelouahed BEN MHAMED Apr 23 '15 at 11:47
  • @AbdelouahedBENMHAMED What node? If some variable (e.g. `beta0` or `ynew`) is 'empty' then the values are just drawn from some distribution. JAGS does the same things with missing values in your data. – Tim Apr 23 '15 at 11:51
  • Why do you need "sigma" in "for (j in 1:Nnew) {ynew[j] ~ dnorm(beta0 + beta1 * xnew[j], sigma) }". Will I make a mistake if I do something like this in R (once I obtain beta0 and beta1 from JAGS): "for(j in 1:Nnew) {ynew[j] = beta0 + beta1 * xnew[j] }"? – eod Oct 27 '21 at 22:05
  • @eod it’s a Bayesian model, you want to sample from the posterior predictive distribution. – Tim Oct 28 '21 at 04:52