MPG Predictor: Know Your Car's Fuel Efficiency in Seconds

Shreya Agarwal
Developing Data Products, Course Project

A Shiny app that predicts miles per gallon from weight, horsepower and transmission.

App: https://shreya-mpg-predictor.shinyapps.io/mpg-predictor/

The Problem

  • Fuel costs are a large part of owning a car.
  • Buyers compare cars on weight, engine power and gearbox, but rarely know how much each one costs them in MPG.
  • Spreadsheets and statistics are too much effort for a quick decision.

Our solution: move three sliders/buttons and get an instant MPG prediction, a 95% prediction interval and a chart comparing your car with 32 real cars.

The Model Behind the App

The server fits a linear regression on R's built-in mtcars data:

fit <- lm(mpg ~ wt + hp + am, data = mtcars)
round(coef(summary(fit))[, c("Estimate", "Pr(>|t|)")], 4)
            Estimate Pr(>|t|)
(Intercept)  34.0029   0.0000
wt           -2.8786   0.0036
hp           -0.0375   0.0005
am            2.0837   0.1413

It explains 84% of the variation in MPG. Every extra 1000 lbs costs about 2.9 MPG.

Example: What the App Computes

A 3000 lb, 150 hp manual car:

predict(fit, data.frame(wt = 3, hp = 150, am = 1),
        interval = "prediction")
       fit      lwr      upr
1 21.82905 16.37024 27.28786

This is exactly the calculation server.R runs whenever a user moves a slider.

plot of chunk unnamed-chunk-3

Try It Now

  1. Open https://shreya-mpg-predictor.shinyapps.io/mpg-predictor/
  2. Set weight and horsepower with the sliders, pick a transmission.
  3. Read your predicted MPG and its 95% range; see your car (red star) on the chart.