Causal Effects in Observational Studies

Matching

  • the Matching package
  • the Match command for nearest-neighbor matching
  • using the output to compute a confidence interval
  • using the output to perform a hypothesis test
  • visualizing pre/post matching covariate balance with love.plot
  • visualizing pre/post matching covariate distribution with bal.plot

The Ideas in Code

Context

We’re trying to determine if going to Cal increases our chances of getting a good job:

\[ \mbox{ATE} = p_{\text{good job, cal}} - p_{\text{good job, no cal}} \overset{?}{>} 0 \]

So, we sent out a survey, recorded treatment variable (if the subject went to Cal), and the outcome (did they get a good job?). On top of that, because this is not a randomized experiment, we collected a few covariates that we thought could be confounders: GPA (because maybe people with higher GPAs would have preferred to go to Cal and also had higher chances to get a better job, anyway), the strength of their recommendation letter (try to explain why this might be a confounder), and their years of professional experience (again, why is this a possible confounder?).

This is the data set:

    GPA years_exp      rec cal_grad good_job
1  3.73         3 moderate    FALSE      yes
2  3.36         0 moderate    FALSE       no
3  3.25         1   strong    FALSE      yes
4  3.19         0 moderate     TRUE      yes
5  2.66         3     weak    FALSE      yes
6  3.67         3 moderate    FALSE       no
7  3.22         3 moderate    FALSE       no
8  3.16         1   strong     TRUE      yes
9  2.70         1   strong    FALSE       no
10 3.11         2 moderate    FALSE      yes
11 3.65         2 moderate     TRUE      yes
12 3.84         0     weak    FALSE      yes
13 3.42         3 moderate    FALSE      yes
14 3.27         2 moderate     TRUE      yes
15 2.67         1   strong     TRUE      yes
16 3.76         2     weak    FALSE      yes
17 3.11         0   strong    FALSE       no
18 3.29         2 moderate    FALSE      yes
19 3.48         2   strong    FALSE      yes
20 3.73         1 moderate    FALSE      yes

Matching

We use the Matching package in R to conduct nearest-neighbor matching using the Scaled Euclidean distance.

Don’t forget to install the package with install.packages("Matching") the first time you use it.

The command Match takes in several arguments. The trickiest one is the covariates X, so the first thing we’ll do is to format the covariates into a numerical matrix. This is because some of the covariates are categorical, so of course we’ll need to somehow make them numerical, in order to calculate distances between observations.

X <- model.matrix(~ GPA + years_exp + rec, data = grad_job)[, -1]

X
    GPA years_exp recstrong recweak
1  3.73         3         0       0
2  3.36         0         0       0
3  3.25         1         1       0
4  3.19         0         0       0
5  2.66         3         0       1
6  3.67         3         0       0
7  3.22         3         0       0
8  3.16         1         1       0
9  2.70         1         1       0
10 3.11         2         0       0
11 3.65         2         0       0
12 3.84         0         0       1
13 3.42         3         0       0
14 3.27         2         0       0
15 2.67         1         1       0
16 3.76         2         0       1
17 3.11         0         1       0
18 3.29         2         0       0
19 3.48         2         1       0
20 3.73         1         0       0

You see? X is numerical. The program created numerical indicator variables for the categorical variable rec. The reference level is clearly "moderate".

Now we need to feed the function Match the treatment variable Tr, the outcome variable Y (which I’m turning to numerical as well), and also specify that we’re doing nearest-neighbor matching using Scaled Euclidean distance.

library(Matching)

out <- with(grad_job, # the function with() helps us feed the dataset to Match()
  Match(Y = ifelse(good_job == "yes", 1, 0), # turning this into a numerical variable
        Tr = cal_grad,
        X = X,
        estimand = "ATE", # this means that we are estimating the full ATE
        M = 1,           # we're using only one nearest neighbor
        replace = TRUE,  # matches can be reused
        Weight = 1))     # scaled Euclidean distance

After the matching is done, we can visualize the results:

summary(out)

Estimate...  0.4 
AI SE......  0.18974 
T-stat.....  2.1082 
p.val......  0.035015 

Original number of observations..............  20 
Original number of treated obs...............  5 
Matched number of observations...............  20 
Matched number of observations  (unweighted).  20 

It seems that we estimated \(\widehat{\mbox{ATE}}=\) 0.4 with a standard error \(\widehat{\mbox{SE}} =\) 0.1897367.

How would you build a 95% C.I. for the \(\mbox{ATE}\)?

\[ \begin{aligned} 95\%\text{ C.I.} &= \widehat{\mbox{ATE}} \pm 1.96 \cdot \widehat{\mbox{SE}}\\ &= 0.4 \pm 1.96 \cdot 0.1897\\ &= 0.4 \pm 0.3719\\ &= [0.0281,\; 0.7719]. \end{aligned} \]

Pretty wide, but still showing a clearly positive estimate of the effect of going to Cal (because the interval doesn’t include 0).

We can see the same thing by noticing that the summary output also gives us the p-value for the hypothesis test of \(H_0 : \mbox{ATE} =0\) as 0.035015, which is clearly below \(\alpha=5\%\).

Visualizing the matches

What did Match actually do? With Weight = 1, it divides each column of X by its standard deviation in the whole sample, then, for every observation, finds the closest observation in the other group, using the usual (Euclidean) distance on these scaled covariates. When several observations are tied for closest, Match averages all of them; below, to keep things simple, we break ties at random instead and keep one match per observation.

In two dimensions, here are the two numerical covariates after scaling. Each arrow goes from an observation to the observation it was matched to, in the other group. Matching is not symmetric: if A’s closest look-alike is B, B’s closest look-alike may be someone else. A double-headed arrow means two observations are each other’s nearest neighbor; a single arrow means the match only goes one way. The shape of each point shows the recommendation letter, which also counts in the distance even though it’s not on these two axes.

Figure 1: Each observation, with a dashed arrow to its nearest neighbor in the other group (scaled GPA and years of experience). Double-headed arrows join observations that are each other’s nearest neighbor.

In three dimensions, we add the recommendation letter as a third axis (weak, moderate, strong). Drag the plot to rotate it. The small cones are the arrowheads: they point toward the match. Keep in mind that, for the computer, rec is really two separate 0/1 columns, so the length of a line in this picture is only an approximation of the distance Match uses.

Figure 2: The same matches in three dimensions, with the recommendation letter as the third axis. Cones point toward the match; mutual matches have a cone at both ends.

Finally, how far is each observation from its match? Each row is one observation (the dot on the left, colored by its group), and the dashed arrow points to its match (the dot on the right, colored by the match’s group, and labeled with its number). The length of the arrow is the scaled Euclidean distance, using all the covariates. Rows are sorted from the closest match to the farthest.

Figure 3: Scaled Euclidean distance between each observation and its match.

Observations at the top of this plot had no close look-alike in the other group, so their predicted potential outcome is the least reliable.

Covariate balance

We can then use cobalt and pass Match objects into the love.plot command, to create a Love plot with both pre- and post-matching covariate SMDs.

love.plot(out, formula = cal_grad  ~ GPA + years_exp + rec, data = grad_job,
          binary = "std", s.d.denom = "pooled")

Notice that, although for most covariates matching reduces imbalance, in rare cases we see an increase because of strong correlation between covariates. In this particular case, rec_moderate, rec_strong, rec_weak are very strongly associated (negatively) among themselves, because they are just indicator variables from the same categorical variable rec, and, e.g., when rec_moderate ==1 then rec_strong==0, and vice versa.

We can also visualize not just the means, but the entire distributions of the covariates in treatment vs. control before and after matching, to see how matching somewhat corrects the imbalance. Use the bal.plot function.

bal.plot(out, formula = cal_grad  ~ GPA + years_exp + rec, data = grad_job,
         var.name = "rec", which = "both")

We see that the balance is reached for "strong" but it’s actually made worse for "moderate". This is again due to the negative association we explained above.

The same can be done with the other two covariates:

bal.plot(out, formula = cal_grad  ~ GPA + years_exp + rec, data = grad_job,
         var.name = "GPA", which = "both")

bal.plot(out, formula = cal_grad  ~ GPA + years_exp + rec, data = grad_job,
         var.name = "years_exp", which = "both")