-
Notifications
You must be signed in to change notification settings - Fork 62
/
05-regression.Rmd
329 lines (194 loc) · 8.93 KB
/
05-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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# (PART) Regression {-}
# Overview {#regression-overview}
**Chapter Status:** This chapter is currently undergoing massive rewrites. Some code is mostly for the use of current students. Plotting code is often not best practice for plotting, but is instead useful for understanding.
- **Supervised Learning**
- **Regression** (Numeric Response)
- What do we want? To make *predictions* on *unseen data*. (Predicting on data we already have is easy...) In other words, we want a *model* that **generalizes** well. That is, generalizes to unseen data.
- How we will do this? By controlling the **complexity** of the model to guard against **overfitting** and **underfitting**.
- Model Parameters
- Tuning Parameters
- Why does manipulating the model complexity accomplish this? Because there is a **bias-variance tradeoff**.
- How do we know if our model generalizes? By evaluating *metrics* on **test** data. We will only ever fit (train) models on training data. All analyses will begin with a test-train split. For regression tasks, our metric will be **RMSE**.
- Classification (Categorical Response) The next section.
![](images/regression.png)
**Regression** is a form of **supervised learning**. Supervised learning deals with problems where there are both an input and an output. Regression problems are the subset of supervised learning problems with a **numeric** output.
Often one of the biggest differences between *statistical learning*, *machine learning*, *artificial intelligence* are the names used to describe variables and methods.
- The **input** can be called: input vector, feature vector, or predictors. The elements of these would be an input, feature, or predictor. The individual features can be either numeric or categorical.
- The **output** may be called: output, response, outcome, or target. The response must be numeric.
As an aside, some textbooks and statisticians use the terms independent and dependent variables to describe the response and the predictors. However, this practice can be confusing as those terms have specific meanings in probability theory.
*Our goal is to find a rule, algorithm, or function which takes as input a feature vector, and outputs a response which is as close to the true value as possible.* We often write the true, unknown relationship between the input and output $f(\bf{x})$. The relationship (model) we learn (fit, train), based on data, is written $\hat{f}(\bf{x})$.
From a statistical learning point-of-view, we write,
$$
Y = f(\bf{x}) + \epsilon
$$
to indicate that the true response is a function of both the unknown relationship, as well as some unlearnable noise.
$$
\text{RMSE}(\hat{f}, \text{Data}) = \sqrt{\frac{1}{n}\displaystyle\sum_{i = 1}^{n}\left(y_i - \hat{f}(\bf{x}_i)\right)^2}
$$
$$
\text{RMSE}_{\text{Train}} = \text{RMSE}(\hat{f}, \text{Train Data}) = \sqrt{\frac{1}{n_{\text{Tr}}}\displaystyle\sum_{i \in \text{Train}}^{}\left(y_i - \hat{f}(\bf{x}_i)\right)^2}
$$
$$
\text{RMSE}_{\text{Test}} = \text{RMSE}(\hat{f}, \text{Test Data}) = \sqrt{\frac{1}{n_{\text{Te}}}\displaystyle\sum_{i \in \text{Test}}^{}\left(y_i - \hat{f}(\bf{x}_i)\right)^2}
$$
- TODO: RSS vs $R^2$ vs RMSE
**Code for Plotting from Class**
```{r}
## load packages
library(rpart)
library(FNN)
```
```{r}
# simulate data
## signal
f = function(x) {
x ^ 3
}
## define data generating process
get_sim_data = function(f, sample_size = 50) {
x = runif(n = sample_size, min = -1, max = 1)
y = rnorm(n = sample_size, mean = f(x), sd = 0.15)
data.frame(x, y)
}
## simulate training data
set.seed(42)
sim_trn_data = get_sim_data(f = f)
## simulate testing data
set.seed(3)
sim_tst_data = get_sim_data(f = f)
## create grid for plotting
x_grid = data.frame(x = seq(-1.5, 1.5, 0.001))
```
```{r}
# fit models
## tree models
tree_fit_l = rpart(y ~ x, data = sim_trn_data,
control = rpart.control(cp = 0.500, minsplit = 2))
tree_fit_m = rpart(y ~ x, data = sim_trn_data,
control = rpart.control(cp = 0.015, minsplit = 2))
tree_fit_h = rpart(y ~ x, data = sim_trn_data,
control = rpart.control(cp = 0.000, minsplit = 2))
## knn models
knn_fit_l = knn.reg(train = sim_trn_data["x"], y = sim_trn_data$y,
test = x_grid, k = 40)
knn_fit_m = knn.reg(train = sim_trn_data["x"], y = sim_trn_data$y,
test = x_grid, k = 5)
knn_fit_h = knn.reg(train = sim_trn_data["x"], y = sim_trn_data$y,
test = x_grid, k = 1)
## polynomial models
poly_fit_l = lm(y ~ poly(x, 1), data = sim_trn_data)
poly_fit_m = lm(y ~ poly(x, 3), data = sim_trn_data)
poly_fit_h = lm(y ~ poly(x, 22), data = sim_trn_data)
```
```{r}
# get predictions
## tree models
tree_fit_l_pred = predict(tree_fit_l, newdata = x_grid)
tree_fit_m_pred = predict(tree_fit_m, newdata = x_grid)
tree_fit_h_pred = predict(tree_fit_h, newdata = x_grid)
## knn models
knn_fit_l_pred = knn_fit_l$pred
knn_fit_m_pred = knn_fit_m$pred
knn_fit_h_pred = knn_fit_h$pred
## polynomial models
poly_fit_l_pred = predict(poly_fit_l, newdata = x_grid)
poly_fit_m_pred = predict(poly_fit_m, newdata = x_grid)
poly_fit_h_pred = predict(poly_fit_h, newdata = x_grid)
```
```{r fig.height = 4, fig.width = 10}
# plot fitted trees
par(mfrow = c(1, 3))
plot(y ~ x, data = sim_trn_data, col = "dodgerblue", pch = 20,
main = "Tree Model, cp = 0.5", cex = 1.5)
grid()
lines(x_grid$x, tree_fit_l_pred, col = "darkgrey", lwd = 2)
plot(y ~ x, data = sim_trn_data, col = "dodgerblue", pch = 20,
main = "Tree Model, cp = 0.015", cex = 1.5)
grid()
lines(x_grid$x, tree_fit_m_pred, col = "darkgrey", lwd = 2)
plot(y ~ x, data = sim_trn_data, col = "dodgerblue", pch = 20,
main = "Tree Model, cp = 0.0", cex = 1.5)
grid()
lines(x_grid$x, tree_fit_h_pred, col = "darkgrey", lwd = 2)
```
```{r fig.height = 4, fig.width = 10}
# plot fitted KNN
par(mfrow = c(1, 3))
plot(y ~ x, data = sim_trn_data, col = "dodgerblue", pch = 20,
main = "KNN, k = 40", cex = 1.5)
grid()
lines(x_grid$x, knn_fit_l_pred, col = "darkgrey", lwd = 2)
plot(y ~ x, data = sim_trn_data, col = "dodgerblue", pch = 20,
main = "KNN, k = 5", cex = 1.5)
grid()
lines(x_grid$x, knn_fit_m_pred, col = "darkgrey", lwd = 2)
plot(y ~ x, data = sim_trn_data, col = "dodgerblue", pch = 20,
main = "KNN, k = 1", cex = 1.5)
grid()
lines(x_grid$x, knn_fit_h_pred, col = "darkgrey", lwd = 2)
```
```{r fig.height = 4, fig.width = 10}
# plot fitted polynomials
par(mfrow = c(1, 3))
plot(y ~ x, data = sim_trn_data, col = "dodgerblue", pch = 20,
main = "Polynomial Model, degree = 1", cex = 1.5)
grid()
lines(x_grid$x, poly_fit_l_pred, col = "darkgrey", lwd = 2)
plot(y ~ x, data = sim_trn_data, col = "dodgerblue", pch = 20,
main = "Polynomial Model, degree = 3", cex = 1.5)
grid()
lines(x_grid$x, poly_fit_m_pred, col = "darkgrey", lwd = 2)
plot(y ~ x, data = sim_trn_data, col = "dodgerblue", pch = 20,
main = "Polynomial Model, degree = 22", cex = 1.5)
grid()
lines(x_grid$x, poly_fit_h_pred, col = "darkgrey", lwd = 2)
```
<!-- ## Regression Notation -->
<!-- - $\mathbf{X}$ = $n \times p$ data matrix -->
<!-- - $\mathbf{x}_j$ = column of data matrix. vector of length $n$. $n$ observations of predictors $j$. -->
<!-- - $X$ = random variable. vector of length $p$, which could be 1 (not bold) -->
<!-- - $x_i$ = predictor values for observation $i$. vector of length $p$. realization of random variable $X$. (not bold) -->
<!-- - $Y$ = random variable. scalar. (not bold) -->
<!-- - $y_i$ = response for observation $i$. scalar. realization of random variable $Y$. (not bold) -->
<!-- - $i$ is for observations, of which there are $n$ -->
<!-- - $j$ is for predictors (features), of which there are $p$ -->
<!-- - $\mathcal{D}_{\texttt{trn}}$ is training data -->
<!-- - $n_{\texttt{trn}}$ is size of training data -->
<!-- - $\mathcal{D}_{\texttt{tst}}$ is training data -->
<!-- - $n_{\texttt{tst}}$ is size of training data -->
<!-- $$ -->
<!-- (X, Y) \in \mathbb{R}^p \times \mathbb{R} -->
<!-- $$ -->
<!-- $$ -->
<!-- \mathcal{D} = (x_i, y_i) \in \mathbb{R}^p \times \mathbb{R} -->
<!-- $$ -->
<!-- $$ -->
<!-- x_i^T = [x_{i1}, x_{i2}, \ldots x_{ip}] -->
<!-- $$ -->
<!-- $$ -->
<!-- x^T = [x_{1}, x_{2}, \ldots x_{p}] -->
<!-- $$ -->
<!-- $$ -->
<!-- \mathbf{x}_j = \begin{bmatrix} x_{1j} \\ x_{2j} \\ \vdots\\ x_{nj} \end{bmatrix} -->
<!-- $$ -->
<!-- $$ -->
<!-- \mathbf{X} = [\mathbf{x}_1, \mathbf{x}_2, \ldots, \mathbf{x}_p] -->
<!-- $$ -->
<!-- $$ -->
<!-- \mathbf{X} = \begin{bmatrix} x_1^T \\ x_2^T \\ \vdots\\ x_n^T \end{bmatrix} -->
<!-- $$ -->
<!-- $$ -->
<!-- \mathbf{X} = [\mathbf{1}, \mathbf{x}_1, \mathbf{x}_2, \ldots, \mathbf{x}_p] -->
<!-- $$ -->
<!-- $$ -->
<!-- \mathbf{y} = \begin{bmatrix} y_1 \\ y_2 \\ \vdots\\ y_n \end{bmatrix} -->
<!-- $$ -->
<!-- $$ -->
<!-- \mathbb{E}[(Y - f(X))^2] -->
<!-- $$ -->
<!-- $$ -->
<!-- Y = f(X) + \epsilon -->
<!-- $$ -->
<!-- $$ -->
<!-- f(x) = \mathbb{E}(Y \mid X = x) -->
<!-- $$ -->