##code for Chapter 7 "Propensity Score Methods for Continuous Treatment Doses" 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 #PART 1B GENERALIZED PROPENSITY SCORES AND DOSE RESPONSE FUNCTION FOR CONTINOUS TREATMENTS #USING A GAMMA DISTRIBUTION #this example estimates the effect of school participation #in the Algebra Nation virtual learning environment #on the mean student scores on Florida's Algebra I End-of-Course (EOC) Assessment. #load data data <- read.csv("Chapter7_data_Algebra_Nation.csv") #standardize continuous covariates #(this is to help interpret covariate balance measures because they will be in standard units) data$numOfStud2014 <- scale(data$numOfStud2014) data$meanScale2012 <- scale(data$meanScale2012) data$lev1Perc2012 <- scale(data$lev1Perc2012) data$lev5Perc2012 <- scale(data$lev5Perc2012) data$perc.free.lunch <- scale(data$perc.free.lunch) data$perc.reduced.lunch <- scale(data$perc.reduced.lunch) #convert binary variables to factors data$SeniorHigh <- factor(data$SeniorHigh) data$middleHigh <- factor(data$middleHigh) #breakdown locale into size and type variables data$locationSize <- with(data, ifelse(Locale.=="City: Large" | Locale.=="Suburb: Large", "Large", ifelse( Locale.=="City: Midsize"| Locale.=="Suburb: Midsize", "Midsize", "Small"))) data$locationSize <- factor(data$locationSize) data$locationRural <- with(data, ifelse(Locale.=="Rural: Distant" | Locale.=="Rural: Fringe" | Locale.=="Rural: Remote", "Rural", "Urban")) data$locationRural <- factor(data$locationRural) #view treatment dose, which in this case is logins per examinee hist(data$loginsPerExaminee) #given that the loginsPerExaminee has outliers, trim the top 5% data = subset(data, loginsPerExaminee