【发布时间】:2020-09-16 05:11:53
【问题描述】:
我正在使用 beta 二项式 n 混合模型来估计丰度。我的代码在模拟和自然数据集上运行良好。但是,当我运行特定数据集时,会弹出此错误 Error in jags.model(etc...) Error in node N[1] Node compatible with parents. 我想知道为什么它与这个数据集而不是其他数据集有问题。 数据集中有相当数量的零不起作用,但数据集中有更多的有效也有相当数量的 0。我玩过之前的值,范围从 0.001 到 10。数据结构和模型如下
n.site <- 6
R <- n.site #number of sites
T <- 10 #number of replicate counts
y <- array(dim = c(R, T)) #null array
y <- array(sur.2019$Num_total, dim = c(R, T)) #populate y
C <- c(y)
C <- as.numeric(C)
site = 1:R
site.p <- rep(site, T)
sink("Model.txt")
cat("
model {
# Priors
lam~dgamma(0.01,0.01)
alpha.p~dgamma(0.01,0.01)
beta.p~dgamma(0.01,0.01)
# Likelihood
#the next four lines are used to model N as a zero-truncated distribution
probs[R+1]<- 1-sum(probs[1:R])
for(i in 1:R){
probs[i]<- exp(-lam)*(pow(lam,x[i]))/(exp(logfact(x[i])) * (1-exp(-lam)))
N[i] ~ dcat(probs[])}
# Observation model for replicated counts
for (i in 1:n) {
C[i] ~ dbin(p[i], N[site.p[i]])
p[i]~dbeta(alpha.p,beta.p)
# Assess model fit using Chi-squared discrepancy
# Compute fit statistic for observed data
eval[i] <- p[i]*N[site.p[i]]
E[i] <- pow((C[i] - eval[i]),2) / (eval[i] + 0.5)
# Generate replicate data and compute fit stats
C.new[i] ~ dbin(p[i], N[site.p[i]])
E.new[i] <- pow((C.new[i] - eval[i]),2) / (eval[i] + 0.5)
} # ends i loop
# Derived and other quantities
totalN <- sum(N[]) # Estimate abundance across all sites
mean.abundance <- lam #mean expected abundance per plot
p.derived<-alpha.p/(alpha.p+beta.p) #derived detection probability
rho.derived<-1/(alpha.p+beta.p+1) #correlation coefficient
fit <- sum(E[])
fit.new <- sum(E.new[])
}
",fill = TRUE)
sink()
R = nrow(y)
T = ncol(y)
n = dim(y)[1] * dim(y)[2]#number of observations (sites*surveys)
nmm.data <- list(C = C, n=n, R = R, site.p = site.p, x=1:R)
# Initial values
Nst <- apply(y, 1, max) + 1 #changed from apply(y, 1, max) + 1
Nst[is.na(Nst)] <- 1
inits <- function(){list(N = Nst, lam = runif(1, 1, 7),alpha.p=runif(1,0.5,1), beta.p=runif(1,0.5,1))}
# Define parameters to be monitored
params <- c("totalN", "mean.abundance", "lam", "p.derived", "rho.derived", "fit", "fit.new","alpha.p","beta.p")
# MCMC settings
ni <- 14000
nt <- 1
nb <- 4000
nc <- 3
abun.1 <- jags(data = nmm.data,
parameters = params,
inits = inits,
model = "model.txt",
n.thin = nt,
n.chains = nc,
n.burnin = nb,
n.iter = ni)
abun.1.mcmc <- as.mcmc(abun.1)
不起作用的数据集如下所示
nmm.data <- list(C = C, n=n, R = R, site.p = site.p, x=1:R)
nmm.data
$C
[1] 7 0 1 0 0 0 2 0 0 0 0 1 3 2 0 0 0 0 1 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[55] 0 0 0 0 0 0
$n
[1] 60
$R
[1] 6
$site.p
[1] 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
[55] 1 2 3 4 5 6
$x
[1] 1 2 3 4 5 6
虽然起作用的数据集看起来像这样
hmm.data <- list(C = C, n=n, R = R, site.p = site.p, x=1:R)
hmm.data
$C
[1] 0 0 0 0 0 2 0 1 0 0 0 0 0 3 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 1 0 0 0 1 0 0
[55] 0 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[109] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[163] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[217] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
$n
[1] 60
$R
[1] 6
$site.p
[1] 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
[55] 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
[109] 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
[163] 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
[217] 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6
$x
[1] 1 2 3 4 5 6
正如我所说,我尝试过使用先验,但仍然在节点 N[1] 处遇到问题。我是 JAGS 的新手,不知道如何解决这个问题,因为它似乎不是编码问题,而是数据问题。任何人有任何想法可能会发生什么?我感谢任何花时间查看它的人。
nmm.data 的输出是
list(C = c(7, 0, 1, 0, 0, 0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 6L, 1L, 2L, 3L, 4L, 5L,
6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L,
4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L,
2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L,
6L), x = 1:6)
【问题讨论】:
-
能否请您
dput(nmm.data)和dput(hmm.data)并将输出粘贴到您的问题中?有了您分享的内容,我们无法重现您的问题。还分享执行马尔可夫链以检查所有规范的代码:) -
我已经添加了用于运行 JAGS 模型的其余代码,并在每个数据集上添加了来自 dput 的输出。如果您需要其他信息,请告诉我,再次感谢您查看此代码。
标签: r jags mixture-model