-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChapter 03 Lab Linear Regression.R
149 lines (115 loc) · 3.18 KB
/
Chapter 03 Lab 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Chapter 3 Lab: Linear Regression
library(MASS)
library(ISLR)
# Simple Linear Regression
# fix(Boston)
str(Boston)
names(Boston)
# plot it first
plot(medv ~ lstat, Boston)
# attach(Boston)
# lm.fit <- lm(medv~lstat)
lm.fit <- lm(medv~lstat,data=Boston)
# attach(Boston)
# lm.fit <- lm(medv~lstat)
lm.fit
summary(lm.fit)
names(lm.fit)
coef(lm.fit)
confint(lm.fit)
# new predictions
predict(lm.fit,data.frame(lstat=(c(5,10,15))), interval="confidence")
predict(lm.fit,data.frame(lstat=(c(5,10,15))), interval="prediction")
# plot the fitted line for linear model
library(ggplot2)
# one method
qplot(data=Boston, x=lstat,y=medv)+
geom_smooth(method='lm', se=0)
# alternatively
qplot(data=Boston, x=lstat, y=medv)+
geom_abline(slope = coef(lm.fit)[[2]], intercept = coef(lm.fit)[[1]], color='blue')
# abline(lm.fit),
# abline(lm.fit,lwd=3),
# abline(lm.fit,lwd=3,col="red")
# plot(lstat,medv,col="red")
# plot(lstat,medv,pch=20)
# plot(lstat,medv,pch="+")
# plot(1:20,1:20,pch=1:20)
# Multiple Linear Regression
lm.fit <- lm(medv~lstat+age,data=Boston)
summary(lm.fit)
lm.fit <- lm(medv~.,data=Boston)
summary(lm.fit)
# Leverge and outliers
# par(mfrow=c(2,2))
plot(lm.fit)
plot(predict(lm.fit), residuals(lm.fit))
plot(predict(lm.fit), rstudent(lm.fit))
plot(hatvalues(lm.fit))
which.max(hatvalues(lm.fit))
dev.off()
# value inflation factor
library(car)
vif(lm.fit)
# steps to remove non-significant variables
lm.fit1 <- lm(medv~.-age,data=Boston)
summary(lm.fit1)
lm.fit1 <- update(lm.fit, ~.-age - indus)
summary(lm.fit1) # all coefs are significant
# Interaction Terms
summary(lm(medv~lstat*age,data=Boston))
# Non-linear Transformations of the Predictors
lm.fit2 <- lm(medv~lstat+I(lstat^2), data=Boston)
summary(lm.fit2)
# reduction fit evaluation
lm.fit <- lm(medv~lstat, data=Boston)
anova(lm.fit,lm.fit2)
# show all the metrics
par(mfrow=c(2,2))
plot(lm.fit2)
# plot the regression line 'quadratic' with easy R's own plot func
par(mfrow=c(1,1))
plot(medv ~ lstat, data = Boston)
points(Boston$lstat, fitted(lm.fit2), col='red', pch=20)
# lstat, power to five
lm.fit5 <- lm(medv~poly(lstat,5), data=Boston)
summary(lm.fit5)
points(Boston$lstat, fitted(lm.fit5), col='blue', pch=20)
# room transformed alone with log()
summary(lm(medv~log(rm),data=Boston))
# Qualitative Predictors
# fix(Carseats)
names(Carseats)
str(Carseats)
lm.fit <- lm(Sales~.+Income:Advertising+Price:Age,data=Carseats)
summary(lm.fit)
# attach(Carseats)
contrasts(Carseats$ShelveLoc)
# Writing Functions
regplot <- function(x,y){
fit <- lm(y ~ x)
plot(x, y)
abline(fit, col='blue')
}
# plot Price ~ Sales from Carseats data set using regplot()
with(data=Carseats,
regplot(Sales, Price)
)
within(data=Carseats,
regplot(Price, Sales)
)
# somethow it is not working...
regplot2 <- function(x,y, data4lm){
fit <- lm(y ~ x, data=data4lm)
plot(data4lm$x, data4lm$y)
abline(fit, col='blue')
}
regplot2(Sales, Price, Carseats)
# LoadLibraries
LoadLibraries <- function(){
library(ISLR)
library(MASS)
print("The libraries have been loaded.")
}
LoadLibraries
LoadLibraries()