Search code examples
rggplot2smoothinggamloess

Equivalent of span using geom_smooth() with "gam"


This is probably a very basic question but I haven't found an answer yet. Is there an equivalent to the span argument in the geom_smooth function when method = "gam"? I am not familiar with GAM's in general so I would appreciate any input on that. I want to add a more flexible (wigglier) smoother to data with n > 1'000 and method = "loess" takes a lot of time to calculate.


Solution

  • mgcv::gam by default optimizes the smoothness using penalized regression. You can switch that off and specify the smoothness manually with the k parameter:

    ggplot(mpg, aes(displ, hwy)) +
      geom_point() +
      geom_smooth(method = "gam", 
                  formula = y ~ s(x, bs = "cs", fx = TRUE, k = 20))
    

    You should probably study the documentation of package mgcv.