R JAGS运行时错误:无法将节点插入X[]。尺寸不匹配

R JAGS运行时错误:无法将节点插入X[]。尺寸不匹配,r,bayesian,jags,R,Bayesian,Jags,我正在尝试向数据增强捕获-再捕获模型添加一些代码,并发现了一些以前从未遇到过的错误。简而言之,我想估计一系列生存阶段,每个阶段持续超过一个时间间隔。我希望该模型能够估计每个生存阶段的长度,并使用它来改进捕获-再捕获模型。我尝试了几种不同的方法,但都失败了,现在我正在尝试使用生存阶段的切换状态阵列来实现这一点: for (t in 1:(n.occasions-1)){ phi1switch[t] ~ dunif(0,1) phi2switch[t] ~ dunif(0,1) phi3

我正在尝试向数据增强捕获-再捕获模型添加一些代码,并发现了一些以前从未遇到过的错误。简而言之,我想估计一系列生存阶段,每个阶段持续超过一个时间间隔。我希望该模型能够估计每个生存阶段的长度,并使用它来改进捕获-再捕获模型。我尝试了几种不同的方法,但都失败了,现在我正在尝试使用生存阶段的切换状态阵列来实现这一点:

for (t in 1:(n.occasions-1)){

phi1switch[t] ~ dunif(0,1)  
phi2switch[t] ~ dunif(0,1)   
phi3switch[t] ~ dunif(0,1)   
phi4switch[t] ~ dunif(0,1)

psphi[1,t,1] <- 1-phi1switch[t]
psphi[1,t,2] <- phi1switch[t]
psphi[1,t,3] <- 0
psphi[1,t,4] <- 0
psphi[1,t,5] <- 0

psphi[2,t,1] <- 0
psphi[2,t,2] <- 1-phi2switch[t]
psphi[2,t,3] <- phi2switch[t]
psphi[2,t,4] <- 0
psphi[2,t,5] <- 0

psphi[3,t,1] <- 0
psphi[3,t,2] <- 0
psphi[3,t,3] <- 1-phi3switch[t]
psphi[3,t,4] <- phi3switch[t]
psphi[3,t,5] <- 0

psphi[4,t,1] <- 0
psphi[4,t,2] <- 0
psphi[4,t,3] <- 0
psphi[4,t,4] <- 1-phi4switch[t]
psphi[4,t,5] <- phi4switch[t]

psphi[5,t,1] <- 0
psphi[5,t,2] <- 0
psphi[5,t,3] <- 0
psphi[5,t,4] <- 0
psphi[5,t,5] <- 1
}
或许

[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2
现在我想给我的实际φ[]项分配一个平均值.phi[],它输入到模型中:

for(t in 1:(n.occasions-1)){
phi[t] ~ dnorm(mean.phi[PhiState[t]],phi.tau[PhiState[t]])
}
但是,当我尝试运行此操作时,会出现以下错误:

Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
  RUNTIME ERROR:
Cannot insert node into mean.phi[1:5]. Dimension mismatch
值得注意的是,当我使用以下phi[]测定时,该模型运行良好:

phi[t] ~ dunif(0,1) #estimate independent annual phi's

#设置固定的生存期(这是最好的方法,但我不想告诉它什么时候
#期间开始/结束以及有多少,因此是当前练习):
(1:21中的a){
surv[a]~dnorm(平均phi1,phi1_头)
}
对于(22:30中的b){
surv[b]~dnorm(平均phi2,phi2_头)
}
对于(t/1:(n.1)){

phi[t]我对您的实际数据(phi[t]
?)有点困惑,但以下内容可能会为您提供一个起点:

nt <- 29
nstate <- 5
M <- function() {
  phi_state[1] <- 1
  for (t in 2:nt) {
    up[t-1] ~ dbern(p[t-1])
    p[t-1] <- ifelse(phi_state[t-1]==nstate, 0, p_[t-1])
    p_[t-1] ~ dunif(0, 1)
    phi_state[t] <- phi_state[t-1] + equals(up[t-1], 1)
  }
  for (k in 1:nstate) {
    mean_phi[k] ~ dunif(0, 1)
    phi_sigma[k] ~ dunif(0, 20)
  }
  for(t in 1:(nt-1)){
    phi[t] ~ dnorm(mean_phi[phi_state[t]], phi_sigma[phi_state[t]]^-2)
  }
}

library(R2jags)
fit <- jags(list(nt=nt, nstate=nstate), NULL, 
            c('phi_state', 'phi', 'mean_phi', 'phi_sigma', 'p'), 
            M, DIC=FALSE)
nt
Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
  RUNTIME ERROR:
Cannot insert node into mean.phi[1:5]. Dimension mismatch
phi[t] ~ dunif(0,1) #estimate independent annual phi's
phi[t] ~ dnorm(mean.phi,phi_tau) #estimate hierarchical phi's from a single mean.phi
#Set fixed survial periods (this works the best, but I don't want to have to tell it when 
#the periods start/end and how many there are, hence the current exercise):
for (a in 1:21){
surv[a] ~ dnorm(mean.phi1,phi1_tau)
}

for (b in 22:30){
surv[b] ~ dnorm(mean.phi2,phi2_tau)
}

for (t in 1:(n.occasions-1)){
phi[t] <- surv[t]
}
nt <- 29
nstate <- 5
M <- function() {
  phi_state[1] <- 1
  for (t in 2:nt) {
    up[t-1] ~ dbern(p[t-1])
    p[t-1] <- ifelse(phi_state[t-1]==nstate, 0, p_[t-1])
    p_[t-1] ~ dunif(0, 1)
    phi_state[t] <- phi_state[t-1] + equals(up[t-1], 1)
  }
  for (k in 1:nstate) {
    mean_phi[k] ~ dunif(0, 1)
    phi_sigma[k] ~ dunif(0, 20)
  }
  for(t in 1:(nt-1)){
    phi[t] ~ dnorm(mean_phi[phi_state[t]], phi_sigma[phi_state[t]]^-2)
  }
}

library(R2jags)
fit <- jags(list(nt=nt, nstate=nstate), NULL, 
            c('phi_state', 'phi', 'mean_phi', 'phi_sigma', 'p'), 
            M, DIC=FALSE)