Introduction to Generalized Additive Models with R and mgcv
Why Cross Validated Stack Exchange recommends it
Stats Stack Exchange (Cross Validated) users link it as the go-to tutorial for learning how GAMs, splines and penalization work in mgcv, including when answering questions about gamm4 and smoothing penalties.
Recommended by 2 people across 2 different months, 2021 to 2024.
What it is
A 3 hour 22 minute online webinar by Gavin Simpson (Bottom of the Heap) introducing generalized additive models in R. It explains how splines let GAMs learn non-linear relationships from data, the spline types in mgcv, and model fitting, checking, diagnostics and plotting.
What people said
Recommends Melissa Key · 2023-02 · Cross Validated Stack Exchange · on “GAMM4 models in R with either a continuous OR categorial variable in the smooth, and model comparison”
I'm not equipped to answer all of these questions (I'm trying to figure out how to work with gamm4 models myself, and there appears to be a dearth of information on them). For a general background on using splines, I've found the following youtube tutorial to be useful: [this video] , but it doesn't get into the peculiarities of gamm4 and its output at all. With that caveat, here are a few pointers that might help you (or others if you've already moved on) I'm not sure why you would want a continuous variable in the "by" statement. This is asking the model to create a separate smooth for each value of April_temp . Is that really what you want? (This, and other options, are explained in the video
Recommends Jack B · 2024-12 · Cross Validated Stack Exchange · on “Accounting for non-independence and autocorrelation in HGAM”
I am currently trying to fit a HGAM to model differences in daily activity patterns of fish in two treatments. Data were collected with high-resolution telemetry, and I currently have estimates of total distance swum (m) per hour (1-24) for 30 fish (~15 of each treatment) over the course of ~35 experimental days for each of three lakes (lakes A-C).These data have a clear hierarchical structure, which I am trying to account for with random effects (please let me know if my syntax is wrong as I am used to modelling in brms / lme4 ). I am creating a separate model for each lake to reduce model complexity. The model structure is as follows: mod <- gam(as.numeric(total_distance) ~ Treatment + exp
Cites COOLSerdash · 2021-03 · Cross Validated Stack Exchange · on “Generalized additive models: What exactly is being penalized when using a P-spline smoother with $\texttt{mgcv}$?”
Generalized additive models (GAMs) avoid overfitting by introducing a $\color{#D55E00}{\text{penalty term}}$ in the loss function: $$ ||\mathbf{y}-\mathbf{X\beta}||^{2} + \color{#D55E00}{\lambda\mathbf{\beta}^{\intercal}\mathbf{S\beta}} $$ where $\lambda$ controls the wiggliness of the smoother or basis. The optimal $\lambda$ can be estimated from the data using several methods, such as REML (restricted maximum likelihood), ML (maximum likelihood) or on the basis of the AIC (see the wonderful introductiory video [this video] on GAMs by @GavinSimpson). The function s() in R s mgcv [LINK] package let's you choose the type of smoother or basis (thin plate splines being the default). Among those smoo
Cites Jade · 2022-05 · Cross Validated Stack Exchange · on “Hessian not listed in GAM output of some models. Does this matter for significance of smooths and comparison with AIC?”
Would really appreciate some help as I don't know how to proceed given my GAM results. I haven't had much luck getting answers to my GAM questions (not sure if there is something wrong with the way I am asking questions), and don't know anyone working with GAMs. I have run two GAMs (in R/ mgcv) which I would like to compare with AIC: stime1 <- gam(deaths~s(time, k = 700, bs='tp') + heap + te(temp_max, lag, k=c(15,6), bs='tp')+ te(precip_daily_total, lag, k=c(7,6), bs='tp'), data = dat, family = nb, method = 'REML', select = TRUE) stime2 <- gam(deaths~s(time, k = 700, bs='tp') + heap + te(temp_mean, lag, k=c(17,6), bs='tp')+ te(precip_daily_total, lag, k=c(7,6), bs='tp'), data = dat, family =
Cites August Edwards · 2024-02 · Cross Validated Stack Exchange · on “GAM-modelling: Accounting for spatial autocorrelation in large datasets”
Let me start by saying that I am rather new to GAMs and using the mgcv package, so let me know if this question has already been addressed thoroughly in this forum. I am aware that there already exist a number of posts addressing the same issue, e.g. Why does including latitude and longitude in a GAM account for spatial autocorrelation? [LINK] My question relates on how to correctly account for spatial autocorrelation when fitting GAMs. I am currently building a model GAM model to describe house prices. The dataset is a collection of roughly 200K house sales in a particular geographic region. Relevant variables are: i) The sales price $P_{i}$ for each data point. ii) The coordinates of each