我正在尝试在 ggplot2 中使用 GAM 平滑。根据此对话和此代码,只有当 n >= 1000 时,ggplot2 才会加载用于一般加法模型的mgcv包。否则,用户必须手动加载包。据我了解,对话中的此示例代码应使用以下内容进行平滑处理geom_smooth(method="gam", formula = y ~ s(x, bs = "cs"))
:
library(ggplot2)
dat.large <- data.frame(x=rnorm(10000), y=rnorm(10000))
ggplot(dat.large, aes(x=x, y=y)) + geom_smooth()
但我收到一个错误:
geom_smooth: method="auto" and size of largest group is >=1000, so using gam with formula: y ~ s(x, bs = "cs"). Use 'method = x' to change the smoothing method.
Error in s(x, bs = "cs") : object 'x' not found
如果我尝试以下操作,也会发生同样的错误:
ggplot(dat.large, aes(x=x, y=y)) + geom_point() + geom_smooth(method="gam", formula = y ~ s(x, bs = "cs"))
但例如线性模型会起作用:
ggplot(dat.large, aes(x=x, y=y)) + geom_smooth(method = "lm", formula = y ~ x)
我在这里做错了什么?
我的 R 和包版本应该是最新的:
R version 3.0.3 (2014-03-06)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
other attached packages: mgcv_1.7-29 ggplot2_0.9.3.1