Kruschke의 Doing Bayesian Data Analysis (제 9 장)에서 외삽하여 JAGS에서 계층 적 분석을 수행하려고합니다. 4 개의 헤드 비율에 대한 사후 매개 변수 추정을 얻고 싶습니다. 두 박하에서 나오는 동전 (세타 1, 2, 3, 4)과 각 박하에서 나온 동전의 평균 편견에 대한 견적 (민트 바이어스 : 오메가). 각 박하의 편차 인 kappa의 변동성을 상수로 유지했습니다. 문제는 두 번째 박하에서 사후 평가를 얻을 수 없다는 것입니다. 이전 샘플링을 샘플링하는 것처럼 보입니다. 누구든지 두 번째 민트에 대한 사후 평가를 생성하기 위해 모델 문자열 텍스트 (아래 3 단계 참조)를 수정하는 방법을 알고 있습니까?JAGS/rjags에서 여러 그룹에 대한 베이 즈 독립 매개 변수를 별도로 할당합니다.
library(rjags)
library(runjags)
library(coda)
############### 1. Generate the data
flips <- c(sample(c(rep(1,3), rep(0,9))), # coin 1, mint 1, 12 flips total
sample(c(rep(1,1), rep(0,4))), # coin 2, mint 1, 5 flips total
sample(c(rep(1,10), rep(0,5))), # coin 1, mint 2, 15 flips
sample(c(rep(1,17), rep(0,6)))) # coin 2, mint 2, 23 flips
coins <- factor(c(rep(1,12), rep(2,5), rep(3, 15), rep(4, 23)))
mints <- factor(c(rep(1,17), rep(2,38)))
nFlips <- length(flips)
nCoins <- length(unique(coins))
nMints <- length(unique(mints))
#################### 2. Pass data into a list
dataList <- list(
flips = flips,
coins = coins,
mints = mints,
nFlips = nFlips,
nCoins = nCoins,
nMints = nMints)
################### 3. specify and save the model
modelString <- "
model{
# start with nested likelihood function
for (i in 1:nFlips) {
flips[i] ~ dbern(theta[coins[i]])
}
# next the prior on theta
for (coins in 1:nCoins) {
theta[coins] ~ dbeta(omega[mints[coins]]*(kappa - 2) + 1, (1 - omega[mints[coins]])*(kappa - 2) + 1)
}
# next we specify the prior for the higher-level parameters on the mint, omega and kappa
for (mints in 1:nMints) {
omega[mints] ~ dbeta(2,2)
}
kappa <- 5
}
"
writeLines(modelString, "tempModelHier4CoinTwoMint.txt")
############################### Step 4: Initialise Chains
initsList <- list(theta1 = mean(flips[coins==1]),
theta2 = mean(flips[coins==2]),
theta3 = mean(flips[coins==3]),
theta4 = mean(flips[coins==4]),
omega1 = mean(c(mean(flips[coins==1]),
mean(flips[coins==2]))),
omega2 = mean(c(mean(flips[coins==3]),
mean(flips[coins==4]))))
initsList
############################### Step 5: Generate Chains
runJagsOut <- run.jags(method = "simple",
model = "tempModelHier4CoinTwoMint.txt",
monitor = c("theta[1]", "theta[2]", "theta[3]", "theta[4]", "omega[1]", "omega[2]"),
data = dataList,
inits = initsList,
n.chains = 1,
adapt = 500,
burnin = 1000,
sample = 50000,
thin = 1,
summarise = FALSE,
plots = FALSE)
############################### Step 6: Convert to Coda Object
codaSamples <- as.mcmc.list(runJagsOut)
head(codaSamples)
############################### Step 7: Make Graphs
df <- data.frame(as.matrix(codaSamples))
theta1 <- ggplot(df, aes(x = df$theta.1.)) + geom_density()
theta2 <- ggplot(df, aes(x = df$theta.2.)) + geom_density()
theta3 <- ggplot(df, aes(x = df$theta.3.)) + geom_density()
theta4 <- ggplot(df, aes(x = df$theta.4.)) + geom_density()
omega1 <- ggplot(df, aes(x = df$omega.1.)) + geom_density()
omega2 <- ggplot(df, aes(x = df$omega.2.)) + geom_density()
require(gridExtra)
ggsave("coinsAndMintsHier/hierPropFourCoinsTwoMints.pdf", grid.arrange(theta1, theta2, theta3, theta4, omega1, omega2, ncol = 2), device = "pdf", height = 30, width = 10, units = "cm")
내가 지금 확인하는 시간이 없어,하지만 한 번 데이터로 한 번에 대한 루프 두 번째의 인덱스로의 사용은'두 번, 몇 가지 문제를 일으키는 원인이 될 수 coins' 궁금? –
@ Jacob Socolar, 감사합니다. 그러나 '동전'은 데이터가 아니며 동전 번호를 나타내는 요소입니다. 'flips'는 데이터입니다. – llewmills
dataList는 '동전'이라는 변수를 데이터로 정의합니다. –