1

我在 JAGS 中有以下模型,但有时它不运行,这是代码:

library(rjags)

model1 <- "model {
for (j in 1:nobs){
  y[j] ~ dbeta(lambda[j] + 10^(-323), 1-lambda[j] - 10^(-323)) 
  
  logit(lambda[j]) <- inprod(X[j, ], beta) 
}
beta[1] ~ dnorm(0,0.1)
beta[2] ~ dgamma(1,1)
}"


n_chains = 1
n_adapt = 5000
n_iter = 10000
n_thin = 1
n_burnin = 5000

# generate data
n = 100

Ffun = plogis
design_mat = cbind(1, matrix(seq(0,1,by = 0.2), ncol=1))

gen_data = function(n, beta) {
X = design_mat[sample(nrow(design_mat), size = n, replace = T), ]
lambda = Ffun(X %*% beta)
y = rbeta(n, lambda, 1-lambda)
list(X = X, y = y)
}


for (i in 1:50){
beta = as.matrix(c(-3, 5))
jags_data = gen_data(n, beta)
jags_data$nobs = n
jg_model <- jags.model(textConnection(model1),
                   data = jags_data,
                   n.chains = n_chains,
                   n.adapt = n_adapt)
update(jg_model, n.iter = n_burnin)
result <- coda.samples(jg_model, 
                       variable.names = c("beta"),
                       n.iter = n_iter, 
                       thin = n_thin,
                       n.chains = n_chains)

beta_est = list(apply(result[[1]],2,median))
}

但这有时会运行,有时会出现此错误:

update.jags(object, n.iter, ...) 中的错误:节点 beta 1 切片器中的错误卡在具有无限密度的值

如果我按照此处的建议使用截断的 Beta,则会出现以下错误:

jags.model 中的错误(textConnection(model1),data = jags_data,n.chains = n_chains,:节点 y 1中的错误节点 与父节点不一致

4

0 回答 0