所有觀測值的平均反應之信賴區間
可以為資料集中的所有觀測值計算平均反應的信賴區間。直接在 twins 資料集上使用 augment(),可以根據所有 Biological 的觀測值,得到對 Foster 雙胞胎的預測與標準誤。
請注意,迴歸線在中心位置的計算較穩定,因此對極端值的預測比在解釋變數 IQ 範圍中間的預測更不穩定、變異也更大。
你先前計算的養育(Foster)雙胞胎 IQ 預測已提供為 predictions。這些預測透過 geom_smooth() 繪製於圖上。
本練習屬於課程
R 中的線性迴歸推論
練習說明
使用 predictions 手動重現 geom_smooth() 的效果。請在每個幾何層上提供對應的美術屬性(aesthetics)與資料。
- 加上一層點圖,將
Foster對Biological,使用data = twins資料集。 - 加上一層折線圖,將
.fitted對Biological,使用data = predictions資料集。將線條顏色設為"blue"。 - 加上一層帶狀圖,
x對應Biological、ymin對應lower_mean_prediction、ymax對應upper_mean_prediction。使用data = predictions資料集,並將透明度alpha設為0.2。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# This plot is shown
ggplot(twins, aes(x = Biological, y = Foster)) +
geom_point() +
geom_smooth(method = "lm")
ggplot() +
# Add a point layer of Foster vs. Biological, using twins
___(aes(___, ___), data = ___) +
# Add a line layer of .fitted vs Biological, using predictions, colored blue
___ +
# Add a ribbon layer of lower_mean_prediction to upper_mean_prediction vs Biological,
# using predictions, transparency of 0.2
___