forked from KimmoVehkalahti/IODS-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSupplement_Codes.Rmd
245 lines (165 loc) · 4.17 KB
/
Supplement_Codes.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
---
title: "supplement codes"
author: "Rong Guang"
date: '2022-11-11'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### read the data set
```{r}
library(tidyverse)
learn <- read_csv(file = "data/learning2014.csv")
```
### Code categorical data
```{r}
learn <- learn %>%
mutate(gender = gender %>%
factor() %>%
fct_recode("Female" = "F",
"Male" = "M"))
```
### generate cooks' distance for each obs
```{r}
fit2 <- learn %>%
lm(points ~ attitude + poly(age, 2, raw =T), data = .)
summary(fit2)
```
```{r}
cooksd <- cooks.distance(fit2)
cooksd <- data.frame(index = 1:length(cooksd), cooksd = cooksd)
```
### plot cooks's distance against index number
```{r}
plot(cooksd$index,
cooksd$cooksd,
pch="*", cex=1.5,
main="Influential Obs by Cooks distance") +
abline(h = 4*mean(cooksd$cooksd),
col="red")+
text(x=cooksd$index+4,
y=cooksd$cooksd,
labels= ifelse(cooksd$cooksd>4*mean(cooksd$cooksd, na.rm=T),
cooksd$index,""),
col="red")
```
### generate an index for original data set and merge cooks's distance into it
```{r}
learn <- learn %>% mutate(index = 1:nrow(learn))
learn <- left_join(learn, cooksd, by = "index")
```
### check the variables with large influence
```{r}
learn %>% filter(index %in% c(1,4,3,10,19,24,35,56,145)) %>% arrange(desc(cooksd))
```
###check the varible with large influence to residual normality and linearity
```{r}
plot(fit2, which = c(1,2))
```
It is found that the data points 145, 35, and 56 have large influence on residuals' normality and linearity as well as the predictor's estimated coefficient. I will remove these observations and fit the model again from a data-drive perspective.
```{r}
fit3 <- learn %>%
filter(!index %in% c(56, 145, 35)) %>%
lm(points ~ attitude + poly(age, 2, raw =T), data = .)
summary(fit3)
```
The new model with influential cases removed showed significant coefficient estimates for each predictor, as the previous model. More notably, the adjusted R-squared has increased to 0.303, indicating the model would explain 30.3% of variability of exam points. This is a remarkable increase from the previous model, which had a adjusted R-squared of 0.23.
```{r}
#Codes not in use
#learn <- learn %>% mutate(age.factor = age %>%
# cut(breaks =c(0,19,22,25,27,34,100)))
```
------
#simlating random coefficient model
```{r}
set.seed(1234) # this will allow you to exactly duplicate your result
Ngroups <- 100
NperGroup <- 10
N <- Ngroups * NperGroup
groups <- factor(rep(1:Ngroups, each = NperGroup))
u <- rnorm(Ngroups, sd = .5)
e <- rnorm(N, sd = .25)
x <- rnorm(N)
y <- 2 + .5 * x + u[groups] + e
d <- data.frame(x, y, groups)
d
```
```{r}
library(lme4)
library(ggplot2)
model <- lmer(y ~ x + (1|groups), data=d)
summary(model)
confint(model)
library(ggplot2)
ggplot(aes(x, y), data=d) +
geom_point()
```
```{r}
re <- ranef(model)$groups
qplot(x = `(Intercept)`, geom = 'density', xlim = c(-3, 3), data = re)
names(re)
```
```{r}
coef(model)$groups
```
```{r}
ranef(model)$groups
aa <- ranef(model)
```
```{r}
install.packages("merTools")
library(merTools)
predictInterval(model)
```
```{r}
REsim(model)
```
```{r}
plotREsim(REsim(model))
```
```{r}
model2 <- lmer(y ~ x + (1+ x|groups), data=d)
summary(model2)
```
```{r}
re <- ranef(model2)$groups
qplot(x = `(Intercept)`, geom = 'density', xlim = c(-3, 3), data = re)
lr <- lm(y~x)
```
```{r}
fixef(model)
ranef(model)$groups
```
```{r}
as.data.frame(t(apply(ranef(model)$groups, 1,function(x) fixef(model) + x)))
```
```{r}
apply(ranef(model)$groups, 1,function(x) fixef(model) + x)
```
```{r}
summary(model2)
```
```{r}
fixef(model2) + ranef(model2)$groups[1,]
```
```{r}
ranef(model2)$groups[1,]
```
```{r}
fixef(model2)
```
```{r}
t(apply(ranef(model2)$groups, 1,function(x) fixef(model2) + x))
df <- as.data.frame(t(apply(ranef(model2)$groups, 1,function(x) fixef(model2) + x)))
df
```
```{r}
pred_model2 <- melt(apply(df,1,function(x) x[1] + x[2]*0:10), value.name = "Reaction")
```
```{r}
df[2]*0:9
```
```{r}
apply(df,1,function(x) x[1] + x[2]*0:9)
```