後驗點估計
回顧以身高 \(X\) 解釋體重 \(Y\) 的貝葉斯迴歸模型之概似:$Y \sim N(m, s^2)$,其中 $m = a + b X$。工作環境中已提供進行 100,000 次反覆運算的 RJAGS 後驗模擬 weight_sim_big,以及馬可夫鏈輸出所組成的資料框:
> head(weight_chains, 2)
a b s iter
1 -113.9029 1.072505 8.772007 1
2 -115.0644 1.077914 8.986393 2
截距與斜率參數 \(a\) 與 \(b\) 的後驗「平均數」會反映體重與身高關係中的後驗平均「趨勢」。相對地,\(a\) 與 \(b\) 的「完整」後驗則反映參數合理值的「範圍」,也就是對該趨勢的後驗不確定性。你將在下方檢視此趨勢與其不確定性。bdims 資料已在你的工作環境中。
本練習屬於課程
使用 RJAGS 的貝氏建模
練習說明
- 取得
weight_sim_big鏈的summary()統計量。 - \(b\) 的後驗平均數會在
summary()的表 1 中呈現。請使用原始的weight_chains來驗證此計算。 - 建立
bdims中wgt對hgt的散佈圖。使用geom_abline()疊加「後驗平均趨勢」。 - 再建立一張
wgt對hgt的散佈圖。疊加由weight_chains前 20 組 \(a\) 與 \(b\) 參數值所定義的 20 條迴歸線。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Summarize the posterior Markov chains
# Calculate the estimated posterior mean of b
mean(___)
# Plot the posterior mean regression model
ggplot(bdims, aes(x = ___, y = ___)) +
geom_point() +
geom_abline(intercept = mean(___), slope = mean(___), color = "red")
# Visualize the range of 20 posterior regression models
ggplot(bdims, aes(x = ___, y = ___)) +
geom_point() +
geom_abline(intercept = ___[1:20], slope = ___[1:20], color = "gray", size = 0.25)