Aan de slagGa gratis aan de slag

Using smoothing parameters to avoid overfitting

The smoothing parameter balances between likelihood and wiggliness to optimize model fit. Here, you'll examine smoothing parameters and will fit models with different fixed smoothing parameters.

Deze oefening maakt deel uit van de cursus

Nonlinear Modeling with Generalized Additive Models (GAMs) in R

Cursus bekijken

Oefeninstructies

  • View the value of the smoothing parameter (\(\lambda\)) of the provided gam_mod model by extracting the sp value from the model.
  • Fit two models to the mcycle data with accel as a smooth function of times and a smoothing parameter of:
    • 0.1
    • 0.0001
  • Visualize both models.

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

library(mgcv)
# Extract the smoothing parameter
gam_mod <- gam(accel ~ s(times), data = mcycle, method = "REML")
___

# Fix the smoothing parameter at 0.1
gam_mod_s1 <- gam(accel ~ s(times), data = mcycle, sp = ___)

# Fix the smoothing parameter at 0.0001
gam_mod_s2 <- gam(___)

# Plot both models
par(mfrow = c(2, 1))
plot(gam_mod_s1, residuals = TRUE, pch = 1)
plot(gam_mod_s2, residuals = TRUE, pch = 1)
Code bewerken en uitvoeren