開始使用免費開始

繪製 Bayesian 模型

在先前的練習中,我們已經估計了一個 Bayesian 模型,用來以歌曲的年齡(song_age)預測歌曲的人氣(popularity)。現在來把模型視覺化。使用已載入的 songs 資料集與 stan_model 物件,使用 ggplot2 繪製一張圖,呈現資料點與估計的迴歸線。

本練習屬於課程

使用 rstanarm 的貝葉斯迴歸建模

檢視課程

練習說明

  • 將模型參數的整齊摘要存到 tidy_coef
  • tidy_coef 取出估計的截距與斜率
  • 建立一張圖,以 song_age 為 x 軸、popularity 為 y 軸,顯示資料與估計的迴歸線

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Save the model parameters
tidy_coef <- ___(stan_model)

# Extract intercept and slope
model_intercept <- tidy_coef$___[1]
model_slope <- tidy_coef$___[2]

# Create the plot
ggplot(songs, aes(x = ___, y = ___)) +
  geom_point() +
  geom_abline(intercept = ___, slope = ___)
編輯並執行程式碼