gavinsimpson / gratia

ggplot-based graphics and useful functions for GAMs fitted using the mgcv package
https://gavinsimpson.github.io/gratia/
Other
206 stars 28 forks source link

`partial_residuals.gam()` doesn't pass `...` on to `compute_partial_residuals()` #317

Closed gavinsimpson closed 2 months ago

gavinsimpson commented 2 months ago

From #316 I noticed that with the example there

library(mgcv)
library(gratia)

# ---- Simulate data ----

set.seed(123)
n <- 200
x1 <- runif(n, 0, 10)
x2 <- runif(n, 0, 10)
x3 <- runif(n, 0, 10)
y <- sin(x1) + 0.5 * x2 + rnorm(n)

# ---- Fit data ----

# Fit a GAM with 3 covariates
model <- gam(y ~ s(x1) + s(x2) + s(x3), method = "REML")

that you can't do

ds <- data_slice(model, x1 = evenly(x1))
sm <- smooth_estimates(model, select = "s(x1)", data = ds) |>
  mutate(presids = partial_residuals(model, select = "s(x1)", data = ds)) |> pull(`s(x1)`)

because compute_partial_residuals is doing odd things and partial_residuals doesn't pass on the data.

Fix compute_partial_residuals such that it does it for the model fitted data if data is NULL or for the data if not NULL

gavinsimpson commented 2 months ago

I'm an idiot - this is working just fine, you can't compute residuals for new data unless you already have the answer...