data(schools)
# Kreft and De Leeuw, Introducing Multilevel Modeling, Sage (1988).
# The data set is the subsample of NELS-88 data consisting of 10 handpicked schools
# from the 1003 schools in the full data set.
# Let us consider the following variables:
X<-schools$ses
Y<-schools$math
Tr<-ifelse(schools$homework>1,1,0)
Group<-schools$schid
# Note that when Group is missing / NULL or there is only one group the function MatchW returns
# the output of the Match function with a warning.
# Matching math scores between gropus of students. X are covariate(s) we wish to match on.
### Matching within schools
mw <- MatchW(Y=Y, Tr=Tr, X=X, Group=Group, caliper=0.1)
# compare balance before and after matching
CMatchBalance(Tr~X,data=schools,match.out=mw)
# find proportion of matched observations
(mw$orig.treated.nobs-mw$ndrops)/mw$orig.treated.nobs
# check number of drops by school
mw$orig.ndrops.by.group
# estimate the math score difference (default is ATT)
mw$estimand
# examine output
mw # complete results
summary(mw) # main results
#### Propensity score matching
# estimate the propensity score (ps) model
mod <- glm(Tr~ses+parented+public+sex+race+urban,
family=binomial(link="logit"),data=schools)
eps <- fitted(mod)
# eg 1: within-school propensity score matching
psmw <- MatchW(Y=schools$math, Tr=Tr, X=eps, Group=schools$schid, caliper=0.1)
# We can use other strategies for controlling unobserved cluster covariates
# by using different specifications of ps:
# eg 2: standard propensity score matching using ps estimated
# from a logit model with dummies for schools
mod <- glm(Tr ~ ses + parented + public + sex + race + urban
+schid - 1,family=binomial(link="logit"),data=schools)
eps <- fitted(mod)
dpsm <- MatchW(Y=schools$math, Tr=Tr, X=eps, caliper=0.1)
# this is equivalent to run Match with X=eps
# eg3: standard propensity score matching using ps estimated from
# multilevel logit model (random intercept at the school level)
require(lme4)
mod<-glmer(Tr ~ ses + parented + public + sex + race + urban + (1|schid),
family=binomial(link="logit"), data=schools)
eps <- fitted(mod)
mpsm<-MatchW(Y=schools$math, Tr=Tr, X=eps, Group=NULL, caliper=0.1)
# this is equivalent to run Match with X=eps
Run the code above in your browser using DataLab