Splines & Piecewise Models

ECON 3209 · Week 14, Lecture 2 · Kerala Agricultural University

Department of Development Economics, KAU

Autumn 2026

Learning Outcomes

By the end of this lecture, you will be able to:

  1. explain the intuition of piecewise linear regression
  2. define knots and hinge functions
  3. estimate spline-like models in Python using manual basis terms
  4. compare piecewise and quadratic fits
  5. interpret how slopes change across ranges of the regressor

Why Piecewise Models?

Sometimes a single straight line is too simple, but a global polynomial is too rigid or unstable.

Piecewise models let the slope change after a chosen threshold, called a knot.

Hinge Function

For a knot at \(c\), define

\[H(X-c)=\max(0, X-c)\]

Then estimate

\[Y = \beta_0 + \beta_1 X + \beta_2 H(X-c) + \varepsilon\]

Before the knot, slope is \(\beta_1\); after the knot, slope is \(\beta_1 + \beta_2\).

Spline Intuition

  • A spline joins local polynomial pieces smoothly at knots.
  • Piecewise linear regression is the simplest spline idea.
  • Knots can come from theory, policy thresholds, or exploratory analysis.

Example: income may rise quickly with landholding up to 3 hectares, then more slowly.

Choosing Knots

Possible strategies: - theoretical threshold - median or quartile cut-off - visible bend in the scatter plot - compare candidate models with AIC/BIC

There is no universal best knot without context.

Python Demo: Build a Hinge Variable

Python Demo: Estimate the Piecewise Model

Python Demo: Plot the Piecewise Fit

Interpretation of Slopes

  • Before 3 hectares, slope is coefficient on land.
  • After 3 hectares, slope is coefficient on land plus coefficient on hinge3.

If the hinge coefficient is negative, the relationship becomes flatter after the knot.

Python Demo: Compare Piecewise and Quadratic Fits

Python Demo: Try a Different Knot

When Are Splines Useful?

  • policy thresholds
  • life-cycle patterns with bends
  • engineering or agronomic dose-response curves
  • cases where local flexibility matters more than a global polynomial shape

Splines give flexibility while keeping interpretation manageable.

🏋️ Exercise

  1. Simulate landholding and income data with a slope change after 3 hectares.
  2. Construct a hinge variable with np.maximum(0, land - 3).
  3. Estimate a piecewise linear model and interpret the slope before and after the knot.
  4. Compare the piecewise model with a quadratic alternative using AIC/BIC.

Summary

✅ Piecewise models allow different slopes in different ranges of a regressor.

✅ A knot marks the point where the slope is allowed to change.

✅ Hinge variables provide a simple way to estimate spline-like models in Python.

✅ Comparing different knots or functional forms with AIC/BIC helps guide choice.

✅ Piecewise fits are often easier to interpret than high-order polynomials.

Next Lecture

Lecture 3 — Python Curve Fitting

We will cover: - numpy.polyfit - scipy.optimize.curve_fit - AIC/BIC comparison - choosing among nonlinear fits