Skip to content

Instantly share code, notes, and snippets.

@viniciusmss
Created January 26, 2020 07:09
Show Gist options
  • Save viniciusmss/e73ca00f189d7daceb127cc28fc799ca to your computer and use it in GitHub Desktop.
Save viniciusmss/e73ca00f189d7daceb127cc28fc799ca to your computer and use it in GitHub Desktop.
library(Matching)
data(lalonde)
run_iter_maching <- function(data, iters = 3)
{
# Setting up variables
sample <- data
ctrls <- list()
treats <- list()
for (i in 1:iters) {
# Run matching
m <- Match(sample$re78, sample$treat,
cbind(sample$age, sample$educ, sample$re74),
M=1, ties=FALSE)
# Save indexes
ctrls[[i]] <- m$index.control
treats[[i]] <- m$index.treated
# Drop matched controls
sample <- sample[-m$index.control, ]
}
# Get treated units that were matched every timei
always_treat <- Reduce(intersect, treats)
# Store matches
match_out <- list()
# For each remaining treated unit, find its control
for (t_idx in always_treat)
{
# Create storage for the three controls
this_ctrls <- c()
# For each iteration
for (i in 1:iters)
{
# Find the correct index in the index.treated (Redundant when not LATE)
inner_t_idx <- match(t_idx, treats[[i]])
# Find corresponding control matched
this_ctrls[i] <- ctrls[[i]][inner_t_idx]
}
# Save three controls
match_out[[t_idx]] <- this_ctrls
}
return(match_out)
}
match_out <- run_iter_maching(lalonde)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment