MPG Predictor

Pratham Nayak
October 27, 2026

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

The Problem

  • Fuel economy matters when buying or comparing cars.
  • Spec sheets are hard to interpret quickly.
  • Question: how many MPG should a car with these specs get?

Our answer: a simple, interactive prediction tool built with R and Shiny.

The Model

We fit a linear regression on the built-in mtcars data:

data(mtcars)
mtcars$am <- factor(mtcars$am, labels = c("Automatic", "Manual"))
fit <- lm(mpg ~ wt + hp + am, data = mtcars)
round(summary(fit)$coefficients, 3)
            Estimate Std. Error t value Pr(>|t|)
(Intercept)   34.003      2.643  12.867    0.000
wt            -2.879      0.905  -3.181    0.004
hp            -0.037      0.010  -3.902    0.001
amManual       2.084      1.376   1.514    0.141
cat("R-squared:", round(summary(fit)$r.squared, 3))
R-squared: 0.84

How the App Works

  1. Set weight and horsepower with sliders
  2. Pick Automatic or Manual
  3. Instantly see the predicted MPG and a 95% interval
  4. A plot shows your car against the real data

Example prediction for a 3,000 lb, 120 hp manual car:

new <- data.frame(wt = 3, hp = 120,
                  am = factor("Manual", levels = c("Automatic", "Manual")))
round(predict(fit, new, interval = "prediction"), 1)
  fit  lwr  upr
1  23 17.4 28.5

Try It