初始化数组时维度不匹配 (JAGS)

Jan*_*ger 5 r bayesian jags

想知道你们中是否有人知道为什么 JAGS 会告诉我这里的初始值与尺寸不匹配。

我正在尝试拟合一个空间明确的捕获-重新捕获模型,在该模型中我在每个时间步估计鱼的位置 (x,y)。对于 T=21 时间步长,有 M=64 个人。这是在 array 中估计的s,它通过 i=M 和 t=T 从每个坐标的两个正态分布中循环——x,y。使维度s= (64,2,21)。

我对这个数组的初始值是合适栖息地内的合理位置,是一个维度为 64、2、21 的数组。

然而,JAGS 给了我错误,Error in setParameters(init.values[[i]], i) : RUNTIME ERROR: Dimension mismatch in values supplied for s. 如果我只是不初始化它,我会得到同样的错误,但对于z维度为 64,21的状态矩阵。如果我也不提供 的值z,我会得到错误Error in node y[1,1,2] Node inconsistent with parentsy我的观察数组在哪里,维度 64、7、21(第二个元素是 #of 检测门)。

非常感谢任何帮助。完整代码见下文。

SourceForge交叉发布,并附有初始值s。不太确定如何在这里发布它。

sink("mod.txt")
cat("
model{


#######################################
# lamda0 = baseline detection rate, gate dependent and stage dependent
# sigma2 = scale parameter for decline in detection prob, time dependent
# phi =  survival, stage dependent
# s = activity center
# M=64 individuals, T=21 time steps
# z = true state, alive (1) or dead (0)
#######################################


for(i in 1:M){
for (j in 1:ngates){
  logit(lamda0[i,j])<- beta[group[i]]+ gamma[j]
}
}

for(j in 1:ngates){
  gamma[j] ~ dnorm(0,0.001)
  lam0.g1[j]<- 1/(1+exp(-gamma[j]))
  lam0.g2[j]<- 1/(1+exp(-gamma[j]-beta[2]))
}

beta[1]<-0
beta[2] ~dnorm(0,0.001)T(-10,10)

for (t in 1:T){
  sigma2[t] ~ dgamma(0.1,0.1)

}


tauv ~ dunif(0,40)
tau<- 1/(tauv*tauv)


phi[1] ~dunif(0,1)
phi[2] ~dunif(0,1)



for(i in 1:M){
  for(t in 1:(first[i]-1)){
    s[i,1,t]<-0   #before first detection, not in system
    s[i,2,t]<-0   #before first detection, not in system

    z[i,t]<-0         
  }
  for(t in (last[i]+1):T){
    s[i,1,t]<-0 
    s[i,2,t]<-0 
    z[i,t]<-0
  }
  #First period, locs and states
    z[i,first[i]] ~ dbern(1)   #know fish is alive 
    s[i,1,first[i]] ~ dunif(xl,xu)    #possible x,y coords 
    s[i,2,first[i]] ~ dunif(yl,yu)


    xdex[i,first[i]]<- trunc(s[i,1,first[i]]+1)
    ydex[i,first[i]]<- trunc(s[i,2,first[i]]+1)
    pOK[i,first[i]] <- habmat[xdex[i,first[i]],ydex[i,first[i]]] # habitat check
    OK[i,first[i]] ~ dbern(pOK[i,first[i]])   # OK[i] = 1, the ones trick


  for(j in 1:ngates){
  #First period, detection
    d[i,j,first[i]]<-sqrt(pow((s[i,1,first[i]]-gate.locs[j,1]),2) + pow((s[i,2,first[i]]-gate.locs[j,2]),2)) #estimate distance to gate (euclid) 
    d2[i,j,first[i]]<-pow(d[i,j,first[i]],2)
    lam_g[i,j,first[i]]<-lamda0[i,j]*exp(-d2[i,j,first[i]]/(2*sigma2[first[i]]))    
    y[i,j,first[i]] ~ dpois(lam_g[i,j,first[i]])  # number of captures/period/gate
}


  #Subsequent periods
  for(t in (first[i]+1):last[i]){
    s[i,1,t] ~ dnorm(s[i,1,(t-1)],tau)T(xl, xu)
    s[i,2,t] ~ dnorm(s[i,2,(t-1)],tau)T(yl, yu)


    xdex[i,t]<- trunc(s[i,1,t]+1)
    ydex[i,t]<- trunc(s[i,2,t]+1)
    pOK[i,t] <- habmat[xdex[i,t],ydex[i,t]] # habitat check
    OK[i,t] ~ dbern(pOK[i,t])   # OK[i] = 1, the ones trick

    for(j in 1:ngates){
        d[i,j,t]<-sqrt(pow((s[i,1,t]-gate.locs[j,1]),2) + pow((s[i,2,t]-gate.locs[j,2]),2)) #estimate distance to gate (euclid) 
        d2[i,j,t]<-pow(d[i,j,t],2)
        lam_g[i,j,t]<-lamda0[i,j]*exp(-d2[i,j,t]/(2*sigma2[t]))
        y[i,j,t] ~ dpois(lam_g[i,j,t])
    }  

    phiUP[i,t]<-z[i,t-1]*phi[group[i]]  #estimate 3-day survival rate
    z[i,t] ~ dbern(phiUP[i,t])


  }




} 
}#model
    ", fill=TRUE)
sink()

OK = matrix(1, nrow=M, ncol=T)

dat<-list(y=y, first=first, habmat=habmat, group=group, 
          xl=xl,xu=xu,yl=yl,yu=yu,
          last=last, OK = OK, M=M, T=T, 
          ngates=ngates,gate.locs=gate.locs)

z<-matrix(NA,M,T)
  for(i in 1:M){
    for(t in first[i]:last[i]){
      z[i,t]<-1
    }
  }

s<-readRDS("s_inits.Rda")





inits<-function() {list(phi=runif(2,0,1), sigma2=runif(T,0,0.5), tauv=runif(1,0,30), s=s, z=z)}

init1<-inits()
init2<-inits()
init3<-inits()
jag.inits<-list(init1,init2,init3)

params<-c("phiUP","tauv","sigma2","s","z","beta","gamma","phi")
Run Code Online (Sandbox Code Playgroud)