Chapter 14: GAMMs — When Data Has Both Curves and Groups

3–5 minutes

By Chapter 14, statistics reaches a really beautiful point.

We already learned:

  • GLMs → Different distributions
  • GAMs → Nonlinear relationships
  • Mixed Models → Random effects
  • GEE → Population averages

But real data often has both:

  • Nonlinear behavior
  • Grouped behavior

Example:

Sales may:

  • Increase nonlinearly with customer count
  • Vary by retailer

That is where Generalized Additive Mixed Models (GAMMs) come in.


What Is a GAMM?

A GAMM combines a GAM with a Mixed Model:

$$
\text{GAMM}=\text{GAM}+\text{Mixed Effects}
$$

One of the most practical modern modeling frameworks.


The Problem GAM Solves — But Not Completely

Suppose we model:

$$
\log(\mu)=\beta_0+f(\text{CustomerCount})
$$

Great.

Customer count can affect sales nonlinearly.

But now suppose:

  • Retailer A consistently sells more.
  • Retailer B consistently sells less.

The smooth curve cannot explain those retailer-specific differences.

We need random effects.


The Problem Mixed Models Solve — But Not Completely

Suppose we model:

$$
\log(\mu)=\beta_0+\beta_1\text{CustomerCount}+u_j
$$

Now retailers can differ.

However, the relationship between customer count and sales is still assumed to be linear.

That assumption may fail.


GAMM Combines Both

The general GAMM model is:

$$
g(\mu)=X\beta+f(X)+Zu
$$

Interpretation:

  • $$X\beta$$ → Fixed effects
  • $$f(X)$$ → Smooth nonlinear effects
  • $$Zu$$ → Random effects

Everything together in one model.


Breaking Down the Model

Fixed Effects

$$
X\beta
$$

Represent overall trends.

Smooth Effects

$$
f(X)
$$

Represent nonlinear relationships.

Random Effects

$$
Zu
$$

Represent group-level variation.


Example: Customer Count and Retailers

Suppose sales depend on:

  • Customer count
  • Retailer

Model:

$$
\log(\mu)=\beta_0+f(\text{CustomerCount})+u_{\text{Retailer}}
$$

Interpretation:

  • Customer count → nonlinear effect
  • Retailer → individual adjustment

Why Not Just Use Polynomial Terms?

You could use:

$$
x+x^2+x^3
$$

However, polynomials can create:

  • Instability
  • Oscillation
  • Difficult interpretation

Splines are usually smoother and more stable.


Random Intercepts

The most common GAMM specification:

$$
g(\mu)=\beta_0+f(X)+u_j
$$

Meaning:

  • Same curve for everyone
  • Different baseline levels

Example:

RetailerRelative Sales Level
AHigher
BLower

The shape of the curve stays the same.


Random Slopes

A more flexible model:

$$
g(\mu)=\beta_0+f(X)+(\beta_1+u_j)X
$$

Now both:

  • Baseline level
  • Predictor effect

can vary by retailer.

Example:

RetailerResponse to Customer Growth
AStrong
BWeak

Smooth Interactions

Now GAMMs become very powerful.

Suppose month changes the effect of customer count.

Model:

$$
g(\mu)=\beta_0+f(\text{CustomerCount},\text{Month})
$$

Now the shape of the curve changes over time.


Tensor Product Smooths

Suppose:

  • Month is nonlinear
  • Customer count is nonlinear

Model:

$$
f(\text{CustomerCount},\text{Month})
$$

This creates smooth surfaces rather than simple curves.


Visualization

Instead of a 2D curve:

Sales
^
|
|
+-------------------->

You obtain a 3D response surface:

  • Customer Count
  • Month
  • Sales

all modeled simultaneously.


Example: Inventory Forecasting

Suppose you model:

  • Month
  • Customer Count
  • Year
  • SKU

Model:

$$ \log(\mu) \beta_0+ \beta_1\text{Month} +f(\text{CustomerCount})+\beta_2\text{Year}+u_{\text{SKU}}$$

Interpretation:

ComponentPurpose
MonthSeasonality
Customer CountNonlinear effect
YearTrend
SKURandom effect

This is essentially a GAMM.


Inventory Forecast Workflow

Step 1

Fit a GAMM.

Step 2

Forecast future periods.

Step 3

Aggregate demand.

Step 4

Adjust for lead time.

Step 5

Add safety stock.

This closely resembles enterprise forecasting systems.


Why Random Effects Help Forecasting

Without random effects:

all SKUs share information equally.

With random effects:

weak SKUs borrow information from stronger SKUs.

This is known as:

partial pooling.

One of the most powerful ideas in modern statistics.


Shrinkage Happens Again

Suppose observed sales are:

$$
50
$$

The model believes this month is unusually high.

The GAMM estimate may become:

$$
35
$$

Random effects help stabilize estimates.


GAMM vs GAM

GAMGAMM
NonlinearNonlinear
No groupingGrouping
Same curve for everyoneGroup-specific adjustments
No random effectsRandom effects

GAMM vs GLMM

GLMMGAMM
Linear effectsSmooth nonlinear effects
Random effectsRandom effects
Straight relationshipsFlexible curves

Estimation

Fitting GAMMs is harder than fitting GAMs.

We must estimate:

  • Smoothing parameters
  • Random effects
  • Fixed effects

simultaneously.

A common approach is:

REML


REML

REML stands for:

Restricted Maximum Likelihood

It generally provides better variance estimation than ordinary maximum likelihood.

Very common in mixed-model applications.


When Should You Use GAMMs?

Use GAMMs when:

  • Measurements are repeated
  • Predictors are nonlinear
  • Data is hierarchical
  • Random effects matter

Examples:

  • Inventory forecasting
  • Retailer sales analysis
  • Customer behavior modeling
  • Demand forecasting

When Should You Avoid GAMMs?

Avoid GAMMs when:

  • Datasets are very small
  • Relationships are clearly linear
  • Simple interpretation is the primary goal

Real Business Applications

Examples include:

  • Customer growth modeling
  • SKU demand forecasting
  • Seasonal inventory planning
  • Price elasticity analysis
  • Energy demand forecasting
  • Website engagement modeling

Inventory Problem Revisited

Suppose your model already contains:

  • Month
  • Spline(Customer Count)
  • Year
  • Lead Time

You are already close to a GAM.

Add:

$$
u_{\text{SKU}}
$$

and the model becomes a full GAMM.

Very realistic for inventory forecasting.


Chapter 14’s Big Lesson

Reality is both:

  • Nonlinear
  • Heterogeneous

Good models must handle:

  • Curves
  • Groups
  • Uncertainty

simultaneously.


Final Thought

Before Chapter 14, you ask:

“What curve fits the data?”

After Chapter 14, you ask:

“What curve fits each group?”

That shift moves statistics from:

one-size-fits-all modeling

to

adaptive learning across populations.

Leave a Reply

Discover more from Nerdish.Org

Subscribe now to keep reading and get access to the full archive.

Continue reading