Skip to content

Commit a339a18

Browse files
ikosmidishadley
authored andcommitted
Stat smooth+reml (#3402)
If gam and gam's method is not specified, use REML Fixes #2630
1 parent b3eb064 commit a339a18

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# ggplot2 (development version)
22

3+
* `stat_smooth()` user `REML` by default, if `method = "gam"` and
4+
`gam`'s method is not specified (@ikosmidis, #2630).
5+
36
* Changed `theme_grey()` setting for legend key so that it creates no
47
border (`NA`) rather than drawing a white one. (@annennenne, #3180)
58

R/stat-smooth.r

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
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
7878
StatSmooth <- 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))

man/geom_smooth.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-geom-smooth.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ test_that("default smoothing methods for small and large data sets work", {
3737
y = x^2 + 0.5 * rnorm(1001)
3838
)
3939

40-
m <- mgcv::gam(y ~ s(x, bs = "cs"), data = df)
40+
m <- mgcv::gam(y ~ s(x, bs = "cs"), data = df, method = "REML")
4141
range <- range(df$x, na.rm = TRUE)
4242
xseq <- seq(range[1], range[2], length.out = 80)
4343
out <- predict(m, data_frame(x = xseq))

0 commit comments

Comments
 (0)