-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter_3_labs_linear_regression.Rmd
176 lines (109 loc) · 4.12 KB
/
Chapter_3_labs_linear_regression.Rmd
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
---
title: "3. Linear Regression Labs"
author: "Russ Conte"
date: "8/7/2021"
output: html_document
---
## 3.6.1 Libraries
From the text:
The library() function is used to load libraries, or groups of functions and data sets that are not included in the base R distribution.
```{r, libraries}
library(MASS)
library(ISLR)
attach(Boston)
```
## 3.6.2 Simple Linear Regression
From the text:
The MASS library contains the Boston data set, which records medv (median house value) for 506 neighborhoods around Boston. We will seek to predict medv using 13 predictors such as rm (average number of rooms per house), age (average age of houses), and lstat (percent of households with low socioeconomic status).
```{r, predicting medv in the Boston data set}
names(Boston)
```
From the text:
We will start by using the lm() function to fit a simple linear regression
model, with medv as the response and lstat as the predictor. The basic lm() syntax is lm(y∼x,data), where y is the response, x is the predictor, and
data is the data set in which these two variables are kept.
```{r, first linear models}
lm.fit = lm(medv~lstat, data = Boston)
summary(lm.fit) # provides a summary of the regression model
names(lm.fit) # which other pieces of information are stored in lm.fit
```
## Confidence intervals for lm.fit
```{r confidence intervals for lm.fit}
confint(lm.fit)
```
## The Predict function
```{r the predict function}
predict(lm.fit, data.frame(lstat = c(5, 10, 15)), interval = "confidence")
```
## Plot our regression solution
```{r plot our simple regression solution}
plot(x = Boston$lstat, y = Boston$medv)
abline(lm.fit) # only runs if "Show output inline" is unchecked in preferences for RStudio
abline(lm.fit, lwd = 3) # a thicker line
abline(lm.fit, wd = 3, col = "red")
plot(Boston$lstat, Boston$medv, col = "red")
plot(lstat, medv, pch = 20)
plot(lstat, medv, pch = "+")
plot(1:20, 1:20, pch = 1:20)
```
## Viewing all four residual plots at once :)
```{r, viewing all four residual plots at one}
par(mfrow = c(2,2))
plot(lm.fit)
```
From the text: we can compute the residuals from a linear regression fit using the residuals() function.
```{r plot residuals}
plot(predict(lm.fit), residuals(lm.fit))
plot(predict(lm.fit), rstudent(lm.fit))
```
## which.max identifies the index of the largest element of a vector
## 3.6.3 Multipl Linear Regression
```{r first multiple linear regression examples}
lm.fit = lm(medv~lstat + age, data = Boston)
summary(lm.fit)
```
## Shorthand in R to include all variables in a regression formula
```{r shorthand in R to include all variables in a regression formula}
lm.fit = lm(medv~., data = Boston)
summary(lm.fit)
```
## Accessing individual components of the solution
from the text:
We can access the individual components of a summary object by name (type ?summary.lm to see what is available). Hence summary(lm.fit)$r.sq gives us the R2, and summary(lm.fit)$sigma gives us the RSE.
```{r, summary object information}
summary(lm.fit)$r.sq
summary(lm.fit)$sigma
```
## regression using all the variables except one
```{r}
lm.fit1 = lm(medv~.-age, data = Boston)
summary(lm.fit1)
```
## 3.6.4 Interaction Terms (from within the summary function - nice!!)
```{r}
summary(lm(medv~lstat*age, data = Boston))
```
## 3.6.5 Non-linear Transformations of the Predictors
from the text:
The lm() function can also accommodate non-linear transformations of the predictors. For instance, given a predictor X, we can create a predictor X2 using I(X^2).
```{r non-linear transformations of the predictors}
lm.fit2 = lm(medv~lstat + I(lstat^2))
summary(lm.fit2)
```
## We use the anova() function to further quantify the extent to which the quadric fit is superior to the linear fit
```{r}
lm.fit = lm(medv~lstat)
anova(lm.fit, lm.fit2)
```
## 3.6.6 Qualitative Predictors
We will attempt to predict sales of car seats in 400 locations, based on a number of predcitors
```{r}
attach(Carseats)
names(Carseats)
lm.fit = lm(Sales~. + Income:Advertising+Price:Age, data = Carseats)
summary(lm.fit)
```
## the Contrasts function returns the coding that R uses for the dummy variables
```{r}
contrasts(ShelveLoc)
```