我确信这段代码有很多错误。我对 JAGS 很陌生,只是无法弄清楚这个模型。
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")
然后我输入数据。
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")
我收到错误
无法将节点插入 alpha[1:284]。尺寸不匹配