-
Notifications
You must be signed in to change notification settings - Fork 6
/
day10-AdjustmentBalance.Rmd
662 lines (507 loc) · 23.4 KB
/
day10-AdjustmentBalance.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
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
---
title: |
| Statistical Adjustment and Assessment of Adjustment in Observational Studies
date: '`r format(Sys.Date(), "%B %d, %Y")`'
author: |
| ICPSR 2023 Session 1
| Jake Bowers \& Tom Leavitt
bibliography:
- 'BIB/MasterBibliography.bib'
fontsize: 10pt
geometry: margin=1in
graphics: yes
biblio-style: authoryear-comp
biblatexoptions:
- natbib=true
output:
beamer_presentation:
slide_level: 2
keep_tex: true
latex_engine: xelatex
citation_package: biblatex
template: icpsr.beamer
incremental: true
includes:
in_header:
- defs-all.sty
md_extensions: +raw_attribute-tex_math_single_backslash+autolink_bare_uris+ascii_identifiers+tex_math_dollars
pandoc_args: [ "--csl", "chicago-author-date.csl" ]
---
<!-- To show notes -->
<!-- https://stackoverflow.com/questions/44906264/add-speaker-notes-to-beamer-presentations-using-rmarkdown -->
```{r setup1_env, echo=FALSE, include=FALSE}
library(here)
source(here::here("rmd_setup.R"))
```
```{r setup2_loadlibs, echo=FALSE, include=FALSE}
## Load all of the libraries that we will use when we compile this file
## We are using the renv system. So these will all be loaded from a local library directory
library(dplyr)
library(ggplot2)
library(coin)
library(RItools)
library(optmatch)
```
## Today
1. Agenda: The problem of covariance adjustment to reduce "bias"/
confounding. **How can we answer the question about whether we have
adjusted enough.** A simple approach: stratification on one categorical
variable (and interaction effects). A more complex approach: find sets
that are as similar as possible in terms of a continuous variable
(bipartite matching). Balance assessment after stratification.
2. Recap:
2. We are now moving into the "Observational Studies" or Matching part of the course.
3. Questions arising from the reading or assignments or life?
## So far/Randomized experiments
- Randomization plus choice of tests statistic tells us which distribution
characterizes our hypothesis (plus Central Limit Theorem for tests of the
weak null of no average effects).
- Randomization tells us that estimators of average causal effects are
unbiased. (and tells us how to summarize how the values of those estimates
would vary across different runs of the same experiment with the same
experimental pool).
- Randomization does not guarantee strict equality of covariate distributions
but does guarantee "closeness on average" of those distributions. We can
compare the covariate distributions in actual experiments to those of
theoretical / hypothesized experiments (in this class using `balanceTest` and
the `overall` or $d^2$ omnibus test of overall balance).
- Randomization thus guarantees that comparisons between groups as-randomized
do not have systematic confounding with any observed or unobserved
covariates: outcome comparisons tell us only about the causal effect of the
treatment (which can have complex theoretical mechanisms, or even unclear or
contested mechanisms of course).
## So far/Non-random compliance {.fragile}
- We can learn about the causal effects of non-randomized variables, $D$, if we have an instrument, $Z$ under certain assumptions:
\begin{center}
\begin{tikzcd}[column sep=large]
Z \arrow[from=1-1,to=1-2, "\text{ITT}_D \ne 0"] \arrow[from=1-1, to=1-4, bend left, "\text{0 (exclusion)}"] & d \arrow[from=1-2,to=1-4, "CACE"] & & y \\
(x_1 \ldots x_p) \arrow[from=2-1,to=1-1, "\text{0 (as if randomized)}"] \arrow[from=2-1,to=1-2] \arrow[from=2-1,to=1-4]
\end{tikzcd}
\end{center}
- The @angrist1996 assumptions for estimating the CACE/LATE are: (1) sutva, (2) ignorable $Z$ (see above), (3) exclusion (see above), (4) no defiers (or they might call this "monotonicity"), (5) non-zero causal effect of $Z$ on $D$.
- This enables **Encouragement Designs**
- We can test the sharp null of no complier average effects directly (under
those assumptions): this helps us sidestep the problem of estimating $\Var\left[\cfrac{\widehat{\text{ITT}}_Y}{\widehat{\text{ITT}}_D}\right]$.
# How to assess the randomization process in an experiment.
## The Neyman-Rubin Model for (simple) experiments
This is what randomization ensures:
$$ ({y_t, y_c},{X}) \perp {Z} $$
i.e., each of $X$, $y_{c}$ and $y_{t}$ is balanced between treatment and control groups (in expectation; given variability from randomization).
\begin{itemize}
\item In controlled experiments, random assignment justifies this argument.
\item In natural experiments, justified otherwise, this is an article of faith.
\item In an experiment, the $x$es aren't necessary for inference (although
they can be used, carefully, to increase precision in both the design and
analysis phases of a project).
\item \textbf{However, the part with the $x$es has testable consequences.}
\end{itemize}
# Did we control for enough?
```{r cache=TRUE}
load(url("http://jakebowers.org/Data/meddat.rda"))
```
## Introducing the Medellin Data
Cerdá et al. collected data on about roughly `r nrow(meddat)`
neighborhoods in Medellin, Colombia. About `r signif(sum(meddat$nhTrt),2)` had
access to the new Metrocable line and `r signif(sum(1-meddat$nhTrt),2)` did not.
\centering
\includegraphics[width=.7\textwidth]{medellin-gondola.jpg}
<!-- For more on the Metrocable project see <https://www.medellincolombia.co/where-to-stay-in-medellin/medellin-orientation/> and <https://archleague.org/article/connective-spaces-and-social-capital-in-medellin-by-jeff-geisinger/> -->
## Introducing the Medellin Data
Cerdá et al. collected data on about roughly `r nrow(meddat)`
neighborhoods in Medellin, Colombia. About `r signif(sum(meddat$nhTrt),2)` had
access to the new Metrocable line and `r signif(sum(1-meddat$nhTrt),2)` did not.
\centering
\includegraphics[width=.8\textwidth]{medellin-conc-pov.jpg}
## Introducing the Medellin Data: Variables Collected
\scriptsize
```{r eval=FALSE}
## The Intervention
nhTrt Intervention neighborhood (0=no Metrocable station, 1=Metrocable station)
## Some Covariates (there are others, see the paper itself)
nh03 Neighborhood id
nhGroup Treatment (T) or Control (C)
nhTrt Treatment (1) or Control (0)
nhHom Mean homicide rate per 100,000 population in 2003
nhDistCenter Distance to city center (km)
nhLogHom Log Homicide (i.e. log(nhHom))
## Outcomes (BE03,CE03,PV03,QP03,TP03 are baseline versions)
BE Neighborhood amenities Score 2008
CE Collective Efficacy Score 2008
PV Perceived Violence Score 2008
QP Trust in local agencies Score 2008
TP Reliance on police Score 2008
hom Homicide rate per 100,000 population Score 2008-2003 (in log odds)
HomCount2003 Number of homicides in 2003
Pop2003 Population in 2003
HomCount2008 Number of homicides in 2008
Pop2008 Population in 2008
```
Get rates from counts:
```{r rates, echo=TRUE}
meddat<- mutate(meddat, HomRate03=(HomCount2003/Pop2003)*1000,
HomRate08=(HomCount2008/Pop2008)*1000)
```
## What is the effect of the Metrocable on Homicides?
One approach: Estimate the average treatment effect of Metrocable on
Homicides after the stations were built.
```{r lmone, echo=TRUE}
## code here
themeans<-group_by(meddat,nhTrt) %>% summarise(ybar=mean(HomRate08))
themeans
diff(themeans$ybar)
lmOne <- lm(HomRate08~nhTrt,meddat)
coef(lmOne)["nhTrt"]
library(estimatr)
difference_in_means(HomRate08~nhTrt,meddat)
```
Another approach, test the null of no effects: `balanceTest` and `oneway_test`
(with `distribution=asymptotic()`) use a large sample Central Limit Theorem
based approximation to a randomization distribution (which here is "as-if
randomized").
```{r initialtest, echo=TRUE}
balanceTest(nhTrt~HomRate08,data=meddat)
meddat$nhTrtF <- factor(meddat$nhTrt)
test2 <- oneway_test(HomRate08~nhTrtF,data=meddat,distribution=asymptotic())
## This next uses a permutation approach to approximate the as-if-randomized
## randomization distribution.
test3 <- oneway_test(HomRate08~nhTrtF,data=meddat,distribution=approximate(nresample=10000))
pvalue(test2)
pvalue(test3)
```
## Do we have any concerns about confounding?
Sometimes people ask about "bias from observed confounding" or "bias from selection on observables".
How would we interpret the following results where we look at the relationship
between one potential confounder ("proportion having more than a high school
degree") and receipt of a new Metrocable station (`nhTrt==1`)? (Recall how we
justified the use of `balanceTest` in terms of randomization above.)
```{r xbmed, echo=TRUE}
xbMed1 <- balanceTest(nhTrt~nhAboveHS,data=meddat)
xbMed1$overall
xbMed1$results
```
## One approach to this problem: model-based adjustment
Let's try to just adjust for this covariate in a very common manner:
```{r echo=FALSE}
lm1 <- lm(HomRate08~nhTrt+nhAboveHS,data=meddat)
```
```{r echo=FALSE}
preddat <- expand.grid(nhTrt=c(0,1),nhAboveHS=range(meddat$nhAboveHS))
preddat$fit <- predict(lm1,newdata=preddat)
```
\centering
```{r, out.width=".9\\textwidth", echo=FALSE}
par(oma=rep(0,4),mgp=c(1.5,.5,0),mar=c(3,3,0,0))
with(meddat, plot(nhAboveHS,HomRate08,pch=c(1,2)[nhTrt+1]))
with(subset(preddat,subset=nhTrt==0),lines(nhAboveHS,fit,lty=1))
with(subset(preddat,subset=nhTrt==1),lines(nhAboveHS,fit,lty=2))
## locator()
text(c(0.111807,0.001629), c(1.871,2.204), labels=c("Treat","Control"),pos=1)
text(c(.3,.5),c( coef(lm1)[1]+coef(lm1)[3]*.3 , coef(lm1)[1]+coef(lm1)[2]+coef(lm1)[3]*.5),
labels=c("Control","Treat"))
```
## Exactly what does this kind of adjustment do?
Notice that I can get the same coefficient (the effect of Metrocable on
Homicides adjusted for HS-Education in the neighborhood) either directly (as
earlier) or via **residualization**:
```{r echo=TRUE}
coef(lm1)["nhTrt"]
eYX <- residuals(lm(HomRate08~nhAboveHS,data=meddat))
eZX <- residuals(lm(nhTrt ~ nhAboveHS, data=meddat))
lm1a <- lm(eYX~eZX)
coef(lm1a)[2]
```
## Did we adjust enough?
Maybe adding some more information to the plot can help us decide whether, and to what extend, we effectively "controlled for" the proportion of the neighborhood with more than High School education. Specifically, we might be interested in assessing extrapolation/interpolation problems arising from our linear assumptions.
\centering
```{r, out.width=".7\\textwidth", echo=FALSE, warning=FALSE, message=FALSE}
par(oma=rep(0,4),mgp=c(1.5,.5,0),mar=c(3,3,0,0))
with(meddat, plot(nhAboveHS,HomRate08,pch=c(1,2)[nhTrt+1]))
with(subset(preddat,subset=nhTrt==0), lines(nhAboveHS,fit,lty=1))
with(subset(preddat,subset=nhTrt==1),lines(nhAboveHS,fit,lty=2))
with(subset(meddat,subset=nhTrt==0),lines(loess.smooth(nhAboveHS,HomRate08,deg=1,span=2/3),lty=1))
with(subset(meddat,subset=nhTrt==1),lines(loess.smooth(nhAboveHS,HomRate08,deg=1,span=.8),lty=2))
## locator()
text(c(0.111807,0.001629), c(1.871,2.204), labels=c("Treat","Control"),pos=1)
text(c(.3,.5),c( coef(lm1)[1]+coef(lm1)[3]*.3 , coef(lm1)[1]+coef(lm1)[2]+coef(lm1)[3]*.5),
labels=c("Control","Treat"))
with(subset(meddat,subset=nhTrt==0),rug(nhAboveHS))
with(subset(meddat,subset=nhTrt==1),rug(nhAboveHS,line=-.5))
```
How should we interpret this adjustment? How should we judge the improvement that we made? What concerns might we have?
```{r echo=FALSE, eval=FALSE}
thecovs <- unique(c(names(meddat)[c(5:7,9:24)],"HomRate03"))
balfmla<-reformulate(thecovs,response="nhTrt")
xbMed<-balanceTest(balfmla,
data=meddat,
p.adjust.method="none")
xbMed$overall
xbMed$results["nhAboveHS",,]
```
```{r echo=FALSE, eval=FALSE}
outcomefmla <- reformulate(c("nhTrt",thecovs),response="HomRate08")
lmbig <- lm_robust(outcomefmla,data=meddat)
lmbig
```
## How would you adjust for Proportion Above HS Degree?
So, part of the Metrocable effect might not reflect the causal effect of
Metrocable per se, but rather the education of people in the
neighborhood. How should we remove `nhAboveHS` from our estimate or test? What
strategies can you think of?
Features of a good adjustment process:
- Blind to outcome analysis (to preserve false positive rate and deter critics). Able to be pre-registered. Perhaps even reviewed by stakeholders.
- Easy to interpret ("controlling for" versus "holding constant")
- Easy to diagnoses (Easy to answere the question "Did we adjust enough?")
## Stratification V 1.0
```{r strat1, echo=TRUE}
lm1a <- lm(HomRate08~nhTrt,data=meddat,subset=nhAboveHS>=.1)
lm1b <- lm(HomRate08~nhTrt,data=meddat,subset=nhAboveHS<.1)
res_strat <- c(hiEd_Effect=coef(lm1a)["nhTrt"],loEd_Effect= coef(lm1b)["nhTrt"])
res_strat
n_strat <- table(meddat$nhAboveHS>=.1)
n_strat
stopifnot(sum(n_strat)==nrow(meddat)) ## A test of code
sum(res_strat * rev(n_strat)/45) ## What is happening here? A weighted average.
```
*But, standard errors? p-values? confidence intervals?*
## Stratified adjustment: One-step V 2.0
```{r strat2, echo=TRUE}
## Weight by block size
ate1c <- difference_in_means(HomRate08~nhTrt, blocks = I(nhAboveHS>=.1),data=meddat)
ate1c
## Weight by both block size and proportion in treatment vs control ("harmonic weight")
lm1c <- lm_robust(HomRate08~nhTrt, fixed_effects = ~I(nhAboveHS>=.1),data=meddat)
coef(lm1c)["nhTrt"]
lm1d <- lm_robust(HomRate08~nhTrt+I(nhAboveHS>=.1),data=meddat)
coef(lm1d)["nhTrt"]
## This next weighted by number of treated observations in each strata
xbate1 <- balanceTest(nhTrt~HomRate08+strata(I(nhAboveHS>=.1)),data=meddat)
xbate1$results[,c("adj.diff","p"),]
strat_by_trt <- with(meddat,table(nhAboveHS>=.1,nhTrt))
nt_strat <- strat_by_trt[,"1"]
sum(res_strat * rev(nt_strat)/sum(nt_strat))
```
## Balance assessment after stratification
Did we adjust enough? What would *enough* mean?
```{r xbHS1, echo=TRUE}
xbHS1 <- balanceTest(nhTrt~nhAboveHS+strata(I(nhAboveHS>=.1)),data=meddat)
xbHS1$overall
xbHS1$results[1,,] ## the covariate specific z-test
```
## Disadvantages and Advantages of Simple Stratification
- (+) Easy to explain what "controlling for" or "adjustment" means.
- (-) Hard to justify any particular cut-point
- (-) We could probably adjust *more* --- comparing neighborhoods similar in education rather than just within big strata
## The Curse of Dimensionality and linear adjustment for one more variable.
What about more than one variable? Have we controlled for both population
density and educational attainment enough? How would we know?
```{r lm2x, echo=TRUE}
lm2x <- lm(HomRate08 ~ nhTrt + nhPopD + nhAboveHS, data=meddat)
coef(lm2x)["nhTrt"]
```
Maybe another plot?
```{r eval=FALSE}
meddat$nhTrtF <- factor(meddat$nhTrt)
library(car)
scatter3d(HomRate08~nhAboveHS+nhPopD,
groups=meddat$nhTrtF,
data=meddat,surface=TRUE,
fit=c("linear")) #additive"))
```
```{r echo=FALSE, eval=FALSE}
scatter3d(HomRate08~nhAboveHS+nhPopD,
groups=meddat$nhTrtF,
data=meddat,surface=TRUE,
fit=c("additive"))
```
## The Problem of Using the Linear Model for Adjustment {.allowframebreaks}
- \bh{Problem of Interepretability:} "Controlling for" is "removing (additive) linear relationships" it is not "holding constant"
- \bh{Problem of Diagnosis and Assessment:} What is the standard against which we can compare a given linear covariance adjustment specification?
- \bh{Problem of extrapolation and interpolation:} Often known as "common support", too.
- \bh{Problems of overly influential points and curse of dimensionality:} As dimensions increase, odds of influential point increase (ex. bell curve in one dimension, one very influential point in 2 dimensions); also real limits on number of covariates (roughly $\sqrt{n}$ for OLS).
- \bh{Problems of bias:} We know that the difference of means is an unbiased
estimator of the ATE (or ITT aka ITT$_Y$) or ITT$_D$ in randomized
experiments, but that the covariance adjusted difference of means is not
unbiased (although the bias may be small).
\begin{equation}
Y_i = \beta_0 + \beta_1 Z_i + e_i
\end{equation}
This is a common practice because, we know that the formula to estimate $\beta_1$ above is the same as the difference of means in $Y$ between treatment and control groups:
\begin{equation}
\hat{\beta}_1 = \overline{Y|Z=1} - \overline{Y|Z=0} = \frac{\cov(Y,Z)}{\var(Z)}.
\end{equation}
Now what about adjusting ro $X$, what does OLS give us for $\beta_1$?
\begin{equation}
Y_i = \beta_0 + \beta_1 Z_i + \beta_2 X_i + e_i
\end{equation}
What is $\beta_1$ in this case? We know the matrix representation here $(\bX^{T}\bX)^{-1}\bX^{T}\by$, but here is the scalar formula for this particular case:
$$ \hat{\beta}_1 = \frac{\var(X)\cov(Z,Y) - \cov(X,Z)\cov(X,Y)}{\var(Z)\var(X) - \cov(Z,X)^2} $$
## Can we improve stratified adjustment?
Rather than two strata, why not three?
```{r lm1cut3, echo=TRUE}
lm1cut3 <- lm(HomRate08~nhTrt+cut(nhAboveHS,3),data=meddat)
coef(lm1cut3)["nhTrt"]
```
But why those cuts? And why not 4? Why not...?
\medskip
**One idea to side-step the problem of choosing cut-points**: collect observations into strata such that the sum of the
differences in means of nhAboveHS within strata is smallest? This is the idea
behind `optmatch` and other matching approaches.
## The optmatch workflow: The distance matrix
Introduction to `optmatch` workflow. To minimize differences requires a matrix
of those differences (in general terms, a matrix of distances between the
treated and control units)
```{r optm1, echo=TRUE}
tmp <- meddat$nhAboveHS
names(tmp) <- rownames(meddat)
absdist <- match_on(tmp, z = meddat$nhTrt,data=meddat)
absdist[1:3,1:3]
abs(meddat$nhAboveHS[meddat$nhTrt==1][1] - meddat$nhAboveHS[meddat$nhTrt==0][1] )
```
## Created a Stratified Research Design
```{r fm1, echo=TRUE}
fm1 <- fullmatch(absdist,data=meddat)
summary(fm1, min.controls=0, max.controls=Inf )
table(meddat$nhTrt,fm1)
pm1 <- pairmatch(absdist,data=meddat)
summary(pm1, min.controls=0, max.controls=Inf )
table(meddat$nhTrt,pm1,exclude=c())
```
## Evaluate the design: Within set differences
```{r echo=FALSE}
meddat$fm1 <- fm1
meddat$pm1 <- pm1
```
Differences within sets versus raw differences.
```{r echo=FALSE, out.width=".9\\textwidth"}
library(gridExtra)
bpfm1 <- ggplot(meddat,aes(x=fm1,y=nhAboveHS)) +
geom_boxplot() +
stat_summary(fun=mean, geom="point", shape=20, size=3, color="red", fill="red")
meddat$nostrata <- rep(1,45)
bporig <- ggplot(meddat,aes(x=nostrata,y=nhAboveHS))+
geom_boxplot()+
stat_summary(fun=mean, geom="point",
shape=20, size=3, color="red", fill="red")
grid.arrange(bpfm1,bporig,ncol=2,layout_matrix=matrix(c(1,1,1,1,2),nrow=1))
```
## Evaluate the design: Within set differences
```{r echo=FALSE, out.width=".9\\textwidth"}
bppm1 <- ggplot(meddat,aes(x=pm1,y=nhAboveHS)) +
geom_boxplot() +
stat_summary(fun=mean, geom="point", shape=20, size=3, color="red", fill="red")
grid.arrange(bppm1,bporig,ncol=2,layout_matrix=matrix(c(1,1,1,1,2),nrow=1))
```
## Evaluate the design: Inspect within set differences (Full Match)
```{r sdiffs, echo=FALSE}
rawmndiffs <- with(meddat, mean(nhAboveHS[nhTrt==1]) - mean(nhAboveHS[nhTrt==0]))
setdiffsfm1 <- meddat %>% group_by(fm1) %>% summarize(mneddiffs =
mean(nhAboveHS[nhTrt==1]) -
mean(nhAboveHS[nhTrt==0]),
mnAboveHS = mean(nhAboveHS),
minAboveHS = min(nhAboveHS),
maxAboveHS = max(nhAboveHS))
setdiffsfm1
#summary(setdiffs$mneddiffs)
#quantile(setdiffs$mneddiffs, seq(0,1,.1))
```
## Evaluate the design: Inspect within set differences (Pair Match)
```{r echo=FALSE, warnings=FALSE}
setdiffspm1 <- meddat %>% group_by(pm1) %>% summarize(mneddiffs =
mean(nhAboveHS[nhTrt==1]) -
mean(nhAboveHS[nhTrt==0]),
mnAboveHS = mean(nhAboveHS),
minAboveHS = min(nhAboveHS),
maxAboveHS = max(nhAboveHS))
setdiffspm1
```
## Evaluate the design: Compare to a randomized experiment.
The within-set differences look different from those that would be expected
from a randomized experiment.
```{r xbhs2, echo=TRUE}
xbfm1 <- balanceTest(nhTrt~nhAboveHS+strata(fm1),
data=meddat)
xbfm1$results[,,]
xbfm1$overall
```
## What is balanceTest doing?
```{r xbagain, echo=TRUE}
setmeanDiffs <- meddat %>% group_by(fm1) %>%
summarise(diffAboveHS=mean(nhAboveHS[nhTrt==1])-mean(nhAboveHS[nhTrt==0]),
nb=n(),
nTb = sum(nhTrt),
nCb = sum(1-nhTrt),
hwt = ( 2*( nCb * nTb ) / (nTb + nCb))
)
setmeanDiffs
```
## What is balanceTest doing with multiple sets/blocks?
The test statistic is a weighted average of the set-specific differences (same
approach as we would use to test the null in a block-randomized experiment)
```{r wtmns, echo=TRUE}
## The descriptive mean difference using block-size weights (focusing on the
## treated observations)
with(setmeanDiffs, sum(diffAboveHS*nTb/sum(nTb)))
## The mean diff used as the observed value in the testing
with(setmeanDiffs, sum(diffAboveHS*hwt/sum(hwt)))
## Compare to balanceTest output
xbfm1$results[,,"fm1"]
```
## Can we improve the design?
Here we (1) require sets to contain neighborhoods that differ by no more than
.08 on proportion educated more than high-school and (2) require than no set
have more than 2 treated neighborhoods.
```{r}
## Look at the kinds of differences between units that are possible.
quantile(as.vector(absdist),seq(0,1,.1))
absdist_cal <- match_on(absdist+caliper(absdist,.08),data=meddat)
fm2 <- fullmatch(absdist_cal,data=meddat,min.controls=.5)
summary(fm2, min.controls=0, max.controls=Inf )
table(meddat$nhTrt,fm2)
stratumStructure(fm1)
stratumStructure(fm2)
matched.distances(distance=absdist_cal,matchobj=fm2)
summary(unlist(matched.distances(distance=absdist_cal,matchobj=fm2)))
summary(unlist(matched.distances(distance=absdist,matchobj=fm1)))
meddat %>% group_by(fm1) %>% summarize(mean(nhAboveHS),n_b=n(), ate_b=mean(HomRate08[nhTrt==1])-mean(HomRate08[nhTrt==0]))
```
## Can we improve the design?
Here we (1) require sets to contain neighborhoods that differ by no more than
.08 on proportion educated more than high-school and (2) require than no set
have more than 2 treated neighborhoods.
```{r xbhs3, echo=TRUE}
xbfm <- balanceTest(nhTrt~nhAboveHS+strata(fm1)+strata(fm2),
data=meddat)
xbfm$results[,,]
xbfm$overall
```
## Can we improve the design?
Here we (1) require sets to contain neighborhoods that differ by no more than
.08 on proportion educated more than high-school and (2) require than no set
have more than 2 treated neighborhoods.
```{r echo=FALSE, out.width=".9\\textwidth"}
bpfm2 <- ggplot(meddat,aes(x=fm2,y=nhAboveHS)) +
geom_boxplot() +
stat_summary(fun=mean, geom="point", shape=20, size=3, color="red", fill="red")
meddat$nostrata <- rep(1,45)
bporig <- ggplot(meddat,aes(x=nostrata,y=nhAboveHS))+
geom_boxplot()+
stat_summary(fun=mean, geom="point",
shape=20, size=3, color="red", fill="red")
grid.arrange(bpfm2,bporig,ncol=2,layout_matrix=matrix(c(1,1,1,1,2),nrow=1))
```
## Summary of the Day
- How to justify an adjustment strategy for an observational study? The
linear model adjustment strategy is difficult to justify. A stratification
based strategy is easier to justify, inspect, learn from. (We can compare
our stratification to a block randomized experiment, to a known design, a
known standard.)
- How to choose a stratification? We can do it by hand. Or we can delegate to
a computer (i.e. `optmatch`) --- we can think of it as an optimization
problem and ask the computer to optimize.
- Today we created two matched designs aiming to reduce the confounding
associated with `nhAboveHS`. We judged those designs based on (1) substantive
information about what kinds of education-based differences seemed small or
large compared with what we know about the context and (2) statistical
information about the consistency of our design with a truly randomized
design.
## References