0

It's the first time I'm working with R2Jags, MCM chains and Bayesian models and I'm having trouble to compute the predicted values for my model. The model is based on research by Hallmann et al. 2017 (https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0185809), specifically the basic model (R code can be found in the appendix of the paper)

Here's my jags code

cat("model{
## Likelihood function for the latent expected daily biomass
for (i in 1:n) {
m_bio[i] ~ dnorm(sum(y[tau1[i]:tau2[i]]), sig_sq[i])
sig_sq[i] <- 1/Var[i]
Var[i] <- sum(vr[tau1[i]:tau2[i]])
}

## Likelihood function for muHat, it's dependent function and variance
for (i in 1:ndaily) {
z[i] <- exp(y[i])
y[i] <- g_intcp + log.lambda * year[i] + c[1] * daynr[i] + c[2] * daynr2[i] +
  c[3] * daynr[i] * year[i] + c[4] * daynr2[i] * year[i] + b[loctype[i]] +
  eps[plot[i]]
vr[i] <- exp(2 * y[i] + lvar) * (exp(lvar) - 1)
} 

## Priors
g_intcp ~ dnorm(0, .01)
log.lambda ~ dnorm(0, .01)
b[1] <- 0
for( i in 2:3) {b[i] ~ dnorm(0, .01)}
for( i in 1:4) {c[i] ~ dnorm(0, .01)}
sdhat ~ dunif(0, 5)
lvar <- pow(sdhat, 2)
for (i in 1:nrandom) {
eps[i] ~ dnorm(0, tau.re)
}
tau.re <- pow(sd.re, -2)
sd.re ~ dunif(0, 1)
}

The mathematical equations can be found in the paper (eqs. 2, 3, 4, 6)

I'm trying to get values for m_bio. I've looked into other posts like this one: How to predict values using estimates from rjags / JAGS but couldn't 'extrapolate' the solution from the simple regression formula (a + b * x) to mine.

I would start by extracting the mean of every parameter, with something along the lines of this:

jags$BUGSoutput$mean

The part where I'm stuck is the matrix multiplication. What changes when I have more than one predictor ?

I'm also unsure about what I have to do after that. If I understood the model correctly, I need to do that with all three equations, i.e. z[i], y[i], and vr[i] to then be able to calculate the values of m_bio (and the dependent eqs. sig_sq and Var), is that correct ?

Hope I adequately conveyed my problem. Appreciate any and all replies :)

L.Thoma
  • 23
  • 6
  • I think this is more of a question about how to do prediction in Bayesian inference than about programming; you're probably better off on the stats site. – user2554330 Jan 06 '21 at 15:07
  • Good point ! Posted it there – L.Thoma Jan 06 '21 at 16:20
  • Can you post a link to your question on the stats SO site so that users can answer there instead of here? – winni2k Aug 18 '21 at 06:46
  • Here's the link to crossvalidated [link](https://stats.stackexchange.com/questions/503731/predict-values-from-complex-rjags-model) – L.Thoma Feb 07 '22 at 09:04

0 Answers0