September 27, 2026

The Problem

Car shoppers and students learning regression often ask a simple question:

“If a car weighs this much and has this many cylinders, what fuel economy should I expect?”

Answering this usually means opening R, loading a dataset, fitting a model, and writing prediction code by hand — not exactly beginner-friendly.

MPG Predictor solves this with a point-and-click Shiny app: pick a weight and a cylinder count, click a button, and instantly see the predicted MPG.

The Model

The app is powered by a linear regression trained on R’s built-in mtcars dataset (32 cars, 1973-74 models):

model <- lm(mpg ~ wt + factor(cyl), data = mtcars)
round(coef(model), 3)
##  (Intercept)           wt factor(cyl)6 factor(cyl)8 
##       33.991       -3.206       -4.256       -6.071

This embedded R code runs live every time this presentation is knitted, proving the model is real and reproducible — not just a static claim.

How It Works

The app has two simple inputs and one clear output:

  • Weight slider — choose the car’s weight (1,000 lbs units)
  • Cylinder radio buttons — choose 4, 6, or 8 cylinders
  • Predict MPG button — triggers the calculation
newcar <- data.frame(wt = 3.2, cyl = 6)
predict(model, newdata = newcar)
##        1 
## 19.47725

For a 3,200 lb, 6-cylinder car, the model predicts about 19.5 MPG — exactly the kind of number the app displays instantly in the browser.

The app’s “Prediction” tab shows this same chart, updated live for whatever weight and cylinder count the user selects.

Try It Yourself