#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 #======================================== #EVALUATE COVARIATE BALANCE #load data load(file="Chapter3_ELS_data_imputed_example_career_academy.Rdata") load(file="Chapter3_ELS_data_imputed_with_weights.Rdata") #create final weights for the base year #the final weigth is the product of the propensity score weight #and the sampling weight ELS.data.imputed$finalWeightBY <- with(ELS.data.imputed,bystuwt*weightATT) #evaluate covariate balance for ATT require(twang) balanceTable <- bal.stat(ELS.data.imputed, vars= covariateNames, treat.var = "treat", w.all = ELS.data.imputed$finalWeightBY, get.ks=F, sampw = ELS.data.imputed$bystuwt, estimand="ATT", multinom=F) #Table with results of balance evaluation. The columns are: # tx.mn - The mean of the treatment group # tx.sd - The standard deviation of the treatment group # ct.mn - The mean of the control group # ct.sd - The standard deviation of the control group # std.eff.sz - The standardized effect size, (tx.mn-ct.mn)/tx.sd. # stat - the t-statistic for numeric variables and the chi-square statistic for categorical variables # p - the p-value for the test associated with stat balanceTable <- balanceTable$results #summarize the covariate balance quality std.eff.sz <- summary(abs(balanceTable$std.eff.sz)) #standardized effect sizes std.eff.sz table(abs(balanceTable$std.eff.sz)>0.05) row.names(balanceTable)[abs(balanceTable$std.eff.sz)>0.05] #save balance table write.csv(balanceTable, file="balance_table.csv")