-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain_LM_Analysis.Rmd
353 lines (230 loc) · 9.02 KB
/
Main_LM_Analysis.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
---
title: "Main Linear Model Analysis"
author: "Sho"
date: "11/26/2021"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```
# Loading in Data
Loading as done by Seokjun.
```{r}
data <- read.csv("MH_survey_only_higher_index.csv",
na.strings = "NA")
names(data)
#only relevant for Seokjun's PC <- thanks :D
names(data)[1] <- "gender"
data <- data[, -c(17:22)]
# Assigning factors
data$gender <- factor(data$gender)
data$age_group <- factor(data$age_group)
data$country_lockdown <- factor(data$country_lockdown)
data$marital <- factor(data$marital)
data$smoking <- factor(data$smoking)
data$fivfruitveg <- factor(data$fivfruitveg)
data$shielded <- factor(data$shielded)
data$week_soc_distancing <- factor(data$week_soc_distancing)
data$athlete <- factor(data$athlete)
```
# Step-wise Regression
Here we conduct the step-wise regression using step.
Note for Seokjun: `step` actually terminates at a different point if you take out missing data.
This is an issue.
The final model is pretty different than what we had before.
```{r}
data_without_na <- data[complete.cases(data), ]
dim(data_without_na)
model_full <- lm(MHC_SF_OVERALL ~ ., data = data[complete.cases(data), ])
summary(model_full)
step(model_full, direction = "backward")
```
This is the model that results through the `step` function.
```{r}
model_step9 = lm(formula = MHC_SF_OVERALL ~ gender + age_group + week_soc_distancing +
athlete + HADS_OVERALL + RES_TOTAL + LONE_TOTAL, data = data[complete.cases(data), ])
model_step9 %>% summary()
```
# Removing variables with Collinearity Issues: RES & HADS
Determine which variable to remove (due to collinearity issue: RES or HADS):
```{r}
no_HADS = lm(formula = MHC_SF_OVERALL ~ gender + age_group + week_soc_distancing +
athlete+ RES_TOTAL + LONE_TOTAL, data = data[complete.cases(data), ])
no_RES = lm(formula = MHC_SF_OVERALL ~ gender + age_group + week_soc_distancing +
athlete + HADS_OVERALL + LONE_TOTAL, data = data[complete.cases(data), ])
paste0("AIC without HADS: ", c(no_HADS %>% extractAIC())[2] %>% round(2))
paste0("AIC without RES: ", c(no_RES %>% extractAIC())[2] %>% round(2))
```
AIC is significantly lower without RES. For the final model, we will remove RES.
# Final Model
```{r}
final_model = lm(formula = MHC_SF_OVERALL ~ gender + age_group + week_soc_distancing +
athlete + HADS_OVERALL + LONE_TOTAL, data = data[complete.cases(data), ])
final_model %>% summary()
```
# collapse trials
```{r}
as.numeric(data$age)
age_cut <- ifelse(as.numeric(data$age_group) >= 6, 1, 0)
data$age_cut <- factor(age_cut)
soc_dist_cut <- ifelse(as.numeric(data$week_soc_distancing)-1 >= 7, 1, 0) #warning! added 1 to original factor value
data$soc_dist_cut <- factor(soc_dist_cut)
final_model_cut = lm(formula = MHC_SF_OVERALL ~ gender + age_cut + soc_dist_cut +
athlete + HADS_OVERALL + LONE_TOTAL, data = data[complete.cases(data), ])
summary(final_model_cut)
```
```{r fig.width=8, fig.height=5}
par(mfrow = c(2, 2))
plot(final_model)
```
Residual plots look almost perfect! We will look at the outliers below.
Potential homoskedasticity?
# Testing All Interactions
Here we run the F-Test on all secondary interactions. It returned a small F statistic and high p-value.
This is good news! Less LaTeX work.
```{r}
model_interaction = lm(formula = MHC_SF_OVERALL ~ (gender + age_group + week_soc_distancing +
athlete + HADS_OVERALL + + LONE_TOTAL)^2, data = data[complete.cases(data), ])
anova(final_model, model_interaction)
```
# F-Test for Model Significance + T-test for continuous/two-category variables
This can be done just via the `summary` function.
```{r}
final_model %>% summary()
anova(final_model) #type 1
# explanation: https://stats.stackexchange.com/questions/20452/how-to-interpret-type-i-type-ii-and-type-iii-anova-and-manova/20455#20455
library(car)
Anova(final_model) #type 2
```
```{r}
w.o.gender = lm(formula = MHC_SF_OVERALL ~ age_group + week_soc_distancing +
athlete + HADS_OVERALL + LONE_TOTAL, data = data[complete.cases(data), ])
anova(w.o.gender, final_model)
```
Gender & Athlete are both significant at 0.05.
HADS, LONE are also significant. We will now do F-test on `age_group` and `week_soc_distancing`.
First with age_group
```{r}
w.o.age_group = lm(formula = MHC_SF_OVERALL ~ gender + week_soc_distancing +
athlete + HADS_OVERALL + LONE_TOTAL, data = data[complete.cases(data), ])
anova(w.o.age_group, final_model)
```
Significant at 0.05
Now with `week_soc_distancing`:
```{r}
w.o.week_soc_dist = lm(formula = MHC_SF_OVERALL ~ gender + age_group +
athlete + HADS_OVERALL + LONE_TOTAL, data = data[complete.cases(data), ])
anova(final_model, w.o.week_soc_dist)
```
Significant at 0.001!
# visualize fitting result
```{r fig.height=5, fig.width=8}
library(car)
# ?avPlots
avPlots(final_model, terms=~HADS_OVERALL + LONE_TOTAL)
```
# diagnosis !
# Outlier Analysis
1. see residual
```{r}
par(mfrow = c(2, 2))
plot(final_model)
par(mfrow = c(1, 1))
plot(final_model, which = 1) #delete 364? (index is 321, after deleting NAs)
which.max(final_model$residuals); final_model$residuals[321]
data[364, ]
par(mfrow = c(3, 2))
for(i in (names(final_model$model)[-1])) {
print(i)
boxplot(data[, i], xlab = i)
points(1, data[364, i], pch = 19, col = "red", cex = 2)
}
```
```{r}
par(mfrow = c(1, 1))
# vs fitted
plot(final_model$residuals ~ final_model$fitted.values)
abline(h = 0)
which.max(final_model$residuals)
final_model$residuals[321] #364 before deleting NA, 321 after deleting NA
#vs predictors
n_lab_to_boxplot <- function(factor_var, y=1) {
lev <- levels(factor_var)
for(i in seq_along(lev)) {
text(i, y, paste("n=", sum(factor_var == lev[i]), sep = ""))
}
}
plot(final_model$residuals ~ data_without_na$gender)
plot(final_model$residuals ~ data_without_na$age_group)
plot(final_model$residuals ~ data_without_na$week_soc_distancing) # <- hmm. but boxes are similar
n_lab_to_boxplot(data_without_na$week_soc_distancing, y = 25) #we can guess what makes the problem
plot(final_model$residuals ~ data_without_na$athlete)
plot(final_model$residuals ~ data_without_na$HADS_OVERALL)
abline(h = 0)
plot(final_model$residuals ~ data_without_na$LONE_TOTAL)
abline(h = 0)
```
2. see leverage
```{r}
par(mfrow = c(1, 1)); plot(final_model, which = 4)
par(mfrow = c(2, 2)); plot(final_model)
final_model$model[c(296, 341, 405), ] #not these
final_model$model[c(266, 301, 354), ] #but these
#or
data[c(296, 341, 405),]
par(mfrow = c(4,4))
for(i in 1:16){
boxplot(data[, i], xlab = colnames(data)[i])
points(1, data[296, i], pch = 19, col = "red", cex = 2)
points(1, data[341, i], pch = 19, col = "blue", cex = 2)
points(1, data[405, i], pch = 19, col = "green", cex = 2)
}
```
The outlier point is unusual in terms of weeks social distancing and the maximum LONE score (very lonely).
No Cook's distance is above 1 (or 0.5) so even the large points (296, 341, and 405) are not concerning.
Compare this to the overall data:
```{r}
final_model$model %>% summary()
```
Comment: not obvious to me why these are outliers (but it is hard when there are so many variables).
Seokjun: see boxplots above (they seem ok)
comment (nov 27 3:20)
issues:
1. delete high-residual point? (364th)
2. I think variance problem seems not big. (see above residual plots, verse predictors)
3. visualize fitting result
more partial-regression plot?
(optional)
7. if we want: make prediction example?
Sho: Not yet
nov 27: later (if our paper seems too short)
comment (nov 27 1:30 am):
1. delete HAD or RES (because of Collinearity) <- we can do this right after 'step' function result
Sho: Done, compared using AIC. Chose HAD.
2. Draw diagnosis plot (residual, normal qq, leverage, ... using 'plot(lm_fit_object)',
Sho: Done.
Delete outlier/other annoying points.
Sho: Did not do yet .
Please make a figure to show an outlier if it exists.
Sho: Done?
And, if there are variance problem (heteroskedasticity or ...), stop and rest (let's discuss together)
3. re-fit lm (after deleting things in 1 and 2)
Sho: Did not remove any outliers but removed HADS to create `final_model`.
4. basic test: F test for model significance(first!) ->
t test for continuous variable, model comparison F-test (for each categorical variable group)
Sho: Done.
5. visualize fitting result (if we can. if it is too high dimention, just make a table). See R^2/AIC/BIC,...
Sho: Tried...
6. residual analysis (same as 2)
Sho: not yet?
7. if we want: make prediction example?
Sho: Not yet
nov 27: later (if our paper seems too short)
8. Tested all interaction terms (secondary) <---- Added
Sho: no terms were significant.
(I think) By Tuesday, we don't have to finish writing everything, but
just 4,5,6(+7) steps table and figure is enough.
Let's write together. Don't write it all alone! I'll help you
And, please give me an idea to visualize the Lasso fit.
After making 2 plots, I don't know what to do.