Skip to contents

A penalised-spline alternative to the kernel methods above. Fits y ~ s(x, k = k) and returns fitted values at x_eval.

Usage

gam_smoother(x, y, x_eval = NULL, k = 10, family = stats::gaussian())

Arguments

x

Numeric covariate vector.

y

Numeric outcome vector.

x_eval

Evaluation grid (defaults to x).

k

Basis dimension for the smoother (default 10).

family

GLM family for mgcv::gam (default gaussian()).

Value

A list with fit (the fitted gam object), x_eval, y_hat (predictions), and edf (effective degrees of freedom).

Examples

if (requireNamespace("mgcv", quietly = TRUE)) {
  set.seed(1)
  x <- sort(runif(40, -2, 2))
  y <- sin(x) + 0.2 * rnorm(40)
  xe <- seq(-1.5, 1.5, length.out = 20)
  r <- gam_smoother(x, y, x_eval = xe, k = 5)
  head(r$y_hat)
}
#> [1] -0.9758711 -0.9677393 -0.9382219 -0.8824320 -0.7990992 -0.6891929