Skip to content

Instantly share code, notes, and snippets.

@diamonaj
Created March 28, 2023 00:14
Show Gist options
  • Save diamonaj/faff0807a0b5ee68ea01632861925300 to your computer and use it in GitHub Desktop.
Save diamonaj/faff0807a0b5ee68ea01632861925300 to your computer and use it in GitHub Desktop.
library(Matching)
data(lalonde)
#### EASY QUANTILE EFFECTS FOR RCTs...
## MEDIAN EFFECT
quantile(lalonde$re78[lalonde$treat == 1], probs = 0.5) -
quantile(lalonde$re78[lalonde$treat == 0], probs = 0.5)
#### 0.9 QUANTILE EFFECT
quantile(lalonde$re78[lalonde$treat == 1], probs = 0.9) -
quantile(lalonde$re78[lalonde$treat == 0], probs = 0.9)
#### 0.1 QUANTILE EFFECT
quantile(lalonde$re78[lalonde$treat == 1], probs = 0.1) -
quantile(lalonde$re78[lalonde$treat == 0], probs = 0.1)
##############
##############
## IF YOU WANT TO GET QUANTILE EFFECTS FOR MATCHED DATA
########################################################
X = cbind(lalonde$age, lalonde$educ, lalonde$black,
lalonde$hisp,
lalonde$married, lalonde$nodegr, lalonde$u74,
lalonde$u75,
lalonde$re75, lalonde$re74)
#The covariates we want to obtain balance on
BalanceMat <- X
genout <- GenMatch(Tr=lalonde$treat, X=X,
pop.size=50, max.generations=10,
wait.generations=10)
mout <- Match(Tr = lalonde$treat, X = X, Weight.matrix = genout)
############
############
### CHECK BALANCE
MatchBalance(lalonde$treat ~ lalonde$age + lalonde$educ +
lalonde$black +
lalonde$hisp + lalonde$married +
lalonde$nodegr +
lalonde$u74 + lalonde$u75 + lalonde$re74 +
lalonde$re75,
match.out = mout)
MatchBalance(treat ~ age + educ + black +
hisp + married + nodegr +
u74 + u75 + re74 + re75, data = lalonde,
match.out = mout)
###########
###########
# MATCHED DATA
matched_data <- lalonde[c(mout$index.treated, mout$index.control),]
# the weights below only relate to matching ties (otherwise all weights are 1s)
matched_weights <- c(mout$weights, mout$weights) # weights that reflect matching ties
## Quantile effects for matched data that don't consider the weights (imprecise effects)
## Specifically, in the example below, the MEDIAN treatment effect for matched data
median(matched_data$re78[matched_data$treated == 1]) - median(matched_data$re78[matched_data$treated == 0])
############
############
# if you want to run a quantile regression... then you can use the weights and obtain precise effects
################
install.packages("quantreg")
library(quantreg)
# median treatment effect (tau = 0.5)
quant_reg1 <- rq(re78 ~ treat, data = matched_data, weights = matched_weights, tau = 0.5)
summary(quant_reg1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment