R error (JAGS) Cannot insert node into alpha[1:284]. Dimension mismatch

202 views Asked by At

I am sure there's a lot of errors with this code. I'm fairly new to JAGS, and just can't figure out this model.

write("model {
#priors
 lamda ~ dunif(0,1)
 p ~ dunif(0,1)
 mu~ dnorm(0,0.001)
 tau ~ dgamma(.001,0.001)
#likelihoods
      for (i in 1:(nind+nz)) { #species observed and missed (i is species) #32 species + 250
      z[i] ~ dbern(lamda) #indicator variable to say the aniaml exists in the population #matrix given
      N[i]<-sum(z[i])
      beta ~ dnorm(mu) #random effect (logit of prob of detection by species)
      logit(p[i])<-N[i]*beta[i]
      
      for (j in 1:J) {#loop over sites for occurrence (J) #90 sites
      logit(eta[j,i])<-alpha[j]
      mu.psi[j,i] <-eta[j,i]*z[j,i]
      w[j,i] ~ dbern(mu.psi[j,i]) #presence of i in j (species present at sites
      Y~dbin(w[j,i],N[i]) #there or not there
      }}
  
      for (j in 1:J) {
      Nquad [j]<-sum(w[j,i])
      N<-sum(z[])
      }}",file = "Moray.txt")

Then I put in the data.

EelMatrix <- pivot_wider(EelCount, names_from=sci.name, values_from=NumPos) %>%
              select(-NotEel)
Present <- ifelse(EelMatrix>0, 1, 0)

EelData <- list(Y=EelMatrix, nind=34, nz=250, J=99)
#nind is number of species observed, nz is number of potential zeros, and J is the number of sampling observations (the sites)

str(EelData)

init1 <- list(list(mu = 0, tau = 1, psi = 0.5, z = Present),
              list(mu = 1, tau = 2, psi = 0.15, z = Present))
str(init1)
jagsMoray <- jags(EelData,init1,parameters.to.save=c("psi", "mu","tau"),
                n.chains = 2,n.burnin = 10000,n.iter = 250000,n.thin=30,
                model.file = "Moray.txt")

And I receive the error

Cannot insert node into alpha[1:284]. Dimension mismatch

1

There are 1 answers

0
Robert Thomas On

I don't see anywhere that alpha is being defined in the model and it also looks like you are not passing it in with the data to the model, so I am going to guess that is where an issue is coming from. If alpha is data then pass it in with EelData, otherwise it will need a prior