微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么 stats 包中的 scatter.smooth 和 ggplot2 包中的 geom_smooth 给出不同的结果?

如何解决为什么 stats 包中的 scatter.smooth 和 ggplot2 包中的 geom_smooth 给出不同的结果?

这是基于r的黄土平滑曲线

dat <- data.frame(RT = c(151,160,168,169,172,175,189,279),IQ = c(123,121,115,110,105,103,100,120))
with(dat,scatter.smooth(IQ,RT,span = 0.8))

enter image description here

这是用ggplot生成的黄土曲线:

ggplot(dat,aes(x = IQ,y = RT)) +
geom_point() +
geom_smooth(method = stats::loess,se = F,span = 0.8)

enter image description here

为什么它们不同?

解决方法

这是因为 scatter.smooth() 不使用默认的 loess() 参数,而 geom_smooth() 使用。如果您将 scatter.smooth() 中默认的方法参数提供给 ggplot2 方法,它们会产生非常相似的结果。

dat <- data.frame(RT = c(151,160,168,169,172,175,189,279),IQ = c(123,121,115,110,105,103,100,120))
with(dat,scatter.smooth(IQ,RT,span = 0.8))

library(ggplot2)

ggplot(dat,aes(x = IQ,y = RT)) +
  geom_point() +
  geom_smooth(formula = y ~ x,method = "loess",se = F,span = 0.8,method.args = list(degree = 1,family = "symmetric"))

reprex package (v1.0.0) 于 2021 年 2 月 3 日创建

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。