55# ' For `method = "auto"` the smoothing method is chosen based on the
66# ' size of the largest group (across all panels). [stats::loess()] is
77# ' used for less than 1,000 observations; otherwise [mgcv::gam()] is
8- # ' used with `formula = y ~ s(x, bs = "cs")`. Somewhat anecdotally,
8+ # ' used with `formula = y ~ s(x, bs = "cs")` with `method = "REML"` . Somewhat anecdotally,
99# ' `loess` gives a better appearance, but is \eqn{O(N^{2})}{O(N^2)} in memory,
1010# ' so does not work for larger datasets.
1111# '
@@ -76,7 +76,6 @@ stat_smooth <- function(mapping = NULL, data = NULL,
7676# ' @usage NULL
7777# ' @export
7878StatSmooth <- ggproto(" StatSmooth" , Stat ,
79-
8079 setup_params = function (data , params ) {
8180 if (identical(params $ method , " auto" )) {
8281 # Use loess for small datasets, gam with a cubic regression basis for
@@ -90,17 +89,16 @@ StatSmooth <- ggproto("StatSmooth", Stat,
9089 params $ method <- " gam"
9190 params $ formula <- y ~ s(x , bs = " cs" )
9291 }
93- message(" `geom_smooth()` using method = '" , params $ method ,
94- " ' and formula '" , deparse(params $ formula ), " '" )
95- }
96- if (identical(params $ method , " gam" )) {
97- params $ method <- mgcv :: gam
92+ message(
93+ " `geom_smooth()` using method = '" , params $ method ,
94+ " ' and formula '" , deparse(params $ formula ), " '"
95+ )
9896 }
9997
10098 params
10199 },
102100
103- compute_group = function (data , scales , method = " auto" , formula = y ~ x ,
101+ compute_group = function (data , scales , method = " auto" , formula = y ~ x ,
104102 se = TRUE , n = 80 , span = 0.75 , fullrange = FALSE ,
105103 xseq = NULL , level = 0.95 , method.args = list (),
106104 na.rm = FALSE ) {
@@ -127,12 +125,23 @@ StatSmooth <- ggproto("StatSmooth", Stat,
127125 xseq <- seq(range [1 ], range [2 ], length.out = n )
128126 }
129127 }
128+
130129 # Special case span because it's the most commonly used model argument
131130 if (identical(method , " loess" )) {
132131 method.args $ span <- span
133132 }
134133
135- if (is.character(method )) method <- match.fun(method )
134+ if (is.character(method )) {
135+ if (identical(method , " gam" )) {
136+ method <- mgcv :: gam
137+ } else {
138+ method <- match.fun(method )
139+ }
140+ }
141+ # If gam and gam's method is not specified by the user then use REML
142+ if (identical(method , mgcv :: gam ) && is.null(method.args $ method )) {
143+ method.args $ method <- " REML"
144+ }
136145
137146 base.args <- list (quote(formula ), data = quote(data ), weights = quote(weight ))
138147 model <- do.call(method , c(base.args , method.args ))
0 commit comments