MPG Predictor: A Reproducible Pitch

Zekuan Zhu

九月 21, 2026

The problem

Anyone shopping for a car, or teaching a stats class, often wants a quick, intuitive answer to: “How much does weight affect fuel economy?”

Digging through a raw dataset and fitting a model in R isn’t something a non-technical user wants to do just to get a ballpark mpg estimate.

MPG Predictor is a Shiny app that answers this with one slider drag — no R knowledge required.

How it works

The app fits a single linear model once, at startup, on R’s built-in mtcars dataset (32 cars, 1974 Motor Trend road test):

fit <- lm(mpg ~ wt, data = mtcars)
coef(summary(fit))
##              Estimate Std. Error   t value     Pr(>|t|)
## (Intercept) 37.285126   1.877627 19.857575 8.241799e-19
## wt          -5.344472   0.559101 -9.559044 1.293959e-10

When the user drags the weight slider, server.R calls predict() on this fitted model to get the expected mpg and a 95% prediction interval — this is the “reactive output” the app displays.

See it in action

This is exactly what the app’s plotting logic does — draw the mtcars scatterplot, overlay the fitted line, and highlight one prediction (a 3,500 lb car, predicted 18.6 mpg):

example_wt <- 3.5
pred <- predict(fit, data.frame(wt = example_wt))

plot(mtcars$wt, mtcars$mpg,
     xlab = "Weight (1000 lbs)", ylab = "Miles per gallon",
     main = "mtcars: mpg vs. weight", pch = 19, col = "steelblue")
abline(fit, col = "darkgray", lwd = 2)
points(example_wt, pred, col = "red", pch = 19, cex = 2)

Thanks for watching!