-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathihdp_matching.rmd
188 lines (147 loc) · 5.01 KB
/
ihdp_matching.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
---
output:
html_document:
toc: true
toc_float:
collapsed: false
theme: lumen
highlight: zenburn
df_print: paged
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
knitr::opts_chunk$set(include=TRUE)
knitr::opts_chunk$set(message=FALSE)
knitr::opts_chunk$set(warning=FALSE)
knitr::opts_chunk$set(tidy.opts=list(width.cutoff=80), tidy=TRUE)
knitr::opts_knit$set(root.dir='.')
```
```{r library}
library(tidyverse)
library(rstan)
library(rstanarm)
library(survey)
source("library/matching.R")
source("library/balance.R")
source("library/estimation.R")
library(cobalt)
library(rgenoud)
library(Matching)
library(CBPS)
library(WeightIt)
library(parallel)
set.cobalt.options(continuous='std', binary='raw')
```
```{r data}
load("data/cc2.Rdata")
covs_all <- setdiff(names(cc2), c("row.names", "row.names.1", "treat",
"treat0", "ppvtr.36"))
covs <- c("bw", "preterm", "dayskidh", "sex", "first", "age", "black",
"hispanic", "white", "b.marr", "lths", "hs", "ltcoll", "college",
"work.dur", "prenatal", "momage")
cov_names <- c("birth weight", "weeks preterm", "days in hospital",
"male", "first born", "age", "black", "hispanic", "white",
"unmarried at birth", "less than high school", "high school graduate",
"some college", "college graduate", "worked during pregnancy",
"had no prenatal care", "age at birth")
```
Raw data balance
========================
```{r, include=TRUE}
ps_form <- formula(treat ~ bwg + hispanic + black + b.marr + lths + hs + ltcoll + work.dur + prenatal + sex + first + st5 + st9 + st12 + st25 + st36 + st42 + st48 + st53 + age + bw + preterm + momage + dayskidh)
bal.tab(ps_form, data=cc2, estimand='ATT', continuous='std', m.threshold=.1, v.threshold=1.1)
```
Propensity score models
==========================
```{r}
# Base logit model
{
model <- 'models/ps_logit.rds'
if (file.exists(model)){
ps_logit <- readRDS(model)
} else{
set.seed(20)
ps_logit <- stan_glm(ps_form, family=binomial(link='logit'), data=cc2, algorithm='optimizing')
saveRDS(ps_logit, model)
}
ps_logit <- readRDS(model)
pscores_base <- apply(posterior_linpred(ps_logit, type='link'), 2, mean)
bal.tab(ps_logit, m.threshold=0.1, v.threshold=1.2)
}
# CBPS
{
model <- 'models/ps_cbps.rds'
if (file.exists(model)){
ps_cbps <- readRDS(model)
} else{
set.seed(20)
ps_cbps <- CBPS(ps_form, data=cc2, ATT=1, method='over')
saveRDS(ps_cbps, model)
}
ps_cbps <- readRDS(model)
pscores_cbps <- ps_cbps$fitted.values
bal.tab(ps_cbps, m.threshold=0.1, v.threshold=1.2)
}
# Genetic matching
{
# Matching w/o replacement
model <- 'models/mgen_1.rds'
mgen_1 <- readRDS(model)
}
# Matching w/ replacement
model <- 'models/mgen_1_wr.rds'
mgen_1_wr <- readRDS(model)
}
```
Matching w/o replacement
==========================
```{r}
# Base logit
matches_base <- matching(cc2$treat, score=pscores_base, replace=FALSE)
matched_base <- cc2[matches_base$match.ind,]
# CBPS
matches_cbps <- matching(cc2$treat, score=pscores_cbps, replace=FALSE)
matched_cbps <- cc2[matches_cbps$match.ind,]
# GenMatch
matches_mgen <- c(mgen_1$matches[,1], mgen_1$matches[,2])
matched_mgen <- cc2[matches_mgen,]
```
Matching w/ replacement
==========================
```{r}
# Base logit
matches.wr_base <- matching(cc2$treat, score=pscores_base, replace=TRUE)
matched.wr_base <- cc2[matches.wr_base$match.ind,]
# CBPS
matches.wr_cbps <- matching(cc2$treat, score=pscores_cbps, replace=TRUE)
matched.wr_cbps <- cc2[matches.wr_cbps$match.ind,]
```
Balance
==========================
```{r, include=TRUE}
bal.tab(ps_form, data=matched_base, estimand='ATT', continuous='std',m.threshold=0.1, v.threshold=1.2)
bal.tab(ps_form, data=matched.wr_base, estimand='ATT', continuous='std',m.threshold=0.1, v.threshold=1.2)
bal.tab(ps_form, data=matched_cbps, estimand='ATT', continuous='std',m.threshold=0.1, v.threshold=1.2)
bal.tab(ps_form, data=matched.wr_cbps, estimand='ATT', continuous='std',m.threshold=0.1, v.threshold=1.2)
bal.tab(ps_form, data=matched_mgen, estimand='ATT', continuous='std',m.threshold=0.1, v.threshold=1.2)
```
Treatment Effect
==========================
```{r, echo=TRUE, include=TRUE}
te_form <- formula(ppvtr.36 ~ treat + bwg + hispanic + black + b.marr + lths + hs + ltcoll + work.dur + prenatal + sex + first + st5 + st9 + st12 + st25 + st36 + st42 + st48 + st53 + bw + preterm + momage + dayskidh)
# Base logit mwor
set.seed(20)
summary(stan_glm(te_form, data=matched_base, algorithm='optimizing'))['treat', 1:2]
# CBPS mwor
set.seed(20)
summary(stan_glm(te_form, data=matched_cbps, algorithm='optimizing'))['treat', 1:2]
# GenMatch mwor
set.seed(20)
summary(stan_glm(te_form, data=matched_mgen, algorithm='optimizing'))['treat', 1:2]
# Base logit mwr
set.seed(20)
summary(stan_glm(te_form, data=matched.wr_base, algorithm='optimizing'))['treat', 1:2]
# CBPS mwr
set.seed(20)
summary(stan_glm(te_form, data=matched.wr_cbps, algorithm='optimizing'))['treat', 1:2]
```