@@ -95,36 +95,52 @@ StatSmooth <- ggproto("StatSmooth", Stat,
95
95
setup_params = function (data , params ) {
96
96
params $ flipped_aes <- has_flipped_aes(data , params , ambiguous = TRUE )
97
97
msg <- character ()
98
- if (is.null(params $ method ) || identical(params $ method , " auto" )) {
98
+ method <- params $ method
99
+ if (is.null(method ) || identical(method , " auto" )) {
99
100
# Use loess for small datasets, gam with a cubic regression basis for
100
101
# larger. Based on size of the _largest_ group to avoid bad memory
101
102
# behaviour of loess
102
103
max_group <- max(table(interaction(data $ group , data $ PANEL , drop = TRUE )))
103
104
104
105
if (max_group < 1000 ) {
105
- params $ method <- " loess"
106
+ method <- " loess"
106
107
} else {
107
- params $ method <- " gam"
108
+ method <- " gam"
108
109
}
109
- msg <- c(msg , paste0(" method = '" , params $ method , " '" ))
110
+ msg <- c(msg , paste0(" method = '" , method , " '" ))
110
111
}
111
112
112
113
if (is.null(params $ formula )) {
113
- if (identical(params $ method , " gam" )) {
114
+ if (identical(method , " gam" )) {
114
115
params $ formula <- y ~ s(x , bs = " cs" )
115
116
} else {
116
117
params $ formula <- y ~ x
117
118
}
118
119
msg <- c(msg , paste0(" formula = '" , deparse(params $ formula ), " '" ))
119
120
}
120
- if (identical(params $ method , " gam" )) {
121
- params $ method <- gam_method()
121
+
122
+ # Special case span because it's the most commonly used model argument
123
+ if (identical(method , " loess" )) {
124
+ params $ method.args $ span <- params $ span %|| % 0.75
125
+ }
126
+
127
+ if (is.character(method )) {
128
+ if (identical(method , " gam" )) {
129
+ method <- gam_method()
130
+ } else {
131
+ method <- match.fun(method )
132
+ }
133
+ }
134
+ # If gam and gam's method is not specified by the user then use REML
135
+ if (identical(method , gam_method())) {
136
+ params $ method.args $ method <- params $ method.args $ method %|| % " REML"
122
137
}
123
138
124
139
if (length(msg ) > 0 ) {
125
140
cli :: cli_inform(" {.fn geom_smooth} using {msg}" )
126
141
}
127
142
143
+ params $ method <- method
128
144
params
129
145
},
130
146
@@ -159,23 +175,6 @@ StatSmooth <- ggproto("StatSmooth", Stat,
159
175
}
160
176
}
161
177
162
- # Special case span because it's the most commonly used model argument
163
- if (identical(method , " loess" )) {
164
- method.args $ span <- span
165
- }
166
-
167
- if (is.character(method )) {
168
- if (identical(method , " gam" )) {
169
- method <- gam_method()
170
- } else {
171
- method <- match.fun(method )
172
- }
173
- }
174
- # If gam and gam's method is not specified by the user then use REML
175
- if (identical(method , gam_method()) && is.null(method.args $ method )) {
176
- method.args $ method <- " REML"
177
- }
178
-
179
178
prediction <- try_fetch(
180
179
{
181
180
model <- inject(method(
0 commit comments