这是我的代码
library(R2jags) #library(rjags)
library(bayesplot)
library(coda)
# set working directory
setwd("/Users/isa/Desktop/logreg")
# BUGS model code
cat("model {
for( i in 1 : 8 ) {
y[i] ~ dbin(theta[i],n[i])
logit(theta[i]) <- beta0 + beta1 * x[i]
}
beta0 ~ dunif(-100, 100)
beta1 ~ dunif(-100, 100)
}",
file = "model_log.txt")
data <- read.delim("data.txt",
sep = "",
header = TRUE,
check.names = "FALSE",
stringsAsFactors = FALSE)
initsone <- list(beta0 = -100, beta1 = 100)
initstwo <- list(beta0 = 100, beta1 = -100)
initslog <- list(initsone, initstwo)
paramslog <- c("beta0", "beta1", "theta[6]")
outputlog <-
jags(data = data,
inits = initslog,
parameters.to.save = paramslog,
model.file = "model_log.txt",
n.chains = 2,
n.iter = 1000,
n.burnin = 1000,
n.thin = 1,
DIC = TRUE#,
# bugs.directory = getwd(),
# working.directory = getwd()
)
一切正常,直到我尝试编译输出。我收到一个错误:
Error in jags.model(model.file, data = data, inits = init.values, n.chains = n.chains, :
Error in node y[1]
Node inconsistent with parents
我相信这与我的 OpenBugs 格式的数据有关:
list(y = c(1, 3, 6, 8, 11, 15, 17, 19),
n = c(20, 20, 20, 20, 20, 20, 20, 20),
x = c(30, 32, 34, 36, 38, 40, 42, 44),
N = 8 )
但我将其转换为 R 格式:
y n x
1 20 30
3 20 32
6 20 34
8 20 36
11 20 38
15 20 40
17 20 42
19 20 44
我是否错误地转换了数据?数据哪里出错了?一切正常,直到我尝试编译输出。我收到一条错误消息:jags.model 中的错误(model.file,data = data,inits = init.values,n.chains = n.chains,:节点 y[1] 中的错误节点与父节点不一致