Generalized additive models, one smooth at a time
Today let's understand how a generalized additive model lets one predictor bend into a curve, while every other predictor in the same model stays exactly the straight line it always was.
The running example for this lesson is FitZone, a gym chain that tracked its daily walk-in attendance for 250 days. Alongside attendance, FitZone also recorded that day's outdoor temperature, from 5 to 35 degrees Celsius, and that day's trial-pass discount, from 0 to 25 percent. The question this lesson answers is simple to ask: does attendance rise and fall with temperature in a straight line, or does it do something a straight line cannot describe?
Here are all 250 days, temperature on the x-axis and attendance on the y-axis.
The chart also reports a Pearson's r of 0.24, barely any straight-line relationship at all. That does not mean temperature has no effect on attendance. It means whatever effect temperature has, a straight line through this scatter is not built to see it.
Where a straight line misses the bend
Before fitting anything on FitZone's own numbers, it helps to know what trouble looks like on a residual plot, since that is exactly what is about to turn up in temperature. A residual is the gap between what a model predicted and what actually happened: the real value minus the fitted value. Plotting every residual against its own fitted value is one of the simplest ways to check a straight-line model's work. If the line is doing its job, the residuals scatter evenly around zero with no pattern in them.
The widget below switches between three fits. Flip it from Healthy fit to Curve (missed nonlinearity) and watch the residuals stop scattering evenly and bend into a clear U instead.
That U shape is the signature of a straight line trying to describe a bend. The line can only tilt one way across the whole range, so it ends up too high in some stretches and too low in others, and that leftover pattern shows up in the residuals instead of disappearing into noise.
FitZone's own numbers carry the same signature, just with real-world noise sitting on top of it. Fit a straight line through both predictors and look at its own residual plot below.
A one-degree rise in temperature is worth 0.64 more attendees by this straight line's reckoning, and every extra percentage point of discount is worth 1.94 more. Those numbers describe one fixed tilt and nothing else: the same 0.64 per degree whether temperature sits near 5 or near 35.
Look at the smoothed red line the plot draws through the residuals. It dips a little below zero around the middle of the fitted range and climbs back above zero at the high end, the same bend the widget's Curve tab showed, just fainter, because real data always carries more noise on top of the pattern than a clean demo does.