Created
January 26, 2020 07:09
-
-
Save viniciusmss/e73ca00f189d7daceb127cc28fc799ca to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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