5
5
# ' For `method = "auto"` the smoothing method is chosen based on the
6
6
# ' size of the largest group (across all panels). [stats::loess()] is
7
7
# ' 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,
9
9
# ' `loess` gives a better appearance, but is \eqn{O(N^{2})}{O(N^2)} in memory,
10
10
# ' so does not work for larger datasets.
11
11
# '
@@ -76,7 +76,6 @@ stat_smooth <- function(mapping = NULL, data = NULL,
76
76
# ' @usage NULL
77
77
# ' @export
78
78
StatSmooth <- ggproto(" StatSmooth" , Stat ,
79
-
80
79
setup_params = function (data , params ) {
81
80
if (identical(params $ method , " auto" )) {
82
81
# Use loess for small datasets, gam with a cubic regression basis for
@@ -90,17 +89,16 @@ StatSmooth <- ggproto("StatSmooth", Stat,
90
89
params $ method <- " gam"
91
90
params $ formula <- y ~ s(x , bs = " cs" )
92
91
}
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
+ )
98
96
}
99
97
100
98
params
101
99
},
102
100
103
- compute_group = function (data , scales , method = " auto" , formula = y ~ x ,
101
+ compute_group = function (data , scales , method = " auto" , formula = y ~ x ,
104
102
se = TRUE , n = 80 , span = 0.75 , fullrange = FALSE ,
105
103
xseq = NULL , level = 0.95 , method.args = list (),
106
104
na.rm = FALSE ) {
@@ -127,12 +125,23 @@ StatSmooth <- ggproto("StatSmooth", Stat,
127
125
xseq <- seq(range [1 ], range [2 ], length.out = n )
128
126
}
129
127
}
128
+
130
129
# Special case span because it's the most commonly used model argument
131
130
if (identical(method , " loess" )) {
132
131
method.args $ span <- span
133
132
}
134
133
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
+ }
136
145
137
146
base.args <- list (quote(formula ), data = quote(data ), weights = quote(weight ))
138
147
model <- do.call(method , c(base.args , method.args ))
0 commit comments