-
Notifications
You must be signed in to change notification settings - Fork 24
/
Cars_Multi_Linear Regression.r
57 lines (42 loc) · 1.39 KB
/
Cars_Multi_Linear Regression.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#Linear Regression model with Multiple parameters
#I have a dataset named Cars
model1 <- lm(MPG~., data = Cars) #Create a linear model
summary(model1) #Summary of the model
boxplot(Cars)
#Scatter Plot Matrix
pairs(Cars)
#Corelation Mtrix
cor(Cars)
#Multi-Collinearity
#Install Package car
install.packages("car")
library(car)
car::vif(model1) #Variance Inflation Factor. #IF the value of a parameter is greater than 20 then that parameter has a Multi-Collinearity Problem
#Diagnostic Plots: Residual Plot, QQ-Plot, Std. Residuals vs Fitted
plot(model1)
#Residual vs Regressors
residualPlots(model1)
#QQ Plots of studentized residuals
qqPlot(model1) #Quartile-Quartile Plot. #This will give the Outliers
#Deletion Diagnostics
influenceIndexPlot(model1) #Index plots of the Influence Measures. #This will give the Outliers that we should remove for better results
###Iteration 1
#Remove the 77th Observation
cars1 <- Cars[-77,]
#Create another model with new dataset i.e cars1 and execute all the above functions
model2 <- lm(MPG~., data = cars1)
car::vif(model2)
plot(model2)
residualPlots(model2)
qqPlot(model2)
influenceIndexPlot(model2)
###Iteration 2
#Remove the observation number you get in the qqPlots
cars2 <- Cars[c(-66,-77,-79,-80),]
model3 <- lm(MPG~., data = cars2)
car::vif(model3)
plot(model3)
residualPlots(model3)
qqPlot(model3)
influenceIndexPlot(model3)
#Continue this till we get no outliers