Understand the idea
A single global polynomial has an awkward property: adjusting the fit to match a bend in one part of the data can shift the curve everywhere else too, and can send the tails shooting off wildly. A spline fixes this by breaking the predictor's range into several smaller sections, fitting a simple, low-degree curve (usually a cubic) within each section, and gluing those pieces together seamlessly at the breakpoints (called knots) so the join looks smooth rather than jagged.
The key advantage is LOCAL control: changing the data in one region of the predictor mostly affects the curve in that region, rather than distorting the whole fit the way a single global polynomial would. More knots (or a higher degrees-of-freedom setting) give the curve more places to bend and more overall flexibility, at the usual cost of a higher risk of chasing noise rather than real signal.
One thing worth remembering when reading the output: the individual numbers (coefficients) that come out of a spline fit are NOT meaningful on their own; each one multiplies an abstract mathematical basis piece, not a real-world quantity you can interpret directly. What actually matters is the FITTED CURVE ITSELF and its confidence band, never a single coefficient in isolation.
Spline regression works by basis expansion: the predictor is turned into several new columns that together let the fit bend at chosen knots. A natural cubic spline is made of smooth cubic pieces joined at the knots and kept straight beyond the outer knots, which stops the ends from swinging wildly; a B-spline is a convenient way to build such pieces.
Practitioner explanation
KernelStats offers two spline families: natural cubic (patsy's cr(), with a NATURAL boundary constraint forcing the curve to be exactly LINEAR beyond the outermost knots; the safer default, since it tames the wild tail behavior polynomials are prone to) and B-spline (patsy's bs(), cubic or degree-d pieces built from locally-supported basis functions; more flexible in the interior, but the tails can still swing and should not be trusted past the observed data range). Interior knots are placed at SAMPLE QUANTILES of the predictor (so every region carries a similar number of data points): df-2 for natural cubic, df-degree for B-spline, with the knot locations marked directly on the fitted-curve plot as vertical dashed lines.
The product states directly, as a required reading practice rather than a footnote, that R-squared ALWAYS RISES as degrees of freedom increase (mechanically, the same as adding polynomial degrees), so model comparison across different df values must use ADJUSTED R-squared or AIC, never raw R-squared, and the basis coefficients themselves are explicitly NOT individually interpretable, only the fitted curve and its confidence band are.
Three assumption checks run automatically, inherited from the underlying OLS machinery: homoscedasticity via Breusch-Pagan, approximate residual normality via Jarque-Bera (with the CLT relaxing this requirement at large n), and enough distinct predictor values to support the requested knots (too much df for too few points overfits, refused when the arithmetic makes the knot placement impossible). When heteroscedasticity is detected, the product's specific guidance is that the CURVE ITSELF remains valid: only the width of the confidence bands is affected, and robust (HC) standard errors are the fix, not a different curve.
When to use it
Consider it when
The relationship between a continuous outcome and a single continuous predictor bends in a way that neither a straight line nor a low-degree polynomial captures well, and you want the flexibility to stay LOCAL rather than affecting the whole curve's shape.
Use caution when
More than one predictor is involved (this function fits exactly one predictor's curve; use a GAM for multiple smooth predictors), you need to extrapolate confidently beyond the observed predictor range (even natural cubic splines' linear-tail guarantee is only as good as the assumption that linearity genuinely continues beyond the data), or the predictor has too few distinct values to support the requested degrees of freedom.
Formulas and symbols
Spline basis expansion
B_j(x) the j-th spline basis function; K (df) the degrees of freedom, controlling how many basis functions (and how wiggly the curve) are used.
Interior knot count
Knots are placed at sample quantiles of the predictor so every region carries a similar number of points.
Overall F-test
Compares the variation the spline curve explains with the variation left over, each per degree of freedom; a large F (a small p-value) is evidence that the curve describes the outcome better than a flat line at its mean.
Assumptions and checks
- Homogeneity of variance (homoscedasticity). Heteroscedasticity (residual spread that changes with the fitted value) makes the confidence band around the spline curve too narrow in some regions and too wide in others.
In KernelStats: Breusch-Pagan test on the residuals, reported directly on the card.
- Normality (of residuals, not of the raw data). Non-normal residuals make the F-test and the confidence band around the spline exact only approximately.
In KernelStats: Jarque-Bera test on the residuals, reported directly on the card.
- Independence of observations. Spline regression (like OLS) assumes rows are independent; time-series or clustered data make the confidence band understate the true uncertainty.
In KernelStats: Not checked by KernelStats: cannot be tested from cross-sectional data alone; stated directly as a standing reminder.
- Too many degrees of freedom (knots) relative to the sample size lets the spline chase noise rather than genuine curvature, producing a wiggly curve that will not generalize.
In KernelStats: Checked directly: the card flags a high degrees-of-freedom-to-n ratio as a warning and recommends comparing AIC across a few df values; it also notes that a natural cubic spline is constrained to be linear beyond the outer knots (safer to extrapolate slightly) while a B-spline is not constrained at the boundary at all.
Alternatives
- Polynomial Regression. A single global polynomial degree (rather than several joined local pieces) is an adequate and simpler description of the curve.
- Generalized Additive Model (GAM). You want smooth, flexible curves for MULTIPLE predictors combined additively in one model, rather than a single predictor's spline in isolation.
- LOESS (Local Regression Smoothing). You want to explore the shape of the relationship without committing to a specific spline basis or degrees-of-freedom choice first.
Worked example
Scenario
A natural cubic spline (df=4) fitting penguin body mass as a function of flipper length.
Result
Two interior knots (df - 2) at the flipper-length tertiles, 192 and 209.3 mm; the curve rises gently below 192 mm (about 15 g per mm) and steeply above it (about 52-59 g per mm); adjusted R-squared = 0.778 versus 0.758 for a straight line. Breusch-Pagan passes; Jarque-Bera flags non-normal residuals (the card notes the CLT makes this minor at n = 342).
Interpretation
The two-point gain in adjusted R-squared says the relationship is mostly linear, with a flatter stretch among the short-flippered birds, a bend the curve and its confidence band show far better than any basis coefficient could. Because that low-flipper stretch is mostly Adelie and Chinstrap and the steep stretch mostly Gentoo, the bend may reflect the species mix rather than a within-species curve.
Dataset: penguins.csv
Common misconceptions
Misconception
Comparing the raw R-squared of spline fits with different degrees of freedom tells you which flexibility level is genuinely better.
Correction
R-squared mechanically INCREASES as degrees of freedom rise, exactly as it does when adding polynomial degrees: a higher-df spline will always show equal or higher raw R-squared even when it is purely fitting noise. Adjusted R-squared or AIC are the correct basis for comparing different df choices, never raw R-squared.
Misconception
The individual basis coefficients from a spline fit can be read the way you'd read an ordinary regression slope.
Correction
Each spline basis coefficient multiplies an ABSTRACT mathematical basis function, not a real-world predictor value; it has no direct real-world interpretation on its own. The fitted curve and its confidence band are what should be read and reported; individual coefficients in isolation are not meaningful.
References
- An Introduction to Statistical Learning with Applications in R (2nd ed.), Chapter 7, James, G., Witten, D., Hastie, T. and Tibshirani, R. (2021).
- The Elements of Statistical Learning, Chapter 5, Hastie, T., Tibshirani, R. and Friedman, J. (2009).
