如何处理JAGS模型中的“不符合参数与inprod函数”

问题描述 投票:1回答:1

我试图使用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)估计值,以影响站点特定(点级)物种丰富度的方差。

任何帮助将不胜感激!

r jags r2jags hierarchical-bayesian
1个回答
0
投票

看起来你正在使用lengthU生成先验。在JAGS中,此函数将返回节点数组中的元素数。在这种情况下,这将是COV的行数乘以列数。

相反,我会为你提供给datajags.model列表提供一个标量。

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()
© www.soinside.com 2019 - 2024. All rights reserved.