Skip to content

Commit ec75f37

Browse files
committed
move all method setup to setup_params()
1 parent fb33e26 commit ec75f37

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

R/stat-smooth.R

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -95,36 +95,52 @@ StatSmooth <- ggproto("StatSmooth", Stat,
9595
setup_params = function(data, params) {
9696
params$flipped_aes <- has_flipped_aes(data, params, ambiguous = TRUE)
9797
msg <- character()
98-
if (is.null(params$method) || identical(params$method, "auto")) {
98+
method <- params$method
99+
if (is.null(method) || identical(method, "auto")) {
99100
# Use loess for small datasets, gam with a cubic regression basis for
100101
# larger. Based on size of the _largest_ group to avoid bad memory
101102
# behaviour of loess
102103
max_group <- max(table(interaction(data$group, data$PANEL, drop = TRUE)))
103104

104105
if (max_group < 1000) {
105-
params$method <- "loess"
106+
method <- "loess"
106107
} else {
107-
params$method <- "gam"
108+
method <- "gam"
108109
}
109-
msg <- c(msg, paste0("method = '", params$method, "'"))
110+
msg <- c(msg, paste0("method = '", method, "'"))
110111
}
111112

112113
if (is.null(params$formula)) {
113-
if (identical(params$method, "gam")) {
114+
if (identical(method, "gam")) {
114115
params$formula <- y ~ s(x, bs = "cs")
115116
} else {
116117
params$formula <- y ~ x
117118
}
118119
msg <- c(msg, paste0("formula = '", deparse(params$formula), "'"))
119120
}
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"
122137
}
123138

124139
if (length(msg) > 0) {
125140
cli::cli_inform("{.fn geom_smooth} using {msg}")
126141
}
127142

143+
params$method <- method
128144
params
129145
},
130146

@@ -159,23 +175,6 @@ StatSmooth <- ggproto("StatSmooth", Stat,
159175
}
160176
}
161177

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-
179178
prediction <- try_fetch(
180179
{
181180
model <- inject(method(

0 commit comments

Comments
 (0)