Statistics Glossary → Generalized Additive Model (GAM, Nonlinear Regression)
Generalized Additive Model (GAM, Nonlinear Regression)analysis
A regression method that models the outcome as a sum of smooth curves, one per explanatory variable, whose shapes are learned from the data rather than specified in advance, so that nonlinear relations are captured flexibly.
Concept
Linear regression sums up each predictor's effect with a straight line: every one-degree rise in temperature changes the outcome by the same amount. Real relations often are not like that; an effect may rise and then level off, or reverse beyond a certain point. A generalised additive model (GAM) learns the shape of the relation from the data without imposing it in advance and shows each predictor's effect as a separate curve. The result is a model that is both flexible and interpretable.
The model assumes that the outcome (or its expected value transformed by a link function) is a sum of smooth functions of the predictors: g(E[y]) = b₀ + s₁(x₁) + s₂(x₂) + …. Each curve s() is built from many small piecewise polynomials called splines. Overly wiggly curves are held back by a penalty: the larger the penalty, the flatter the curve; the smaller, the more closely it follows the data. The penalty strength is usually chosen from the data automatically. By choosing the family and link, continuous, binary, count or positive skewed outcomes can be modelled.
The flexibility of each curve is summarised by its effective degrees of freedom (edf): edf ≈ 1 indicates an almost linear relation, larger values a more curved one. Results are interpreted through partial-effect plots rather than coefficients: holding the other variables fixed, how does one predictor's contribution to the outcome change? The curves' p values are approximate; where data are sparse, at the ends, the curves are uncertain and should not be over-interpreted. Because the model is additive, interactions between two predictors are not captured unless specified explicitly.
When to use it
- When the relation between predictors and outcome is suspected to be nonlinear and its shape is not known in advance (environment, ecology, epidemiology, demand forecasting).
- If the form of the relation is known from theory (e.g. enzyme kinetics, a growth curve), use nonlinear regression; if the relation really is linear, an ordinary or generalised linear model is simpler and adequate.
- If only predictive accuracy matters and interpretability does not, machine-learning methods such as random forests or gradient boosting can also be considered.
An everyday example
A city wants to relate daily rentals in its bike-share scheme to the weather. Rentals climb steeply as the temperature rises from 5 °C to 22 °C but fall again above 30 °C; a straight line cannot see this peak. A GAM might produce an inverted-U curve for temperature and a steadily declining curve for wind speed. The city can use these curves to plan on which days to have more bikes ready.
Parameters in MerQur
Explanatory variable(s), Linear (parametric) terms, Categorical factors
Concept: In a GAM, predictors can enter in three ways: as smooth curves whose shape is learned from the data, as linear terms whose effect a straight line describes well enough, and as categorical factors with a separate effect for each level. Choosing the simplest adequate form for each variable keeps the model both understandable and stable.
Everyday example: In the bike-rental model, temperature and wind speed enter as smooth curves, the general year-on-year growth as a linear term, and day of the week as a categorical factor, giving Saturday and Sunday their own levels.
What it does: Selects the explanatory variables to be modeled with splines. Linear terms enter as unpenalized linear terms and categorical factors as treatment-coded (first level is the base level) dummies; for each factor an overall Wald test is given (F in Gaussian, chi-square in the others).
Effect on the result: Model structure.
On the validation data: airquality Temp linear term: b = 1.39050, SE 0.24554. Month factor: 4 dummies and overall F = 2.5353. equip year factor (Poisson): chi-square = 2.5916, p = 0.4590.
Family (Gaussian, Binomial, Poisson, Gamma)
Concept: The family sets the distribution suited to the outcome's possible values and the scale on which the curves are added (the link function). Gaussian is used for continuous symmetric outcomes, binomial for 0/1 outcomes, Poisson for counts and gamma for positive right-skewed outcomes.
Everyday example: Daily rentals are a count that cannot be negative, so the Poisson family fits. If the question were recast as 'did rentals exceed 1000 that day?', the binomial family would be chosen.
What it does: Response distribution and link: Gaussian (identity), binomial (logit), Poisson (log), gamma (log). Gaussian is chosen for a continuous response, binomial for 0/1, Poisson for counts, gamma for a positive right-skewed response.
Effect on the result: Log-likelihood, deviance, AIC and all estimates.
On the validation data: airquality Ozone, fixed lambda = 0.6: Gaussian total edf 14.010, deviance 29743.9; gamma edf 14.010, deviance 20.108; Poisson edf 21.515, deviance 531.33.
Number of splines (k)
Concept: The number of splines (k) is the number of building blocks a curve is made from and sets the maximum flexibility the curve can have. Because the actual flexibility is controlled by the penalty, it is enough for k to be large enough; a k that is too small cannot capture the true curve.
Everyday example: If rentals rise slowly up to 10 °C, quickly between 10 and 22 °C and then fall above 30 °C, the curve has more than one bend. A small value such as k = 3 cannot capture this shape; a larger k does no harm as long as the penalty reins in the unneeded flexibility.
What it does: Sets the number of basis functions per term (the upper limit of flexibility). It should be increased if the k-index is < 1 and p is small (Wood, 2017).
Effect on the result: Flexibility and edf; at fixed lambda the penalty also changes because the difference penalty depends on the number of coefficients.
On the validation data: mcycle, fixed lambda = 0.6: k = 4 edf 2.10, deviance 277380; k = 10 edf 5.59, deviance 128630; k = 20 edf 10.24, deviance 64055.
λ (lambda) (fixed)
Concept: Lambda is the strength of the penalty applied to the curve's wiggles. A large lambda pulls the curve towards a straight line, a small lambda lets it follow every fluctuation in the data; the right value captures the real pattern without tracking the noise.
Everyday example: If lambda is too small, the temperature curve also follows the low rentals of the odd rainy day and zigzags. If it is too large, the inverted-U disappears and a misleading straight line remains, saying 'rentals rise slightly with temperature'.
What it does: Penalty multiplier; a larger lambda gives a flatter curve. Applies only when "Fixed λ" is selected; it is for exploration, and automatic selection is recommended for inference.
Effect on the result: Flexibility and edf. With automatic selection the box is disabled.
On the validation data: mcycle k = 10: edf 8.51 for lambda 0.01; 5.59 for 0.6; 3.61 for 10; 2.50 for 100.
Smoothing selection (Fixed λ, Automatic GCV/UBRE, Automatic REML)
Concept: The degree of smoothing can be set by hand or chosen from the data. Generalised cross-validation (GCV) seeks the penalty that minimises prediction error on new data; REML chooses the penalty through the likelihood and usually gives more stable, somewhat smoother curves.
Everyday example: If the city's analyst chooses REML instead of trying lambdas by hand, the data decide how wiggly the temperature curve should be. This also stops different analysts from drawing different curves from the same data.
What it does: Selects lambda from the data: GCV (Gaussian) / UBRE (binomial, Poisson) or REML (Laplace approximation). REML is generally recommended (Wood, 2011); GCV may select a less smooth curve. A warning is given when the search falls on the grid boundary.
Effect on the result: Lambda, edf and the curve.
On the validation data: mcycle GCV: lambda 0.000837, edf 9.699, GCV 757.634; REML: lambda 0.00584, edf 8.829. airquality gamma: GCV edf 9.0899, REML edf 8.4726.
Additional output: Adjusted R² and test against the linear model
Concept: Adjusted R² gives the share of variability the model explains, with a penalty for model complexity. The test against the linear model checks whether the curves improve fit significantly compared with a model in which the same predictors enter as straight lines.
Everyday example: If the test is significant, the inverted-U shape for temperature is a real pattern and a straight line is not enough to describe the relation. If not, the city can settle for a simpler linear model.
What it does: Gives the adjusted R-squared and the F or chi-square test comparing the generalized linear model with linear terms in place of the smoothers.
Effect on the result: An additional table only.
On the validation data: mcycle fixed lambda: adjusted R2 0.5676, F = 34.409, df difference 4.390, p = 1.66e-20.
Additional output: Basis dimension check (k-index) and residual summary
Concept: The basis-dimension check tests whether the chosen k is enough to capture the true shape of the curve. If the k-index is clearly below 1 and the p value small, an unexplained pattern remains in the residuals and k should be increased; the residual summary helps review the model's overall fit.
Everyday example: If the temperature curve has a k-index of 0.85 and a p value of 0.01, the model may be missing the drop above 30 °C. Raising k from 10 to 20 and refitting helps capture that pattern.
What it does: Shows basis adequacy with the neighboring residual difference ratio (k-index) and the permutation p value (400 repetitions); k' is shown as k - 1.
Effect on the result: An additional table only.
On the validation data: mcycle k-index 0.5534; the permutation p value is random.
Example on real data
Data: New York air quality 1973, R datasets airquality
Variables: target Ozone, smoothed explanatory variables Solar.R (solar radiation), Wind and Temp (temperature)
- Question: Is the ozone level (Ozone) nonlinearly related to solar radiation (Solar.R), wind (Wind) and temperature (Temp)?
- Model: Gaussian GAM, a cubic P-spline (k = 10) for each explanatory variable, fixed λ = 0.6; complete observations N = 111.
- Model fit: Deviance (RSS) = 29743.86; deviance explained (pseudo R²) = .756; total edf = 14.01; AIC = 966.58.
- Spline terms: s(Wind) edf = 4.22, p < .001 and s(Temp) edf = 4.37, p < .001 are clear nonlinear effects; s(Solar.R) edf = 4.41, p = .051 is borderline non-significant (MerQur Wald approximation).
- Interpretation: Since edf > 1, all three relationships are curved; the partial dependence plots show that ozone rises with temperature and falls with wind.
- Limitation: Because λ is fixed, the smoothness is exploratory; automatic (GCV/REML) selection is recommended for inference. The smoother p values rest on an approximate Wald test and should be interpreted with care.
- APA: GAM (gaussian): R² = .76 (pseudo), AIC = 966.58, edf = 14.01, N = 111.
Related concepts
Splines and basis dimension (k)Smoothing parameter (λ, span)Regularization (penalty) and the penalty parameter λ/αDistribution family (GLM)Coefficient of determination (R²) and adjusted R²Generalized cross-validation (GCV)Generalized linear model (GLM)Restricted maximum likelihood (REML)
The numbers in the examples are MerQur's output on independent, publicly available validation data; see the MerQur Technical Note for details.