admin管理员组

文章数量:1334193

I am attempting to create a daily nest survival probability that spans 4 years. The model in question is the null model fitting only the intercept.

Model fitting in JAGs to estimate parameters using Bayes Theorem and MCMC to generate posterior distrib for each parameter.

Here is how I am setting up the model, referencing code to when this analysis was done previously, that only used a single year's worth of data. This model is trimmed down to reduce complexity, and includes the simplest version, following Kery and Royle's Ch 5.15 Model Building Strategy.

Index values:

first <- nest_filtered$first 
#calculated value. dt_found - earliest_dt_found + 1. L366. values < 42

lastCheck <- nest_filtered$lastCheck 
#numeric value, dt_lastCheck - earliest_dt_found. L366. values are less than 42

x <- encounter_matrix 
#366x42, matrix of nest encounter histories, if a nest is alive or dead (1/0) 

Now here is the model:

null<-list(x=x, nest_row=nrow(x), first=first, lastCheck=lastCheck)

writeLines("
model {
  #Fixed effects prior
  intercept ~ dunif(-5, 5)  #prior on the intercept

  #Model
  for (i in 1:nest_row) {  #loop through nests
    for (j in (first[i]):lastCheck[i]) {  #loop through days
      lq[i, j] <- intercept  #intercept only
      q[i, j] <- 1 / (1 + exp(-lq[i, j]))  #define logit
      p[i, j] <- x[i, j-1] * q[i, j]  
#p(underlying prob is Bernoulli distribution) defined as product of p (daily survival prob) * x (data in encounter histories)
      x[i, j] ~ dbern(p[i, j])  #data given Bernoulli distribution with prob p
    }
  }

  #Derived values
  realsurvival <- 1 / (1 + exp(-intercept))
  realcumsurvival <- pow(realsurvival, 29) #29 is ave period from init to hatch
}
", con = "model.txt")

#name model
mNULL<-'model.txt'

#initial values for estimation, where inits are random draws from normal distributions
initsN<-function(){
  list(intercept=rnorm(1,0,0.2)) #draw 1 value from normal with mean 0 and sd 0.2
}

#parameters to be estimated
parametersN <- c("intercept","realsurvival","realcumsurvival")

#pull the lever
outN <- jags(null, 
             initsN, 
             parameters.to.save=parametersN, 
             model.file=mNULL, 
             n.thin=1,
             n.chains=2,
             n.burnin=1000,
             n.iter=22000)

When I run outN, rjags outputs the error:

Error in rjags::jags.model(file = model.file, data = data, inits = inits,  : 
  RUNTIME ERROR:
Compilation error on line 11.
Index out of range taking subset of  x

Line 11 in question is:

p[i, j] <- x[i, j-1] * q[i, j]

Struggling to find why I am getting error Index out of range taking subset of x.

What I've tried: I've gone through and recalculated the data to ensure everything was correct (e.g., first, lastCheck, and matrix x)

Ensured first and lastCheck have a length of 366 (nests in analysis), with values less than 42 (days).

Also have tried to find similar posts with the same error message but could find a solution.

本文标签: