Cars <- read.csv("E:\\New Volume\\DataScience Yogesh\\R _Codes\\Multilinear Regression\\Cars.csv") # choose the Cars.csv data set
View(Cars)
dim(Cars)
## [1] 81  5
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(HP)
##  int [1:81] 49 55 55 70 53 70 55 62 62 80 ...
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 ...
# 7. Find the correlation b/n Output (MPG) & (HP,VOL,SP)-Scatter plot
#windows()
plot(Cars)
pairs(Cars)

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
plot(HP,MPG)

# 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
# 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,data = Cars)
summary(mv) # Volume became significant
## 
## Call:
## lm(formula = MPG ~ VOL, data = Cars)
## 
## 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,data = Cars)
summary(mw) # Weight became significant
## 
## Call:
## lm(formula = MPG ~ WT, data = Cars)
## 
## 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,data = Cars)
summary(mvw) # Both became Insignificant
## 
## Call:
## lm(formula = MPG ~ VOL + WT, data = Cars)
## 
## 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 

library(car)
## Loading required package: carData
library(mvinfluence)
## Loading required package: heplots
## plotting Influential measures 
#windows()
a <-influencePlot(m1) # A user friendly representation of the above

# Regression after deleting the 77th observation, which is influential observation
m2 <- lm(MPG ~ VOL+SP+HP+WT,data=Cars[-77,])
summary(m2)
## 
## 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
m3 <- lm(MPG ~ VOL+ SP+ HP+ WT,data=Cars[-c(71,1,77),])
summary(m3)
## 
## Call:
## lm(formula = MPG ~ VOL + SP + HP + WT, data = Cars[-c(71, 1, 
##     77), ])
## 
## 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(m1)
##       VOL        HP        SP        WT 
## 638.80608  19.92659  20.00764 639.53382
## vif>10 then there exists collinearity among all the variables 

finalmodel <- lm(MPG ~ VOL + SP + HP,data=Cars)

summary(finalmodel)
## 
## Call:
## lm(formula = MPG ~ VOL + SP + HP, data = Cars)
## 
## 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
## Added Variable plot to check correlation b/n variables and o/p variable
#windows()
avPlots(m1)

avPlots(finalmodel)

## VIF and AV plot has given us an indication to delete "wt" variable
predict(finalmodel,newdata = Cars)
##        1        2        3        4        5        6        7        8 
## 43.59077 42.25679 42.25679 42.36150 42.26954 42.85590 42.25679 48.13221 
##        9       10       11       12       13       14       15       16 
## 48.13221 40.76616 41.43447 47.94095 39.86565 41.43447 41.67943 41.43447 
##       17       18       19       20       21       22       23       24 
## 41.27332 47.94095 41.27332 38.01722 38.66367 37.46001 38.11462 39.42854 
##       25       26       27       28       29       30       31       32 
## 40.09224 46.73898 35.69724 38.66367 38.09467 35.87282 35.04067 37.18309 
##       33       34       35       36       37       38       39       40 
## 37.32689 34.69006 37.40255 37.63925 39.28727 38.33839 38.33839 35.96910 
##       41       42       43       44       45       46       47       48 
## 34.13369 35.28731 37.34958 38.25375 35.95927 36.20872 34.23109 35.56226 
##       49       50       51       52       53       54       55       56 
## 36.95791 33.17920 33.17920 33.17920 29.38875 27.38159 28.31041 28.69214 
##       57       58       59       60       61       62       63       64 
## 35.78519 33.17920 35.43009 32.36991 29.73729 28.87233 25.07082 26.38923 
##       65       66       67       68       69       70       71       72 
## 25.85377 36.45744 25.91011 23.76768 24.42689 20.12047 27.91145 22.66872 
##       73       74       75       76       77       78       79       80 
## 23.16313 18.68892 23.79778 20.97037 21.23314 17.86773 26.21686 12.23755 
##       81 
## 15.59296

summary(Cars) windows() plot(test) ## Final model finalmodel1<-lm(MPG ~ log(VOL)+log(SP)+log(HP),data = Cars) summary(finalmodel1)

predict(finalmodel,newdata = Testcar)

windows()# Evaluate model LINE assumptions

avPlots(finalmodel) avPlots(model.car)

stepAIC(m1)