开始使用免费开始使用

所有观测的平均响应置信区间

可以为数据集中所有观测计算平均响应的置信区间。直接对 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
  ___
编辑并运行代码