初始化数组时尺寸不匹配(JAGS)

时间:2019-02-26 19:18:57

标签: r bayesian jags

您想知道为什么JAGS会告诉我这里的尺寸与我的初始值不匹配。

我正在尝试拟合一个空间明确的捕获-捕获模型,在该模型中,我在每个时间步长估计一条鱼的位置(x,y)。在T = 21个时间步中,有M = 64个人。这是在数组s中估计的,该数组通过每个坐标的两个正态分布-x,y的i = M和t = T进行循环。使s的尺寸=(64,2,21)。

我对该数组的初始值是合适栖息地内的合理位置,并且是一个数组,尺寸为64、2、21。

但是,JAGS给了我错误Error in setParameters(init.values[[i]], i) : RUNTIME ERROR: Dimension mismatch in values supplied for s。如果我只是不初始化它,我将得到相同的错误,但是对于尺寸为64,21的状态矩阵z。如果我也没有提供z的值,则会得到错误Error in node y[1,1,2] Node inconsistent with parents,其中y是我的观测数组,尺寸为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")

0 个答案:

没有答案