R- JAGS运行时错误:无法将节点插入X [].维度不匹配



我正在尝试将一些代码添加到数据启示捕获捕获率模型中,并提出一些我以前从未遇到的错误。简而言之,我想估算一系列的生存阶段,每个阶段都持续一个时间间隔。我希望该模型估算每个生存阶段的长度,并使用它来改善捕获重新捕获模型。我尝试了几种不同的方法,并且正在尝试使用Switching状态阵列来实现生存阶段:

来实现这一点:
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
}

因此,这会创建一个[5,t,5]数组,其中生存状态只能切换到后续状态而不是向后切换(例如1至2、4至5,而不是4至3)。现在,我创建了一个定义生存状态的向量:

PhiState[1] <- 1  
for (t in 2:(n.occasions-1)){
# State process: draw PhiState(t) given PhiState(t-1)
PhiState[t] ~ dcat(psphi[PhiState[t-1], t-1,])
}

我们始终以状态1的启动,然后在每个时间步骤't'中进行分类抽奖,以保留在当前状态或转移到阵列中的概率下的下一个。我最多想要5个状态(假设该模型能够通过估计从状态3到4及以后移动的概率在功能上产生较少在现实中具有相同的生存价值)。因此,我创建了5个分层生存概率:

for (a in 1:5){
mean.phi[a] ~ dunif(0,1)
phi.tau[a] <- pow(phi_sigma[a],-2)
phi.sigma[a] ~ dunif(0,20)
}

现在,下一步是错误启动的位置。现在,我已经为我的phistate向量分配了值1-5,它应该看起来像这样:

[1] 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 4 4 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 2 2

,我现在想为我的实际phi []术语分配一个earman.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

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]
}

我确实阅读了这篇文章:https://sourceforge.net/p/mcmc-jags/discussion/610037/thread/36c48f25/

但是,在这种情况下,我看不到我在哪里重新定义变量...任何帮助解决此问题或在更好的方法上的建议都是最欢迎的!

非常感谢,乔什

我对您的实际数据有些困惑(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)

请注意,上面的p是向上移至下一个(相邻)状态的概率的向量。

最新更新