Skip to content

Stat smooth+reml #3402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# ggplot2 (development version)

* `stat_smooth()` user `REML` by default, if `method = "gam"` and
`gam`'s method is not specified (@ikosmidis, #2630).

* Changed `theme_grey()` setting for legend key so that it creates no
border (`NA`) rather than drawing a white one. (@annennenne, #3180)

Expand Down
27 changes: 18 additions & 9 deletions R/stat-smooth.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#' For `method = "auto"` the smoothing method is chosen based on the
#' size of the largest group (across all panels). [stats::loess()] is
#' used for less than 1,000 observations; otherwise [mgcv::gam()] is
#' used with `formula = y ~ s(x, bs = "cs")`. Somewhat anecdotally,
#' used with `formula = y ~ s(x, bs = "cs")` with `method = "REML"`. Somewhat anecdotally,
#' `loess` gives a better appearance, but is \eqn{O(N^{2})}{O(N^2)} in memory,
#' so does not work for larger datasets.
#'
Expand Down Expand Up @@ -76,7 +76,6 @@ stat_smooth <- function(mapping = NULL, data = NULL,
#' @usage NULL
#' @export
StatSmooth <- ggproto("StatSmooth", Stat,

setup_params = function(data, params) {
if (identical(params$method, "auto")) {
# Use loess for small datasets, gam with a cubic regression basis for
Expand All @@ -90,17 +89,16 @@ StatSmooth <- ggproto("StatSmooth", Stat,
params$method <- "gam"
params$formula <- y ~ s(x, bs = "cs")
}
message("`geom_smooth()` using method = '", params$method,
"' and formula '", deparse(params$formula), "'")
}
if (identical(params$method, "gam")) {
params$method <- mgcv::gam
message(
"`geom_smooth()` using method = '", params$method,
"' and formula '", deparse(params$formula), "'"
)
}

params
},

compute_group = function(data, scales, method = "auto", formula = y~x,
compute_group = function(data, scales, method = "auto", formula = y ~ x,
se = TRUE, n = 80, span = 0.75, fullrange = FALSE,
xseq = NULL, level = 0.95, method.args = list(),
na.rm = FALSE) {
Expand All @@ -127,12 +125,23 @@ StatSmooth <- ggproto("StatSmooth", Stat,
xseq <- seq(range[1], range[2], length.out = n)
}
}

# Special case span because it's the most commonly used model argument
if (identical(method, "loess")) {
method.args$span <- span
}

if (is.character(method)) method <- match.fun(method)
if (is.character(method)) {
if (identical(method, "gam")) {
method <- mgcv::gam
} else {
method <- match.fun(method)
}
}
# If gam and gam's method is not specified by the user then use REML
if (identical(method, mgcv::gam) && is.null(method.args$method)) {
method.args$method <- "REML"
}

base.args <- list(quote(formula), data = quote(data), weights = quote(weight))
model <- do.call(method, c(base.args, method.args))
Expand Down
2 changes: 1 addition & 1 deletion man/geom_smooth.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/testthat/test-geom-smooth.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ test_that("default smoothing methods for small and large data sets work", {
y = x^2 + 0.5 * rnorm(1001)
)

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