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.
Our solution: enter three details, get an instant prediction with a confidence range.
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.
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)