【发布时间】:2020-08-17 19:52:15
【问题描述】:
我使用rjags 安装了以下simple linear regression Bayesian 模型。
我能够通过分别指定所有预测变量来运行模型(例如 lm 对象)。现在我想学习如何通过将它们作为矩阵引入而不是单独指定它们来指定预测变量。
所以我运行了以下代码,但它给出了一些错误。
我使用rrr 包中的tobbaco 数据集提供了一个可重现的示例。
library(rrr)
require(dplyr)
library(rjags)
tobacco <- as_data_frame(tobacco)
N1 = length(tobacco$Y1.BurnRate)
x1 = model.matrix(Y1.BurnRate~X2.PercentChlorine+X3.PercentPotassium ,data = tobacco)
bayes_model_mul1=
"model {
for(i in 1:N1){
Y1.BurnRate[i]~dnorm(mu1[i],tau1)
for(j in 1:3){
mu1[i]=beta1[j]*x1[i,j]
}
}
for (l in 1:3) { beta1[l] ~dnorm(0, 0.001) }
tau1 ~ dgamma(.01,.01)
sigma_tau1 = 1/tau1
}"
model3 <- jags.model(textConnection(bayes_model_mul1),
data = list(Y1.BurnRate=tobacco$Y1.BurnRate, x1=x1, N1=N1),
n.chains=1)
运行 model3 后,出现以下错误。
jags.model 中的错误(textConnection(bayes_model_mul1),数据 = 列表(Y1.BurnRate = 烟草$Y1.BurnRate,:
运行时错误:
第 6 行编译错误。
尝试重新定义节点mu1[1]
谁能帮我解决这个问题? 这是因为将预测变量作为矩阵引入吗?
【问题讨论】:
标签: r linear-regression bayesian rjags