#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 #===================================================== #obtain weights with propensity scores estimated with random forests #load data load(file="Chapter3_ELS_data_imputed_with_weights.Rdata") #obtain weights for estimating the ATT for propensity scores obtained with logistic regression ELS.data.imputed$weightATTRf <- with(ELS.data.imputed,ifelse(treat==1, 1, pScoresRf/(1-pScoresRf))) with(ELS.data.imputed, by(weightATTRf,treat,summary)) #obtain weights for estimating the ATE ELS.data.imputed$weightATERf <- with(ELS.data.imputed,ifelse(treat==1, 1/pScoresRf, 1/(1-pScoresRf))) with(ELS.data.imputed, by(weightATERf,treat,summary)) #save data save(ELS.data.imputed, file="Chapter3_ELS_data_imputed_with_weights.Rdata", compress=T) #====================================================== #obtain weights with propensity scores estimated with GBM #obtain weights for estimating the ATT for propesnity scores obtained with logistic regression ELS.data.imputed$weightATTGBM <- with(ELS.data.imputed,ifelse(treat==1, 1, pScoresGBM/(1-pScoresGBM))) with(ELS.data.imputed, by(weightATTGBM,treat,summary)) #obtain weights for estimating the ATE ELS.data.imputed$weightATEGBM <- with(ELS.data.imputed,ifelse(treat==1, 1/pScoresGBM, 1/(1-pScoresGBM))) with(ELS.data.imputed, by(weightATEGBM,treat,summary)) #save data save(ELS.data.imputed, file="Chapter3_ELS_data_imputed_with_weights.Rdata", compress=T)