-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtutorialMatching20190218.R
More file actions
294 lines (221 loc) · 9.5 KB
/
Copy pathtutorialMatching20190218.R
File metadata and controls
294 lines (221 loc) · 9.5 KB
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
# required packages
library("tidyverse")
library("Hmisc")
library("zoo")
library("Matching")
library("tableone")
library("pROC")
library("skimr")
# read rhc dataset ------------------------------------------------------------
# YOU NEED TO POINT R TO THE "rhcDataset.RDS" FILE ON YOUR MACHINE
rhc <- readRDS("rhcDataset.RDS")
rhc
# View(rhc)
# explore the rhc dataset -----------------------------------------------------
# identify exposure and outcome variables
# exposure = swang1; outcome = dth30
table(rhc$swang1, useNA = "ifany")
table(rhc$dth30, useNA = "ifany")
table(rhc$swang1, rhc$dth30, useNA = "ifany")
addmargins(table(rhc$swang1, rhc$dth30, useNA = "ifany"))
addmargins(table(rhc$swang1, rhc$dth30, useNA = "ifany"), margin = 2)
prop.table(table(rhc$swang1, useNA = "ifany"))
prop.table(table(rhc$swang1, rhc$dth30, useNA = "ifany"), margin = 1)
# plot counts
ggplot(rhc) +
geom_bar(aes(x = swang1, fill = dth30))
# plot proportions
ggplot(rhc %>%
group_by(swang1, dth30) %>%
summarise(n = n()) %>%
mutate(freq = n / sum(n))) +
geom_bar(aes(x = swang1, y = freq, fill = dth30), stat = "identity") +
coord_flip()
# plot age
ggplot(rhc) +
geom_density(aes(x = age, group = swang1, fill = swang1), alpha = .4)
# exact matching --------------------------------------------------------------
# why exact matching is problematic
# select a small number of key matching variables
matchVars <- c("age", "sex", "cat1", "meanbp1")
# filter on key matching variables
rhc <- rhc %>%
dplyr::select_at(vars(swang1, dth30, matchVars)) %>%
mutate(age = as.integer(age))
# use tableone
unmatchedTbl1 <- CreateTableOne(
vars = matchVars, strata = "swang1", data = rhc, test = FALSE)
print(unmatchedTbl1)
print(unmatchedTbl1, smd = TRUE)
# Match() requires numeric variables
rhc <- rhc %>%
mutate(
swang1 = case_when(swang1 == "No RHC" ~ 0, TRUE ~ 1)
, dth30 = case_when(dth30 == "No" ~ 0, TRUE ~ 1))
# Match() requires numeric variables
matchVarsDf <- rhc %>%
dplyr::select(-dth30, -swang1) %>%
mutate_if(is.character, funs(as.numeric(as.factor(.))))
# why exact matching is problematci
# set.seed(12345)
exactMatch <- Match(
Y = rhc$dth30
, Tr = rhc$swang1
, M = 1
, X = matchVarsDf
, ties = FALSE
, replace = FALSE # ties are randomly broken when replace = FALSE
, estimand = "ATT"
, exact = colnames(matchVarsDf) %in% c("age", "sex", "cat1", "meanbp1"))
# recover matched dataset
exactMatch$mdata[["Y"]]
exactMatch$mdata[["Tr"]]
exactMatch$mdata[["X"]]
# exactMatchDf <- bind_cols(
# tibble(dth30 = exactMatch$mdata[["Y"]], swang1 = exactMatch$mdata[["Tr"]])
# , as_tibble(exactMatch$mdata[["X"]]))
# more useful to have variables as character/factor
exactMatchDf <- rhc[unlist(exactMatch[c("index.treated", "index.control")]), ]
# only 208 matches (from 2184 cases)
table(exactMatchDf$swang1)
# check balance
matchedTbl1 <- CreateTableOne(vars = matchVars, strata = "swang1", data = exactMatchDf , test = FALSE)
print(matchedTbl1, smd = TRUE)
# examine outcome
prop.table(table(exactMatchDf$swang1, exactMatchDf$dth30, useNA = "ifany"), margin = 1)
# test outcome
trtDth30 <- exactMatchDf %>% filter(swang1 == 1) %>% pull(dth30)
conDth30 <- exactMatchDf %>% filter(swang1 == 0) %>% pull(dth30)
t.test(trtDth30, conDth30, alternative = c("two.sided"), paired = TRUE)
# try running it again - what result do you get?
# https://stackoverflow.com/questions/13605271/reasons-for-using-the-set-seed-function
# psm matching ----------------------------------------------------------------
# fit a propensity score model (logistic regression)
psmM1 <- glm(swang1 ~ age + sex + cat1 + meanbp1
, family = binomial()
, data = rhc)
# check psm model statistics
summary(psmM1)
# predictions from model - probability of assignment to treatement i.e. receiving rhc
rhc$psmM1 <- predict.glm(psmM1, type = c("response"))
# plot a roc curve to evaluate models predictive ability
# psmM1Roc <- roc(response = rhc$swang1, predictor = rhc$psmM1)
psmM1Roc <- roc(swang1 ~ psmM1, data = rhc)
# plot the roc curve
plot(psmM1Roc)
# area under the curve
auc(psmM1Roc)
ci.auc(psmM1Roc)
# use propensity score for matching
pScoreM1 <- predict.glm(psmM1, type = c("link"))
# run Match()
pScoreM1Match <- Match(
Y = rhc$dth30
, Tr = rhc$swang1
, M = 1
, X = pScoreM1
, ties = FALSE
, replace = FALSE # ties are randomly broken when replace = FALSE
, estimand = "ATT"
, exact = NULL)
# more useful to have variables as character/factor
pScoreM1MatchDf <- rhc[unlist(pScoreM1Match[c("index.treated", "index.control")]), ]
# 2184 matches (from 2184 cases)
table(pScoreM1MatchDf$swang1)
# check balance
matchedTbl1 <- CreateTableOne(vars = matchVars, strata = "swang1", data = pScoreM1MatchDf , test = FALSE)
print(matchedTbl1, smd = TRUE)
# examine outcome
prop.table(table(pScoreM1MatchDf$swang1, pScoreM1MatchDf$dth30, useNA = "ifany"), margin = 1)
# test outcome
trtDth30 <- pScoreM1MatchDf %>% filter(swang1 == 1) %>% pull(dth30)
conDth30 <- pScoreM1MatchDf %>% filter(swang1 == 0) %>% pull(dth30)
t.test(trtDth30, conDth30, alternative = c("two.sided"), paired = TRUE)
# produce plot showing balance pre-post matching
# construct df with variable name and smd
lovePlotData <- data.frame(variable = dimnames(ExtractSmd(unmatchedTbl1))[[1]]
, unmatched = unname(ExtractSmd(unmatchedTbl1))
, matched = unname(ExtractSmd(matchedTbl1)))
# wrangle long-form data for ggplot2
lovePlotData <- lovePlotData %>%
gather(key = "method", value = "smd", -variable)
# plot
ggplot(lovePlotData, aes(x = variable, y = smd, group = method, color = method)) +
geom_point(shape = 16, size = 3) +
geom_hline(yintercept = 0.1, color = "#2c2825", size = 0.4, linetype = "33") +
coord_flip() +
scale_color_manual(values = c("#ec6555", "#5881c1")) +
scale_y_continuous(expand = c(0, 0), limits = c(-0.05, 1.05), name = "mean difference", breaks = c(seq(0, 1, 0.1))) +
scale_x_discrete(expand = c(0, 1), name = element_blank())
# test outcome using regression model (clean up any residual unbalance)
# gaussian model - no covariates - should match t.test() result
glmM1 <- glm(formula = dth30 ~ swang1
, family = gaussian()
, data = pScoreM1MatchDf)
summary(glmM1)
# binomial model (appropriate for binary outcome) with covariates
glmM2 <- glm(formula = as.formula(paste0("dth30 ~ swang1 +", paste(matchVars, collapse = "+")))
, family = binomial()
, data = pScoreM1MatchDf)
summary(glmM2)
exp(cbind(OddsRatios = coef(glmM2), confint(glmM2)))
# greedy matching on Mahanalobis ----------------------------------------------
# add-in some more covariates
rhc <- readRDS("rhcDataset.RDS")
matchVars <- c("age", "sex", "cat1", "meanbp1"
, "surv2md1", "hrt1", "resp1", "renalhx", "liverhx")
# filter on key matching variables
rhc <- rhc %>%
dplyr::select_at(vars(swang1, dth30, matchVars)) %>%
mutate(age = as.integer(age))
# use tableone
unmatchedTbl1 <- CreateTableOne(vars = matchVars, strata = "swang1", data = rhc, test = FALSE)
print(unmatchedTbl1)
print(unmatchedTbl1, smd = TRUE)
# Match() requires numeric variables
rhc <- rhc %>%
mutate(
swang1 = case_when(swang1 == "No RHC" ~ 0, TRUE ~ 1)
, dth30 = case_when(dth30 == "No" ~ 0, TRUE ~ 1))
# Match() requires numeric variables
matchVarsDf <- rhc %>%
dplyr::select(-dth30, -swang1) %>%
mutate_if(is.character, funs(as.numeric(as.factor(.))))
greedyMatch1 <- Match(
Y = rhc$dth30
, Tr = rhc$swang1
, M = 1
, X = matchVarsDf
, ties = FALSE
, replace = FALSE # ties are randomly broken when replace = FALSE
, estimand = "ATT"
, exact = colnames(matchVarsDf) %in% c("cat1"))
# more useful to have variables as character/factor
greedyMatch1Df <- rhc[unlist(greedyMatch1[c("index.treated", "index.control")]), ]
# 2011 matches (from 2184 cases)
table(greedyMatch1Df$swang1)
# check balance
matchedTbl1 <- CreateTableOne(vars = matchVars, strata = "swang1", data = greedyMatch1Df , test = FALSE)
print(matchedTbl1, smd = TRUE)
# examine outcome
prop.table(table(greedyMatch1Df$swang1, greedyMatch1Df$dth30, useNA = "ifany"), margin = 1)
# test outcome
trtDth30 <- greedyMatch1Df %>% filter(swang1 == 1) %>% pull(dth30)
conDth30 <- greedyMatch1Df %>% filter(swang1 == 0) %>% pull(dth30)
t.test(trtDth30, conDth30, alternative = c("two.sided"), paired = TRUE)
# produce plot showing balance pre-post matching
# construct df with variable name and smd
lovePlotData <- data.frame(variable = dimnames(ExtractSmd(unmatchedTbl1))[[1]]
, unmatched = unname(ExtractSmd(unmatchedTbl1))
, matched = unname(ExtractSmd(matchedTbl1)))
# wrangle long-form data for ggplot2
lovePlotData <- lovePlotData %>%
gather(key = "method", value = "smd", -variable)
# plot
ggplot(lovePlotData, aes(x = variable, y = smd, group = method, color = method)) +
geom_point(shape = 16, size = 3) +
geom_hline(yintercept = 0.1, color = "#2c2825", size = 0.4, linetype = "33") +
coord_flip() +
scale_color_manual(values = c("#ec6555", "#5881c1")) +
scale_y_continuous(expand = c(0, 0), limits = c(-0.05, 1.05), name = "mean difference", breaks = c(seq(0, 1, 0.1))) +
scale_x_discrete(expand = c(0, 1), name = element_blank())