所有观测的平均响应置信区间
可以为数据集中所有观测计算平均响应的置信区间。直接对 twins 数据集使用 augment(),即可基于所有 Biological 的观测为 Foster 双胞胎给出预测值和标准误。
请注意,回归线在中心位置更稳定,因此解释变量 IQ 取值范围的两端比中间位置的预测更不稳定、变异更大。
上次您计算的 Foster 双胞胎 IQ 预测已作为 predictions 提供。这些预测在图中通过 geom_smooth() 展示。
本练习是课程的一部分
R 中的线性回归推断
练习说明
使用 predictions 手动重现 geom_smooth() 的效果。为每个几何对象分别提供美学映射和数据。
- 添加
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
___