Car mileage calculator

Tarun Kaushik
December 21, 2014

Intoduction

Car milage calculator is a simple app. As evident from the name, it calculated, or rather estimates the milage of a car when some values are provided. Following are the details about car which the user has to enter.

  • Number of cylinders : takes value as 4, 6 or 8.
  • Number of forward gears: takes values as 3, 4 or 5.
  • Number of carburetors: takes interger values from 1 to 8.
  • Gross horsepower: a slider with values from 50 to 350.
  • Weight (lb): a slider with values from 1500 to 5500.
  • ¼ mile time: a slider with values from 15 to 25
  • Extra features: checkbox for Automatic transmission ans straight engine allignment.

Regression model

Based on the mtcars dataset provided in R a linear model was fit on miles per gallon, with the following model summary:

Equation

\( mpg=17.12752 \) \( -0.05103*cyl \) \( -0.01328*hp \) \( -2.71336*wt/1000 \) \( +0.63603*iqsec \) \( +0.18232*am \) \( +2.66026*vs \) \( +0.81521*gear \) \( -0.55570*carb \)

data(mtcars)
library(xtable)
names <- names(mtcars)
names <- setdiff(names,c("drat","disp"))
mtcars <- mtcars[,names]
fit <- lm(mpg~.,data=mtcars)
k <- xtable(summary(fit)$coefficients)
print(k, type="html")
Estimate Std. Error t value Pr(>|t|)
(Intercept) 17.13 16.50 1.04 0.31
cyl -0.05 0.95 -0.05 0.96
hp -0.01 0.02 -0.73 0.47
wt -2.71 1.17 -2.32 0.03
qsec 0.64 0.68 0.93 0.36
vs 0.18 2.04 0.09 0.93
am 2.66 1.98 1.34 0.19
gear 0.82 1.45 0.56 0.58
carb -0.56 0.59 -0.95 0.35

ui.R and server.R

ui.R

  • Takes in value in forms of numeric input
    • numeric input
    • slider
    • checkbox
  • Displays
    • Estimated MPG
    • MPG Density histogram showing estimated MPG as well.
    • All the values entered by the user
    • checkbox value, automatic transmission and engine type displayed separately, unlike input.

server.R

  • Has two functions
    • To extract type of transmission and engine type character strings used for output.
    • to extract numeric vaues of transmission and engine type used for prediction.
  • Renders value of each input to a corresponding value in output.
  • Estimates mpg using the input values using the linear model fit.
  • Has a density histogram for mpg, including calculated mpg as vertical live.

Output histogram

The app diplays all the values entered by the user, calculates estimated miles per gallon of the car and displays it along with the density graph like the one shwn below.

plot of chunk plot

.

.

.

.

.

.

.

Please use the app at https://tarunkaushik.shinyapps.io/Shiny/

Thanks you!