From coefficients to predictions people understand

Today let's turn a fitted regression model into the three things people actually ask for once it exists: a predicted number, a range around that number, and a picture of how the number moves as one input changes.

Here is a food delivery dispatcher looking back at the last 60 deliveries. For each one, three things were recorded: how far it travelled, in km, whether it happened during rush hour, and how long it took, in minutes.

A model has already been fit to this data, one that predicts delivery_time from distance_km, distance_km squared, and rush_hour. This lesson starts from that fitted model, not from fitting it. Here is what the 60 deliveries look like.

Distance and delivery time climb together, but the climb curves rather than sitting on one straight line. And at almost any distance, the points split into two loose bands, one sitting above the other by about the same gap throughout: rush hour's effect, visible before anything has even been fitted to it.

The fitted model you are handed

A model like this is usually handed to you already fit. Someone, maybe you last week, maybe a colleague, ran lm() on these 60 deliveries and saved the result. Build that same model once here, so it is ready to reuse without rebuilding it.

RInteractive R
# Build the 60 deliveries you have been handed, and the model fit to them set.seed(42) distance_km <- round(runif(60, 1, 20), 1) rush_hour <- sample(rep(c(0, 1), length.out = 60)) delivery_time <- round(9 + 2.7 * distance_km - 0.065 * distance_km^2 + 8.5 * rush_hour + rnorm(60, 0, 3), 1) deliveries <- data.frame(distance_km, rush_hour, delivery_time) fit <- lm(delivery_time ~ distance_km + I(distance_km^2) + rush_hour, data = deliveries) round(coef(fit), 4) round(summary(fit)$r.squared, 4) #> (Intercept) distance_km I(distance_km^2) rush_hour #> 9.0755 2.7705 -0.0726 8.4682 #> [1] 0.9024

  

Four numbers came out of that fit, and this lesson is not about how lm() found them. It is about what to do with them once you have them.

9.08 is the intercept: what the model would predict at distance_km = 0 and rush_hour = 0. It mostly exists to make the arithmetic work, not a number you would quote to anyone.

2.77 is the coefficient on distance_km. On its own, it would say that every extra km adds 2.77 minutes. But I(distance_km^2) also has a coefficient, -0.0726, so distance_km's effect on delivery_time is not one fixed number. It curves: the squared term chips away a little more from each extra km's gain as distance_km grows.

8.47 is the coefficient on rush_hour: a delivery that happens during rush hour (rush_hour = 1) gets 8.47 more minutes added to its prediction than an identical delivery outside rush hour (rush_hour = 0), at any distance. That constant, 8.47, is the gap between the two bands you noticed in the scatter plot a moment ago.

R-squared is 0.9024: the model explains about 90% of how delivery_time varies across these 60 deliveries. That is a good fit, and it is also not what this lesson is about. Everything from here is about reading predictions, intervals, and effects out of a model you are handed, already fit, already checked.