#code for Chapter 3 "Propensity Score Weighting" of book:
#Leite, W. L. (2017). Practical propensity score methods using R.
#Thousand Oaks, CA: Sage.
#
#this is the code that was used to generate the example results in the book
#As the R software and the R packages used in this example are updated frequently
#some incompatibilities between the current code and new R versions or package versions
#may appear
#Any updates to the code will be posted at:
# http://www.practicalpropensityscore.com
#===========================================================
#ESTIMATE THE ATT
#Estimate the effect of high school student participation in
#career academies on future income
#using data from the Education Longitudinal Study (ELS)
#load data
load(file="Chapter3_ELS_data_imputed_with_weights.Rdata")
#load required survey library
require(survey)
options(survey.lonely.psu = "adjust")
#re-create the survey design including the final weight for 2006
surveyDesign2006 <- svydesign(ids=~psu, strata=~STRAT_ID, weights=~finalWeight2006,
data = ELS.data.imputed, nest=T)
#create replicate weights for bootstrapping
surveyDesign2006Boot <- as.svrepdesign(surveyDesign2006, type=c("bootstrap"),replicates=1000)
#obtain ATT as weighted mean differences.
(weightedMeans <- svyby(formula=~F2ERN5P2,by=~treat,design=surveyDesign2006Boot,
FUN=svymean,covmat=TRUE))
(ATT2006 <- svycontrast(weightedMeans, contrasts=c(-1,1)))
#obtain the group variances
(weightedVars <- svyby(formula=~F2ERN5P2,by=~treat,design=surveyDesign2006Boot,
FUN=svyvar,covmat=TRUE))
#estimate the ATT for 2006 with regression analysis for complex survey data
outcomeModel2006 <- svyglm(F2ERN5P2~treat,surveyDesign2006)
summary(outcomeModel2006)
#re-estimate the ATT with regression, but this time obtain standard errors with bootstrapping
outcomeModel2006Boot <- svyglm(F2ERN5P2~treat,surveyDesign2006Boot)
summary(outcomeModel2006Boot)
#save all objects
save(list=ls(), file="Chapter_3_treatment_effect_estimates.Rdata",compress=T)