ggplot2 で GAM スムージングを使用しようとしています。この会話とこのコードによると、ggplot2 は、n >= 1000 の場合にのみ、一般的な加法的モデルに使用される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