MPG Predictor

Vir Pratap Singh
October 27, 2026

Know a car's fuel economy before you drive it.

A Shiny app that predicts MPG from weight, horsepower and transmission.

The Problem

  • Spec sheets list weight, horsepower and transmission.
  • They rarely give a quick, realistic MPG estimate.
  • Buyers and students need a fast way to see how these specs affect fuel economy.

Our solution: enter three details, get an instant prediction with a confidence range.

The Model

A linear regression fitted on the built-in mtcars dataset (32 cars):

cars <- mtcars
cars$am <- factor(cars$am, labels = c("Automatic", "Manual"))
fit <- lm(mpg ~ wt + hp + am, data = cars)
round(summary(fit)$coefficients[, c(1, 4)], 3)
            Estimate Pr(>|t|)
(Intercept)   34.003    0.000
wt            -2.879    0.004
hp            -0.037    0.001
amManual       2.084    0.141

The model explains 84% of the variation in MPG.

Example Prediction

A 3,000 lb, 120 hp manual car:

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

Predicted: 23 MPG (95% range: 17.4 to 28.5)

plot of chunk unnamed-chunk-3

Try It Yourself

  1. Open the app and set weight and horsepower with the sliders.
  2. Choose Automatic or Manual transmission.
  3. Read the predicted MPG, updated instantly.
  • App: paste your shinyapps.io link here
  • Code: paste your GitHub link here
  • The apps Documentation tab explains every step.