Cars <- read.csv("E:\\New Volume\\DataScience Yogesh\\R _Codes\\Multilinear Regression\\Cars.csv") # choose the Cars.csv data set
View(Cars)
attach(Cars)
# Exploratory Data Analysis(60% of time)
# 1. Measures of Central Tendency
# 2. Measures of Dispersion
# 3. Third Moment Business decision
# 4. Fourth Moment Business decision
# 5. Probability distributions of variables
# 6. Graphical representations
# > Histogram,Box plot,Dot plot,Stem & Leaf plot,
# Bar plot
summary(Cars)
## HP MPG VOL SP
## Min. : 49.0 Min. :12.10 Min. : 50.00 Min. : 99.56
## 1st Qu.: 84.0 1st Qu.:27.86 1st Qu.: 89.00 1st Qu.:113.83
## Median :100.0 Median :35.15 Median :101.00 Median :118.21
## Mean :117.5 Mean :34.42 Mean : 98.77 Mean :121.54
## 3rd Qu.:140.0 3rd Qu.:39.53 3rd Qu.:113.00 3rd Qu.:126.40
## Max. :322.0 Max. :53.70 Max. :160.00 Max. :169.60
## WT
## Min. :15.71
## 1st Qu.:29.59
## Median :32.73
## Mean :32.41
## 3rd Qu.:37.39
## Max. :53.00
str(Cars)
## 'data.frame': 81 obs. of 5 variables:
## $ HP : int 49 55 55 70 53 70 55 62 62 80 ...
## $ MPG: num 53.7 50 50 45.7 50.5 ...
## $ VOL: int 89 92 92 92 92 89 92 50 50 94 ...
## $ SP : num 104 105 105 113 104 ...
## $ WT : num 28.8 30.5 30.2 30.6 29.9 ...
class(Cars)
## [1] "data.frame"
# 7. Find the correlation b/n Output (MPG) & (HP,VOL,SP)-Scatter plot
windows()
plot(Cars)

# 8. Correlation Coefficient matrix - Strength & Direction of Correlation
cor(Cars)
## HP MPG VOL SP WT
## HP 1.00000000 -0.7250383 0.07745947 0.9738481 0.07651307
## MPG -0.72503835 1.0000000 -0.52905658 -0.6871246 -0.52675909
## VOL 0.07745947 -0.5290566 1.00000000 0.1021700 0.99920308
## SP 0.97384807 -0.6871246 0.10217001 1.0000000 0.10243919
## WT 0.07651307 -0.5267591 0.99920308 0.1024392 1.00000000
#B1X1 + B2X2 + B3X3 +..... BnXn
# The Linear Model of interest
m1 <- lm(MPG ~ VOL + HP+ SP + WT,data = Cars)
summary(m1)
##
## Call:
## lm(formula = MPG ~ VOL + HP + SP + WT, data = Cars)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.6320 -2.9944 -0.3705 2.2149 15.6179
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 30.67734 14.90030 2.059 0.0429 *
## VOL -0.33605 0.56864 -0.591 0.5563
## HP -0.20544 0.03922 -5.239 1.4e-06 ***
## SP 0.39563 0.15826 2.500 0.0146 *
## WT 0.40057 1.69346 0.237 0.8136
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.488 on 76 degrees of freedom
## Multiple R-squared: 0.7705, Adjusted R-squared: 0.7585
## F-statistic: 63.8 on 4 and 76 DF, p-value: < 2.2e-16
# Prediction based on only Volume
mv <- lm(MPG ~ VOL)
summary(mv) # Volume became significant
##
## Call:
## lm(formula = MPG ~ VOL)
##
## Residuals:
## Min 1Q Median 3Q Max
## -25.3074 -5.2026 0.1902 5.4536 17.1632
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 55.81709 3.95696 14.106 < 2e-16 ***
## VOL -0.21662 0.03909 -5.541 3.82e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.798 on 79 degrees of freedom
## Multiple R-squared: 0.2799, Adjusted R-squared: 0.2708
## F-statistic: 30.71 on 1 and 79 DF, p-value: 3.823e-07
# Prediction based on only Weight
mw <- lm(MPG ~ WT)
summary(mw) # Weight became significant
##
## Call:
## lm(formula = MPG ~ WT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -25.3933 -5.4377 0.2738 5.2951 16.9351
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 55.2296 3.8761 14.249 < 2e-16 ***
## WT -0.6420 0.1165 -5.508 4.38e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.811 on 79 degrees of freedom
## Multiple R-squared: 0.2775, Adjusted R-squared: 0.2683
## F-statistic: 30.34 on 1 and 79 DF, p-value: 4.383e-07
# Prediction based on Volume and Weight
mvw <- lm(MPG ~ VOL + WT)
summary(mvw) # Both became Insignificant
##
## Call:
## lm(formula = MPG ~ VOL + WT)
##
## Residuals:
## Min 1Q Median 3Q Max
## -24.9939 -4.9460 0.0028 5.3905 17.6972
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 56.8847 4.5342 12.55 <2e-16 ***
## VOL -0.6983 0.9841 -0.71 0.480
## WT 1.4349 2.9291 0.49 0.626
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 7.835 on 78 degrees of freedom
## Multiple R-squared: 0.2821, Adjusted R-squared: 0.2637
## F-statistic: 15.33 on 2 and 78 DF, p-value: 2.434e-06
# It is Better to delete influential observations rather than deleting entire column which is
# costliest process
# Deletion Diagnostics for identifying influential observations
#influence.measures(m1)
library(car)
## Loading required package: carData
## plotting Influential measures
windows()
influenceIndexPlot(m1) # index plots for infuence measures

influencePlot(m1) # A user friendly representation of the above

## StudRes Hat CookD
## 1 2.421762 0.05200781 0.06047977
## 71 -2.100131 0.22253511 0.24164401
## 77 4.503603 0.25138750 1.08651940
# Regression after deleting the 77th observation, which is influential observation
model.car1 <- lm(MPG ~ VOL + SP + HP + WT,data = Cars[-77,])
summary(model.car1)
##
## Call:
## lm(formula = MPG ~ VOL + SP + HP + WT, data = Cars[-77, ])
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.3943 -2.3555 -0.5913 1.8978 12.0184
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 27.82675 13.32251 2.089 0.04013 *
## VOL -0.18546 0.50895 -0.364 0.71659
## SP 0.41189 0.14139 2.913 0.00471 **
## HP -0.22664 0.03534 -6.413 1.14e-08 ***
## WT 0.03754 1.51458 0.025 0.98029
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.008 on 75 degrees of freedom
## Multiple R-squared: 0.8192, Adjusted R-squared: 0.8096
## F-statistic: 84.96 on 4 and 75 DF, p-value: < 2.2e-16
# Regression after deleting the 77th & 71st Observations
model.car2 <- lm(MPG ~ VOL + SP + HP + WT,data=Cars[-c(71,77,1),])
summary(model.car2)
##
## Call:
## lm(formula = MPG ~ VOL + SP + HP + WT, data = Cars[-c(71, 77,
## 1), ])
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.7300 -2.5391 -0.3696 2.1482 10.7151
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 24.82062 13.01740 1.907 0.06049 .
## VOL -0.31823 0.49668 -0.641 0.52372
## SP 0.44618 0.13881 3.214 0.00195 **
## HP -0.22688 0.03413 -6.647 4.67e-09 ***
## WT 0.40617 1.48045 0.274 0.78459
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.859 on 73 degrees of freedom
## Multiple R-squared: 0.821, Adjusted R-squared: 0.8112
## F-statistic: 83.72 on 4 and 73 DF, p-value: < 2.2e-16
## Variance Inflation factor to check collinearity b/n variables
#vif or MASS Packages
vif(m1)
## VOL HP SP WT
## 638.80608 19.92659 20.00764 639.53382
## vif>10 then there exists collinearity among all the variables
## Added Variable plot to check correlation b/n variables and o/p variable
windows()
avPlots(m1)

## VIF and AV plot has given us an indication to delete "wt" variable
## Final model
finalmodel <- lm(MPG ~ VOL + SP+ HP)
summary(finalmodel)
##
## Call:
## lm(formula = MPG ~ VOL + SP + HP)
##
## Residuals:
## Min 1Q Median 3Q Max
## -8.5869 -2.8942 -0.3157 2.1291 15.6669
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 29.92339 14.46589 2.069 0.0419 *
## VOL -0.20165 0.02259 -8.928 1.65e-13 ***
## SP 0.40066 0.15586 2.571 0.0121 *
## HP -0.20670 0.03861 -5.353 8.64e-07 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.46 on 77 degrees of freedom
## Multiple R-squared: 0.7704, Adjusted R-squared: 0.7614
## F-statistic: 86.11 on 3 and 77 DF, p-value: < 2.2e-16
library(MASS)
stepAIC(m1)
## Start: AIC=248.06
## MPG ~ VOL + HP + SP + WT
##
## Df Sum of Sq RSS AIC
## - WT 1 1.13 1531.8 246.12
## - VOL 1 7.03 1537.7 246.43
## <none> 1530.7 248.06
## - SP 1 125.87 1656.5 252.46
## - HP 1 552.74 2083.4 271.03
##
## Step: AIC=246.12
## MPG ~ VOL + HP + SP
##
## Df Sum of Sq RSS AIC
## <none> 1531.8 246.12
## - SP 1 131.46 1663.3 250.79
## - HP 1 570.08 2101.9 269.75
## - VOL 1 1585.81 3117.6 301.68
##
## Call:
## lm(formula = MPG ~ VOL + HP + SP, data = Cars)
##
## Coefficients:
## (Intercept) VOL HP SP
## 29.9234 -0.2017 -0.2067 0.4007