-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode.R
234 lines (133 loc) · 5.56 KB
/
code.R
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
# ---------------------------------------------------------------------------- #
### Load packages, import, clean, and view data, estimate baseline model
# ---------------------------------------------------------------------------- #
# install.packages("mice")
# install.packages("naniar")
library("mice")
library("naniar")
# import file from web, keep only select variables, name them
d <- read.csv('https://stats.idre.ucla.edu/wp-content/uploads/2016/02/hsbdemo.dat', header = F)
d <- d[,6:8]
names(d) <- c("read", "write", "math")
# view first 6 rows of dataset
head(d)
# descriptive statistics
summary(d)
# scatterplot of reading scores and math scores
plot(d$read, d$math, col = "darkgreen")
abline(lm(d$math ~ d$read))
# estimate a linear regression using full data
full_model <- lm(math ~ 0 + read + write, data = d)
summary(full_model)
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
### Missing Completely at Random (MCAR)
# ---------------------------------------------------------------------------- #
# create missing values as MCAR
set.seed(681)
d$na_math <- rbinom(n = nrow(d), size = 1, prob = 0.7) # 70% chance for each obs
head(d)
# two-sample t-test ?
t.test(math ~ na_math, data = d)
d$math <- ifelse(d$na_math == 1, NA, d$math) # make value NA if indicator == 1
head(d)
# Little's (1988) MCAR test
naniar::mcar_test(d[,c("math", "read", "write")])
# proportion of missing?
prop.table(table(d$na_math))["1"]
# estimate a linear regression modeling using missing data (listwise deletion)
missing_model <- lm(math ~ 0 + read + write, data = d)
summary(missing_model) # observations deleted due to missingness
# compare estimates from full model vs. listwise deletion model
coef(full_model)
coef(missing_model)
# start from beginning to create only 20% missing
d <- read.csv('https://stats.idre.ucla.edu/wp-content/uploads/2016/02/hsbdemo.dat', header = F)
d <- d[,6:8]
names(d) <- c("read", "write", "math")
set.seed(681)
d$na_math <- rbinom(n = nrow(d), size = 1, prob = 0.2)
head(d)
d$math <- ifelse(d$na_math == 1, NA, d$math)
head(d)
# porportion missing
prop.table(table(d$na_math))["1"]
# estimate missing model
missing_model <- lm(math ~ 0 + read + write, data = d)
# again compare estimates from full model vs. listwise deletion model
coef(full_model)
coef(missing_model)
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
### Missing at Random (MAR)
# ---------------------------------------------------------------------------- #
d <- read.csv('https://stats.idre.ucla.edu/wp-content/uploads/2016/02/hsbdemo.dat', header = F)
d <- d[,6:8]
names(d) <- c("read", "write", "math")
set.seed(681)
d$na_math <- rbinom(n = nrow(d), size = 1,
prob = ifelse(d$read > quantile(d$read, probs = .7),
1, 0))
# two-sample t-test ?
t.test(math ~ na_math, data = d)
# overwrite the income value as missing if the missing indicator == 1
d$math <- ifelse(d$na_math == 1, NA, d$math)
# view our data
d[100:110,]
# proportion of missing?
prop.table(table(d$na_math))["1"]
# Little's (1988) MCAR test
naniar::mcar_test(d[,c("math", "read", "write")])
# estimate missing model using listwise deletion
missing_model <- lm(math ~ 0 + read + write, data = d)
summary(missing_model) # observations deleted due to missingness
# compare estimates from both models
coef(full_model)
coef(missing_model)
# how does multiple imputation perform?
imps <- mice(d, m = 50, method = "pmm")
res <- with(imps, lm(math ~ 0 + read + write))
# estimates from (1) full model, (2) missing model, and (3) imputation model
coef(full_model)
coef(missing_model)
summary(pool(res))[,2]
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
### Missing Not at Random (MNAR)
# ---------------------------------------------------------------------------- #
d <- read.csv('https://stats.idre.ucla.edu/wp-content/uploads/2016/02/hsbdemo.dat', header = F)
d <- d[,6:8]
names(d) <- c("read", "write", "math")
set.seed(681)
d$na_math <- rbinom(n = nrow(d), size = 1,
prob = ifelse(d$math > quantile(d$math, probs = 0.75) &
d$math < quantile(d$math, probs = 0.95) |
d$math < quantile(d$math, probs = 0.1),
1, 0))
# two-sample t-test ?
t.test(math ~ na_math, data = d)
# overwrite the income value as missing if the missing indicator == 1
d$math <- ifelse(d$na_math == 1, NA, d$math)
# view our data
d[100:110,]
# proportion of missing?
prop.table(table(d$na_math))["1"]
# Little's (1988) MCAR test
naniar::mcar_test(d[,c("math", "read", "write")])
# estimate missing model using listwise deletion
missing_model <- lm(math ~ 0 + read + write, data = d)
summary(missing_model) # observations deleted due to missingness
# compare estimates from full model vs. missing model
coef(full_model)
coef(missing_model)
# how does multiple imputation perform?
imps <- mice(d, m = 50, method = "pmm")
res <- with(imps, lm(math ~ 0 + read + write))
# estimates from (1) full model, (2) missing model, and (3) imputation model
coef(full_model)
coef(missing_model)
summary(pool(res))[,2]
# ---------------------------------------------------------------------------- #
# # # #
# END #
# # # #