Choosing how wiggly is too wiggly

Today let's understand what actually decides how wiggly a fitted curve should be, when that decision is left to a rule instead of your own eye.

Here is the running example for this lesson. A ride-hailing app recorded the pickup wait time, in minutes, for 400 rides spread across one week, against the hour of day each ride happened, 0 to 24. Underneath the noise, the true wait time has a baseline of about 5 minutes, with two real rush-hour bumps, one near 8am and one near 6pm, each adding about 9 minutes, so wait climbs to around 14 minutes right at the two peaks.

Now suppose two people each draw a curve through that scatter by eye. One of them draws something close to a straight, gently rising line. The other draws something that bends sharply around almost every point. Both could defend their choice. Neither has a number to back it up.

The widget below is not drawn from today's 400 rides. It uses its own small built-in example, a wavy curve with noise on top, but the question it lets you play with is exactly the one this lesson is about: how far should a fitted curve be allowed to bend?

Drag the slider all the way to one end, then the other. At one end the fit barely leaves a straight line. At the other it bends enough to chase every little zigzag in the points, including the noise. You have just made, by eye, the same two choices that opened this lesson, and the rest of it is about replacing that guess with a number.

Why wiggliness cannot be chosen by eye

Let's see that same tension on the app's own numbers. Here are all 400 rides, with two very different curves fitted through them: one stiff, one wiggly, both chosen to sit at opposite extremes of how much a curve is allowed to bend.

RInteractive R
# Fit two very different curves through the same 400 rides, one stiff and one wiggly library(mgcv) set.seed(42) hour <- runif(400, 0, 24) true <- 5 + 9 * exp(-((hour - 8)^2) / (2 * 1.3^2)) + 9 * exp(-((hour - 18)^2) / (2 * 1.6^2)) wait <- true + rnorm(400, 0, 1.8) rides <- data.frame(hour, wait) fit_stiff <- gam(wait ~ s(hour, k = 20), sp = 100, data = rides) fit_wiggly <- gam(wait ~ s(hour, k = 20), sp = 1e-5, data = rides) grid_hour <- seq(0, 24, length.out = 200) curves <- rbind( data.frame(hour = grid_hour, wait = predict(fit_stiff, newdata = data.frame(hour = grid_hour)), curve = "stiff"), data.frame(hour = grid_hour, wait = predict(fit_wiggly, newdata = data.frame(hour = grid_hour)), curve = "wiggly"), data.frame(hour = grid_hour, wait = 5 + 9 * exp(-((grid_hour - 8)^2) / (2 * 1.3^2)) + 9 * exp(-((grid_hour - 18)^2) / (2 * 1.6^2)), curve = "true") ) library(ggplot2) ggplot(rides, aes(hour, wait)) + geom_point(color = "grey60", alpha = 0.45) + geom_line(data = curves, aes(hour, wait, color = curve, linetype = curve), linewidth = 1) + scale_color_manual(values = c(stiff = "#2b6cb0", wiggly = "#c05621", true = "black")) + scale_linetype_manual(values = c(stiff = "solid", wiggly = "solid", true = "dashed"))

  

Look at the stiff, blue curve first. Across the whole 24 hours it only rises from about 6.2 minutes to about 8.3 minutes. At hour 8 it sits near 7.5, and at hour 18 near 8.2, nowhere close to the real 14-minute peaks. It smooths both rush hours away completely, and in exchange it overshoots the quiet stretches instead, reading closer to 8 minutes where the true wait is only about 5.

Now look at the orange, wiggly curve. Between the two rush hours it does not sit still. It rises and dips in small zigzags from ride to ride, tracking individual noisy points rather than the one smooth shape those points actually came from. It still finds both peaks, but it is paying for that by also chasing noise it should be ignoring.

So which one is right? By eye, you could defend either: the stiff one looks calmer, the wiggly one looks like it tries harder to fit the data. That is exactly the problem. Eyeballing a curve gives different people different answers, and neither answer comes with a number attached to say how good it actually is. The next step writes down the number that both curves are secretly being judged against.