JAGS模型中如何处理"Non-conforming parameters with inprod function"

How to deal with "Non-conforming parameters with inprod function" in JAGS model

我正在尝试使用 R2jags 对相机诱捕站的栖息地协变量的整体物种丰富度的方差进行建模。但是,我不断收到错误消息:

"Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
  RUNTIME ERROR:
Non-conforming parameters in function inprod"

我在以前的 JAGS 模型中使用了一个非常相似的函数(以查找物种丰富度),所以我不确定为什么它现在不起作用...

我已经尝试以不同的方式格式化 inprod 函数中的协变量,如数据框和矩阵,但无济于事。

变量说明:

J=length(ustations) #number of camera stations

NSite=Global.Model$BUGSoutput$sims.list$Nsite
NS=apply(NSite,2,function(x)c(mean(x)))

###What I think is causing the problem:

COV <- data.frame(as.numeric(station.cov$NDVI), as.numeric(station.cov$TRI), as.numeric(station.cov$dist2edge), as.numeric(station.cov$dogs), as.numeric(station.cov$Leopard_captures))

###but I have also tried:

COV <- cbind(station.cov$NDVI, station.cov$TRI, station.cov$dist2edge, station.cov$dogs, station.cov$Leopard_captures)

JAGS 模型:

sink("Variance_model.txt")
cat("model {
# Priors
Y ~ dnorm(0,0.001)              #Mean richness
X ~ dnorm(0,0.001)              #Mean variance

for (a in 1:length(COV)){
U[a] ~ dnorm(0,0.001)}      #Variance covariates

# Likelihood
for (i in 1:J) { 
mu[i] <- Y          #Hyper-parameter for station-specific all richness
NS[i] ~ dnorm(mu[i], tau[i])   #Likelihood
tau[i] <- (1/sigma2[i])
log(sigma2[i]) <- X + inprod(U,COV[i,])
}
}
", fill=TRUE)
sink()

var.data <- list(NS = NS, 
                 COV = COV,
                 J=J)

捆绑包数据:

# Inits function
var.inits <- function(){list(
  Y =rnorm(1), 
  X =rnorm(1), 
  U =rnorm(length(COV)))}

# Parameters to estimate
var.params <- c("Y","X","U")

# MCMC settings
nc <- 3
ni <-20000
nb <- 10000
nthin <- 10

启动 Gibbs 采样器:

jags(data=var.data,
     inits=var.inits,
     parameters.to.save=var.params,
     model.file="Variance_model.txt", 
     n.chains=nc,n.iter=ni,n.burnin=nb,n.thin=nthin)

最终,我得到了错误:

Compiling model graph
   Resolving undeclared variables
   Allocating nodes
Deleting model

Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains,  : 
  RUNTIME ERROR:
Non-conforming parameters in function inprod

最后,我想计算栖息地协变量的平均值和 95% 可信区间 (BCI) 估计值,这些协变量假设会影响站点特定(点级)物种丰富度的方差。

如有任何帮助,我们将不胜感激!

您似乎在使用 lengthU 生成先验。在 JAGS 中,此函数将 return 节点数组中的元素数。在这种情况下,这将是插入的行数 COV 乘以列数。

相反,我会为您提供给 jags.modeldata 列表提供一个标量。

var.data <- list(NS = NS, 
                 COV = COV,
                 J=J,
                 ncov = ncol(COV)
)

在此之后,您可以修改 JAGS 代码,为 U 生成先验。该模型将变为:

sink("Variance_model.txt")
cat("model {
# Priors
Y ~ dnorm(0,0.001)              #Mean richness
X ~ dnorm(0,0.001)              #Mean variance

for (a in 1:ncov){ # THIS IS THE ONLY LINE OF CODE THAT I MODIFIED
U[a] ~ dnorm(0,0.001)}      #Variance covariates

# Likelihood
for (i in 1:J) { 
mu[i] <- Y          #Hyper-parameter for station-specific all richness
NS[i] ~ dnorm(mu[i], tau[i])   #Likelihood
tau[i] <- (1/sigma2[i])
log(sigma2[i]) <- X + inprod(U,COV[i,])
}
}
", fill=TRUE)
sink()