MPG prediction based on a car weight

Moh Hassan
05/21/2015

mtcars data set

This R presentation is for a shiny app that predicts MPG usage for a car using mtcars dataset. The user enters the weight of the car. Then the user selects whether the weight is in pounds or tonnes. A linear regression line is drawn to predicts the MPG of the car.

Slide With Code

summary(mtcars)
      mpg             cyl             disp             hp       
 Min.   :10.40   Min.   :4.000   Min.   : 71.1   Min.   : 52.0  
 1st Qu.:15.43   1st Qu.:4.000   1st Qu.:120.8   1st Qu.: 96.5  
 Median :19.20   Median :6.000   Median :196.3   Median :123.0  
 Mean   :20.09   Mean   :6.188   Mean   :230.7   Mean   :146.7  
 3rd Qu.:22.80   3rd Qu.:8.000   3rd Qu.:326.0   3rd Qu.:180.0  
 Max.   :33.90   Max.   :8.000   Max.   :472.0   Max.   :335.0  
      drat             wt             qsec             vs        
 Min.   :2.760   Min.   :1.513   Min.   :14.50   Min.   :0.0000  
 1st Qu.:3.080   1st Qu.:2.581   1st Qu.:16.89   1st Qu.:0.0000  
 Median :3.695   Median :3.325   Median :17.71   Median :0.0000  
 Mean   :3.597   Mean   :3.217   Mean   :17.85   Mean   :0.4375  
 3rd Qu.:3.920   3rd Qu.:3.610   3rd Qu.:18.90   3rd Qu.:1.0000  
 Max.   :4.930   Max.   :5.424   Max.   :22.90   Max.   :1.0000  
       am              gear            carb      
 Min.   :0.0000   Min.   :3.000   Min.   :1.000  
 1st Qu.:0.0000   1st Qu.:3.000   1st Qu.:2.000  
 Median :0.0000   Median :4.000   Median :2.000  
 Mean   :0.4062   Mean   :3.688   Mean   :2.812  
 3rd Qu.:1.0000   3rd Qu.:4.000   3rd Qu.:4.000  
 Max.   :1.0000   Max.   :5.000   Max.   :8.000  

Exploring relationship between a car weight and its mpg

plot(mtcars$wt, mtcars$mpg)
abline(lm(mpg~wt, data=mtcars), col="red")

plot of chunk unnamed-chunk-2

Exploring relation between number of cylinder and mpg

plot of chunk unnamed-chunk-3

Looking at correlation between mpg and car weight

cor(mtcars$wt, mtcars$mpg)
[1] -0.8676594
  • We can see that the relationship between mpg and wt is strong and negative. As the car weight increases by one unit of wt, the mpg decreases by 0.867

Correlation between mpg and number of cylinder

cor(mtcars$cyl, mtcars$mpg)
[1] -0.852162
  • We can see that the relationship between mpg and cyl is strong and negative. As the number of cylinder increases by 2 unit, the mpg decreases by 0.852

Using a linear regression to fit in the dataset

model <- glm(mpg~wt, data=mtcars)

summary(model)

Call:
glm(formula = mpg ~ wt, data = mtcars)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-4.5432  -2.3647  -0.1252   1.4096   6.8727  

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  37.2851     1.8776  19.858  < 2e-16 ***
wt           -5.3445     0.5591  -9.559 1.29e-10 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for gaussian family taken to be 9.277398)

    Null deviance: 1126.05  on 31  degrees of freedom
Residual deviance:  278.32  on 30  degrees of freedom
AIC: 166.03

Number of Fisher Scoring iterations: 2