MPG Predictor

Your Name
October 28, 2026

A tiny Shiny app that predicts a car's fuel economy from its weight, horsepower and transmission.

The Problem

  • Buying a car? Fuel economy matters for your wallet and the planet.
  • Spec sheets are full of numbers; which ones actually drive MPG?
  • MPG Predictor answers in seconds: move three inputs, see the result.

The Data and Model

We use the built-in mtcars dataset (32 cars) and a linear model.

fit <- lm(mpg ~ wt + hp + factor(am), data = mtcars)
round(summary(fit)$r.squared, 3)
[1] 0.84
round(coef(fit), 3)
(Intercept)          wt          hp factor(am)1 
     34.003      -2.879      -0.037       2.084 

Weight and horsepower both reduce MPG; manual transmission adds a little.

A Sample Prediction

A 3,000 lb, 120 hp manual car:

newcar <- data.frame(wt = 3, hp = 120, am = 1)
predict(fit, newcar, interval = "prediction")
       fit      lwr      upr
1 22.95341 17.42961 28.47722

plot of chunk plot-figure/plot-1.png)

Try It Yourself