模擬 Beta 先驗分佈
假設你正在參選公職。設 \(p\) 為你的基本支持度,也就是打算投票給你的選民比例。根據過去的民調,你對 \(p\) 的先驗模型可用形狀參數為 45 與 55 的 Beta 分佈來表示。
你將使用 rbeta() 的隨機樣本來近似 Beta(45, 55) 的先驗。此函式需要三個引數:樣本大小(n)與兩個「shape」參數(shape1、shape2)。接著,你會使用 ggplot() 建立這些樣本的機率密度圖。此函式需要兩個引數:包含樣本的資料集,以及在 aes() 中要繪製於 x 軸的變數。密度圖的圖層以 geom_density() 加上。
本練習屬於課程
使用 RJAGS 的貝氏建模
練習說明
- 使用
rbeta()從 Beta(45, 55) 抽取 10,000 次樣本。將輸出指定給prior_A。 prior_sim資料框已包含prior_A樣本。對prior_sim套用ggplot(),建立先驗樣本的密度圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Sample 10000 draws from Beta(45,55) prior
prior_A <- rbeta(n = ___, shape1 = ___, shape2 = ___)
# Store the results in a data frame
prior_sim <- data.frame(prior_A)
# Construct a density plot of the prior sample
ggplot(prior_sim, aes(x = ___)) +
geom_density()