Reputation: 389
I have a model that I am trying to add covariates into. The model runs when I don't add "gamma". When I add "gamma", I get the following error:
Error in checkForRemoteErrors(val) :
3 nodes produced errors; first error: Error in node -1.8306
Index expression evaluates to non-integer value
I have tried to make all of the parts of the data list into numeric, I still have the same error. I am not sure how else to resolve this and I don't understand how a node could be negative. The augtemp data are normally distributed, I have z-scored the data. I have tried different prior distributions and that has not resolved the problem. If you are able to help, what does this error mean, where is a negative node, and how could I resolve this?
Thank you. The model and code are below. (cross-posted on: https://sourceforge.net/p/mcmc-jags/discussion/610037/thread/3634179a0d/?limit=25#85db)
# compile data into a list for JAGS
jags.data.augtemp <- list(Ndata = nrow(dat),
SS=dat$Redds.10000m2,
lRR=log(dat$EST.1.10000m2),
RR=dat$EST.1.10000m2,
stream = dat$StreamNum,
Nstream = max(dat$StreamNum),
augtemp = c(as.numeric(dat$Z_stream_augtemp_yr)),
#drainage=DrainageHeirarchy$DrainageNum,
#Ndrainage=max(DrainageHeirarchy$DrainageNum),
Year=as.numeric(as.factor(dat$Year)),
Nyear = max(as.numeric(as.factor(dat$Year)))
#MSF = BLT_SR$Z_MSF
)
str(jags.data.augtemp)
List of 9
$ Ndata : int 246
$ SS : num [1:246] 3.444 3.587 4.592 2.296 0.287 ...
$ lRR : num [1:246] 6.08 5.56 5.23 5.03 4.56 ...
$ RR : num [1:246] 437.6 259.4 186.6 153.6 95.4 ...
$ stream : num [1:246] 1 1 1 1 1 1 1 1 1 1 ...
$ Nstream: num 11
$ augtemp: num [1:246] -1.831 -0.074 0.694 -0.626 -2.066 ...
$ Year : num [1:246] 5 6 7 8 9 10 11 12 13 14 ...
$ Nyear : num 38
##### STEP 2: SPECIFY JAGS MODEL CODE #####
modelScript.name <- "ricker_augtemp.txt"
jagsscript <- cat("
model {
###priors####
alpha.m ~ dunif(1,100);
beta.m ~ dunif(0,1);
gamma ~ dunif(-5,5); # Prior for the coefficient of the august temp covariate
#FlowEff ~ dnorm(0,3);
s.a.sd ~ dunif(0,3);
s.b.sd ~ dunif(0,3);
#d.a.sd ~ dunif(0,3);
#y.a.sd ~ dunif(0,3);
sigmaObs ~ dunif(0,2);
####
####
####define ricker model###
####
####
for(i in 1:Ndata){
##alpha = recruits per stock unit at small stock size
##SS= stock, in this case the Redds/km2
##beta = b to density dependence
lRR[i] ~ dnorm(log(pRR[i]) -
(pow(sigmaObs,2)/2),pow(sigmaObs,-2)); #log recruits defined as a normal distribution where the mean is the log of the output from the ricker model and the sd is made into a precision parameter by taking the SD to the power of -2
pRR[i] <- alpha[i] * SS[i] * exp(-1*beta[i]*SS[i]); ##ricker model
lalpha[i] <- log(alpha.m) + s.a[stream[i]];# + y.a[Year[i]]; #taking into account the random effect of stream on alpha
lbeta[i] <- log(beta.m) + s.b[stream[i]] + gamma[augtemp[i]];#taking into account the random effect of stream on beta & covariate effect of mean august temp
alpha[i] <- exp(lalpha[i]); #exp() so no longer on log scale
beta[i] <- exp(lbeta[i]);
}
####
####
######this is defining things for drainage, but I don't have drainage in the data as of 8/11/23####
####
####
#for(DD in 1:Ndrainage){
#d.a[DD] ~ dnorm(0,pow(d.a.sd,-2));
#s.a.sd[DD] ~ dunif(0,3);
#}
####
####
####defining the distributions for the random effects of stream####
####
####
for(SS in 1:Nstream){
#s.a[SS] ~ dnorm(d.a[drainage[SS]],pow(s.a.sd[drainage[SS]],-2));
s.a[SS] ~ dnorm(0,pow(s.a.sd,-2));
s.b[SS] ~ dnorm(0,pow(s.b.sd,-2));
}
####
####
####defining the distributions for the random effects of year####
####
####
#for(YY in 1:Nyear){
#y.a[YY] ~ dnorm(0,pow(y.a.sd,-2));
#}
####
####
####defining management quantities####
####
####
for(SS in 1:Nstream){
Smsy[SS] <- ((log(alpha.m) + s.a[stream[SS]]) * (0.5 - 0.07*(log(alpha.m) + s.a[stream[SS]]))) / exp(log(beta.m) + s.b[stream[SS]])
}
}
",
file = here(modelScript.name))
##### SET NODES TO MONITOR #####
jags.params.temp <- c('beta.m','alpha.m','gamma','s.a','s.b','sigmaObs','Smsy')
mod_lm_augtemp <- R2jags::jags.parallel(jags.data.augtemp,
parameters.to.save = jags.params.temp,
model.file = here(modelScript.name),
n.chains = 3,
n.burnin = 30000/2, # number of post-burn-in samples per chain
n.thin = 10, # thinning rate
n.iter = 30000)
Upvotes: 0
Views: 33