-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_modelling_exercises.Rmd
486 lines (266 loc) · 9 KB
/
linear_modelling_exercises.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
---
title: "Introduction to linear modelling"
author: "Dr. Laurie Baker"
date: "07/12/2020"
output:
html_document:
theme: cosmo
highlight: haddock
toc: TRUE
toc_float: TRUE
number_sections: TRUE
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(message = FALSE, warning = FALSE)
```
# Learning Objectives
## Exploratiory Data Analysis
* What is the difference between a continuous and categorical variable?
* What is variation and covariation?
* Where does Exploratory Data Analysis fit in with analysis?
* How to use plots to explore variation in
* A continuous variable
* A categorical variable
* How to use plots to explore covariation between
* Two continuous variables
* A categorical and continuous variable.
## Model Basics
* What is a model family and fitted model?
* What is the difference between a response and an explanatory variable?
## Model Construction
* How to construct a linear model in R?
* What are the slope and intercept in a linear model?
* Picking out key information from the model summary table.
* How to extract specific parameters from the model object.
## Assessing Model Fit
* How to inspect model residuals to assess model fit?
* How to pick out key information from the table from a fitted model.
* How to use Adjusted R-squared and AIC to compare models.
# Import the libraries
```{r}
#install.packages(tidyverse)
#install.packages(broom)
#install.packages(GGally)
library(tidyverse)
library(broom)
library(GGally)
```
# Exploratory Data Analysis
## Read in the data
```{r salaries}
salaries <- read_csv("../data/faculty-data.csv")
salaries <- salaries %>%
mutate(department = as_factor(department))
```
## Explore the data
* What are the variables in our data set?
```{r}
head(salaries)
```
* How many departments are in the data? How many employees are in each department?
```{r}
summary(salaries)
```
Using `View()`, what does each row in the data.frame represent?
```{r}
```
* What's the difference between a continuous and a categorical (factor or discrete) variable?
**Hint:** Country is a categorical variable, Height is a continous variable
### Plotting the data
* Plotting a single continuous variable
```{r}
ggplot(data = salaries) +
geom_density(aes(salary), fill = "blue")
```
* Plotting a single categorical variable
```{r}
ggplot(data = salaries) +
geom_bar(aes(x = department, fill = department)) +
labs(x = "Department", y = "# Staff", fill = "Department")
```
* Relationships between two continuous variables
```{r}
ggplot(data = salaries) +
geom_point(aes(x = experience, y = salary))
```
* Relationships between a discrete and continous variable
```{r}
ggplot(data = salaries) +
geom_boxplot(aes(x = department, y = salary, color = department))
```
```{r}
ggplot(data = salaries) +
geom_violin(aes(x = department, y = salary, fill = department))
```
* Looking at the plot, which departments have the highest salaries? Do you think the departments are very different from one another?
# Constructing a linear model
## The relationship between salary and experience is the same across all departments.
Each department has the same starting salary and the same increase in salary with experience.
### Exercise:
* What are our response and explanatory variables in this case?
```{r}
m1 <- lm(salary ~ experience, data = salaries)
```
```{r}
summary(m1)
```
* **Intercept interpretation:** the starting salary is 62154 USD.
* **Slope interpretation:** with every year of experience an employee's salary increases by 683.5 USD.
* Overall the model is a poor fit, with only 0.01235 percent of the variation explained by the model as shown by the Adjusted R-squared.
* Plotting the fitted model
```{r}
m1_results <- broom::augment(m1)
head(m1_results)
```
## Residuals
What are residuals? How are they used to determine the best fit?
```{r}
ggplot(data = m1_results,
aes(x = experience, y = salary)) +
geom_point() +
geom_smooth(method = lm, se = FALSE) +
geom_segment(aes(xend = experience, yend = .fitted), color = "red", size = 0.3)
```
## The four assumptions of the linear model:
1. **Linearity** - The relationship between the explanatory variable ($X$) and the response variable ($Y$) must be linear.
2. **Normality**- The residual must be approximately normally distributed.
3. **Homoscedasticity**- The residuals are should have a constant variance.
4. **Independence**- There should be no relationship between the residuals and the response variable ($Y$).
```{r}
plot(m1)
```
## Each department has a different starting salary but the increase in salary with experience is the same.
Our data is "nested", that is we have groups within our data. We can add group, the department, as a third variable as the colour.
```{r}
ggplot(data = salaries) +
geom_point(aes(x = experience, y = salary, color = department)) +
labs(x="Experience", y="Salary") +
scale_colour_discrete('Department')
```
* How does the formula change if we think each group may have a different starting salary?
```{r}
m2 <- lm(salary ~ experience + department, data = salaries)
```
```{r}
m2_results <- broom::augment(m2)
ggplot(data = m2_results,
aes(x = experience, y = salary, colour = department)) +
geom_point() +
labs(x="Experience", y="Salary") +
scale_colour_discrete('Department') +
geom_line(aes(y = .fitted), size = 1)
```
* Let's take a look again at those residuals
```{r}
ggplot(data = m2_results,
aes(x = experience, y = salary, colour = department)) +
geom_point() +
labs(x="Experience", y="Salary") +
scale_colour_discrete('Department') +
geom_line(aes(y = .fitted), size = 1) +
geom_segment(aes(xend = experience, yend = .fitted, colour = department), size = 0.3, linetype = 2)
```
```{r}
summary(m2)
```
* Let's inspect the residual plots again
```{r}
plot(m2)
```
If someone works in a department for 6 years, will they have a higher salary if they are in the biology or sociology department?
* Let's extract the residuals using `coef`
```{r}
m2_coef <- coef(m2)
names(m2_coef)
```
```{r}
biology_ex6 <- m2_coef["(Intercept)"] + m2_coef["departmentbiology"] + 6*m2_coef["experience"]
sociology_ex6 <- m2_coef["(Intercept)"] + 6*m2_coef["experience"]
```
```{r}
m2_results <- broom::augment(m2)
ggplot(data = m2_results,
aes(x = experience, y = salary, colour = department)) +
geom_point() +
labs(x="Experience", y="Salary") +
scale_colour_discrete('Department') +
geom_line(aes(y = .fitted), size = 1) +
geom_point(aes(x = 6, y = biology_ex6), color = "black") +
geom_point(aes(x = 6, y = sociology_ex6), color = "black")
```
## Each department has different starting salaries and salaries increase at different rates.
```{r}
m3 <- lm(salary ~ experience*department, data = salaries)
m3_results <- broom::augment(m3)
```
```{r}
ggplot(data = m3_results,
aes(x = experience, y = salary, color = department)) +
geom_point() +
labs(x="Experience", y="Salary") +
scale_colour_discrete('Department') +
geom_line(aes(y = .fitted), size = 1)
```
* Plot with the residuals
```{r}
ggplot(data = m3_results,
aes(x = experience, y = salary, color = department)) +
geom_point() +
labs(x = "Experience", y = "Salary") +
scale_colour_discrete('Department') +
geom_line(aes(y = .fitted), size = 1) +
geom_segment(aes(xend = experience, yend = .fitted, colour = department), size = 0.3, linetype = 2)
```
* Summary of the model
```{r}
summary(m3)
```
### Exercises
* Create the four model diagnostic plots. What do you make of the plots?
```{r}
```
* What is the predicted salary with 7 years of experience in Biology?
```{r}
m3_coefs <- coef(m3)
```
```{r}
## Biology
biology_ex7 <- m3_coefs["(Intercept)"] + m3_coefs["departmentbiology"] + 7*(m3_coefs["experience:departmentbiology"] + m3_coefs["experience"])
```
### Exercise:
What is the predicted salary for someone with 7 years of experience Sociology?
```{r, eval = FALSE}
sociology_ex7
```
### Exercise:
Try plotting both salaries after 7 years on the graph.
```{r, eval = FALSE}
ggplot(data = m3_results,
aes(x = experience, y = salary, colour = `as.factor(department)`)) +
geom_point() +
labs(x="Experience", y="Salary") +
scale_colour_discrete('Department') +
geom_line(aes(y = .fitted), size = 1) +
geom_point(aes(x = ______, y = ______), color = "black") +
geom_point(aes(x = _____, y = ______), color = "black")
```
### Exercise
* Let's visualise the coefficients
```{r}
GGally::ggcoef(m3)
```
* Which department get the highest raises (biggest slope)?
```{r}
biology_slope <- m3_coefs["experience:departmentbiology"] + m3_coefs["experience"]
```
* Which department has the highest starting salary (biggest intercept)?
```{r, echo = FALSE}
biology_intercept <- m3_coefs["(Intercept)"] + m3_coefs["departmentbiology"]
```
# Comparing the models
* Which model has the best fit?
```{r}
broom::glance(m1)
broom::glance(m2)
broom::glance(m3)
```