-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiple_imputation.R
169 lines (138 loc) · 3.54 KB
/
multiple_imputation.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
# Impute missing values ---------------------------------------------------
install.packages("mice") # you only need to do this once
install.packages("survival") # you only need to do this once
install.packages("dplyr") # you only need to do this once
library("mice")
library("survival")
library("dplyr")
# Nelson-Aalen estimator used in imputation model
na <- basehaz(coxph(Surv(sos_outtime_death, sos_out_death == "Yes") ~ 1,
data = rsdata, method = "breslow"
))
rsdata <- left_join(rsdata, na, by = c("sos_outtime_death" = "time"))
# OBS! All categorical variables used in imputation model must be of class factor and NOT character
str(rsdata)
# Alt 1. Only imputation variables in dataset -----------------------------
# Keep only variables used in the imputation model in the dataset used in the imputation model
rsdatausedforimp <- rsdata %>%
select(
shf_sex,
shf_age,
shf_indexyear,
shf_durationhf,
# labs
shf_ef,
shf_nyha,
shf_bpsys,
shf_heartrate,
shf_bmi,
shf_hb,
shf_gfrckdepi,
shf_ntprobnp,
# comorbs
shf_smoke,
shf_diabetes,
shf_af,
shf_hypertension,
sos_com_peripheralartery,
sos_com_stroketia,
sos_com_liver,
sos_com_cancer3y,
sos_com_copd,
# treatments
shf_rasarni,
shf_bbl,
shf_mra,
shf_diuretic,
shf_device,
shf_digoxin,
shf_asaantiplatelet,
shf_anticoagulantia,
shf_statin,
shf_nitrate,
# organizational
shf_followuphfunit,
shf_followuplocation,
# socec
scb_famtype,
scb_child,
scb_education,
scb_dispincome,
sos_outtime_death,
hazard,
sos_out_death
)
rsdataimp <- mice(rsdatausedforimp,
m = 10, maxit = 10,
printFlag = FALSE,
seed = 349872354
)
# Alt 2. Use prediction matrix and method ---------------------------------
# Specify in the prediction matrix and method input parameters which variables should be used in the imputation model
modvars <- c(
# demo
"shf_sex",
"shf_age",
"shf_indexyear",
"shf_durationhf",
# labs
"shf_ef",
"shf_nyha",
"shf_bpsys",
"shf_heartrate",
"shf_bmi",
"shf_hb",
"shf_gfrckdepi",
"shf_ntprobnp",
# comorbs
"shf_smoke",
"shf_diabetes",
"shf_af",
"shf_hypertension",
"sos_com_peripheralartery",
"sos_com_stroketia",
"sos_com_liver",
"sos_com_cancer3y",
"sos_com_copd",
# treatments
"shf_rasarni",
"shf_bbl",
"shf_mra",
"shf_diuretic",
"shf_device",
"shf_digoxin",
"shf_asaantiplatelet",
"shf_anticoagulantia",
"shf_statin",
"shf_nitrate",
# organizational
"shf_followuphfunit",
"shf_followuplocation",
# socec
"scb_famtype",
"scb_child",
"scb_education",
"scb_dispincome",
"hazard",
"sos_out_death"
)
noimpvars <- names(rsdata)[!names(rsdata) %in% modvars] # variables NOT included in the imputation model
ini <- mice(rsdata, maxit = 0, print = F) # to get initial predictors matrix and methods in place
# in order to not use the noimpvars in the model
pred <- ini$pred
pred[, noimpvars] <- 0
pred[noimpvars, ] <- 0 # redundant
meth <- ini$method
# change method used in imputation to prop odds model
meth[c("scb_education", "scb_dispincome", "shf_nyha")] <- "polr"
# in order not to impute the noimpvars
meth[noimpvars] <- ""
rsdataimp <- mice(rsdata,
m = 10, maxit = 10,
method = meth,
predictorMatrix = pred,
printFlag = FALSE,
seed = 349872354
)
# More ------------------------------------------------------------------
# for parallel computing (to speed up the process) see https://github.com/KIHeartFailure/resistantht/blob/main/munge/06-impute.R