geom_smooth() 中的 "method" 参数值有什么区别

what's difference between "method" argument values in geom_smooth()

我想在 X 轴和 Y 轴上为 Orange 数据集(内置数据集)agecircumference 的两个变量绘制平滑图.

我使用了这个代码:

ggplot(Orange, aes(
  x = age,
  y = circumference,
  shape = Tree,
  color = Tree
)) +
  geom_point() +
  scale_color_manual(values = c("red", "blue", "green", "black", "orange")) +
  ggtitle("Categorized Scatter plot") +
  theme_minimal() +
  theme(plot.title = element_text(hjust = 0.5)) +
geom_smooth(span = 0.7, se = TRUE, method = "loess") <----

结果:

主要问题

goem_smooth() 函数中 method 参数的 R 文档是:

smoothing method (function) to use, eg. "lm", "glm", "gam", "loess", "rlm". For method = "auto" the smoothing method is chosen based on the size of the largest group (across all panels). loess is used for than 1,000 observations; otherwise gam is used with formula = y ~ s(x, bs = "cs"). Somewhat anecdotally, loess gives a better appearance, but is O(n^2) in memory, so does not work for larger datasets.

但没有足够的解释说明哪一个最适合不同的场景。

请详细回答这个值。

如果树龄和周长之间的关系是线性的,您将使用 lm(线性模型)。

如果关系是线性的但可能因数据中异常值的存在而扭曲,您可以使用 rlm(稳健线性模型)淡化异常值对关系估计的影响。

如果关系是非线性但平滑的,则可以使用 loess 或 gam。黄土方法基于局部线性平滑并且可以处理离群值。 gam 方法允许不同类型的平滑 - 您使用哪种类型的平滑可能取决于您的模型是用于解释还是预测。

glm 方法在结果变量(在本例中为周长)将被视为二元变量(例如,低周长与高周长)的情况下会很有帮助。在这种情况下,glm 将使您能够将高周长的对数几率建模为年龄的线性函数。如果您怀疑年龄以 non-linear 方式影响对数几率,那么您可以使用 gam 而不是 glm。 glm 和 gam 还可以处理超过 2 个类别的结果变量、计数变量等

lm 和 rlm 函数还可以容纳 non-linear 参数形式的关系(例如,二次、三次、四次),但您必须将它们与公式规范结合使用。类似于:

geom_smooth(方法="lm",公式=y~x+I(x^2))

对于使用 lm 方法估计的二次关系。

相比之下,loess 和 gam 假设关系的非线性可以由非参数模型捕获。

如果使用 gam,您可以根据 pre-defined 标准(例如,用于预测目的的 AIC)研究可用的不同类型的平滑器和 select 您的 "best" 模型。对模型感到满意后,绘制其结果。