使用 RJAGS 的貝氏建模
Alicia Johnson
Associate Professor, Macalester College
$Y_i$ = 第 $i$ 位成年人的體重(kg)
$X_i$ = 第 $i$ 位成年人的身高(cm)
$\;$
模型
$Y_i \sim N(m_i, s^2)$
$m_i = a + b X_i$
$a \sim N(0, 200^2)$
$b \sim N(1, 0.5^2)$
$s \sim \text{Unif}(0, 20)$



$Y_i \sim N(m_i, s^2)$
$m_i = a + b X_i$
wt_mod <- lm(wgt ~ hgt, bdims)
coef(wt_mod)
(Intercept) hgt
-105.011254 1.017617
summary(wt_mod)$sigma
9.30804
weight_model <- "model{
# Likelihood model for Y[i]
# Prior models for a, b, s
}"
weight_model <- "model{
# Likelihood model for Y[i]
for(i in 1:length(Y)) {
}
# Prior models for a, b, s
}"
weight_model <- "model{
# Likelihood model for Y[i]
for(i in 1:length(Y)) {
Y[i] ~ dnorm(m[i], s^(-2))
}
# Prior models for a, b, s
}"
weight_model <- "model{
# Likelihood model for Y[i]
for(i in 1:length(Y)) {
Y[i] ~ dnorm(m[i], s^(-2))
m[i] <- a + b * X[i]
}
# Prior models for a, b, s
}"
<-,不要用 ~weight_model <- "model{
# Likelihood model for Y[i]
for(i in 1:length(Y)) {
Y[i] ~ dnorm(m[i], s^(-2))
m[i] <- a + b * X[i]
}
# Prior models for a, b, s
a ~ dnorm(0, 200^(-2))
b ~ dnorm(1, 0.5^(-2))
s ~ dunif(0, 20)
}"
$m_i = a + b X_i$
<-,不要用 ~ $a \sim N(0, 200^2)$
# 編譯模型 weight_jags <- jags.model(textConnection(weight_model), data = list(X = bdims$hgt, Y = bdims$wgt), inits = list(.RNG.name = "base::Wichmann-Hill", .RNG.seed = 2018))dim(bdims)
507 25
head(bdims$hgt)head(bdims$wgt)
174.0 175.3 193.5 186.5 187.2 181.565.6 71.8 80.7 72.6 78.8 74.8
# 編譯模型
weight_jags <- jags.model(textConnection(weight_model),
data = list(X = bdims$hgt, Y = bdims$wgt),
inits = list(.RNG.name = "base::Wichmann-Hill",
.RNG.seed = 2018))
# 模擬後驗分佈
weight_sim <- coda.samples(model = weight_jags,
variable.names = c("a", "b", "s"),
n.iter = 10000)

標準化身高自變數(減去平均數並除以標準差)
拉長鏈長度


使用 RJAGS 的貝氏建模