# Quant 3: Section on matching procedures # Alex Herzog # Nov 16, 2007 # Data is from Michael J. Gilligan and Ernest J. Sergenti (2007): Do UN Interventions Cause Peace? Using Matching to Improve Causal Inference. Unpublished Manuscirpt. October 31. # Install Gary King's MatchIt package # install.packages("MatchIt", dependencies=TRUE) # Install Jas Sekhon's Matching package # install.packages("Matching", dependencies=TRUE) # install.packages("rgenoud") # Load packages library(MatchIt) library(Matching) library(foreign) # Load Gilligan/Sergenti dataset peace.data <- read.dta('peace_pre_match.dta') attach(peace.data) # Assign country names as row names # (Row names are by default the row numbers. Here I replace the row names with the # name of the observation (country) together with the date of the peace periode # (the latter in order to distinguish multiple observations of the same country)). rownames(peace.data) <- paste(cname,date0,sep=" ") # Look at variables in the dataset summary(peace.data) # Look at treated and non-treated cases table(UN) # Match treatment cases to control cases via nearest neigbhor method w/o replacement #################################################################################### m.out1 <- matchit(UN ~ lwdurat + milper + pop + ethfrac + lmtnest + bwplty2 + ssafrica + asia + lamerica, data=peace.data, method="nearest", replace=FALSE) ## Look at matching result m.out1 m.out1$match.matrix ## Check for balance summary(m.out1) plot(m.out1) plot(m.out1,type="jitter") # Match treatment cases to control cases via nearest neigbhor method w/ replacement ################################################################################### m.out2 <- matchit(UN ~ lwdurat + milper + pop + ethfrac + lmtnest + bwplty2 + ssafrica + asia + lamerica, data=peace.data, method="nearest", replace=TRUE) ## Look at matching result m.out2 m.out2$match.matrix ## Check for balance summary(m.out2) plot(m.out2) plot(m.out2,type="jitter") # Match treatment cases to control cases via genetic algorithm ############################################################## m.out3 <- matchit(UN ~ lwdurat + milper + pop + ethfrac + lmtnest + bwplty2 + ssafrica + asia + lamerica, data=peace.data, method="genetic", estimand="ATT", pop.size=1000, max.generations=100, wait.generations=10, int.seed=7, unif.seed=10, data.type.int=FALSE) ## Overview of matching result m.out3 m.out3$match.matrix ## Check for balance summary(m.out3) plot(m.out3) plot(m.out3,type="jitter") # Generate dataset of matched results and convert into Stata datafile m.data <- match.data(m.out2) write.dta(m.data,file="mdata.dta") ######################################################################################### # See Gary King's website for more info on MatchIt and the different matching algorithms: #+ MatchIt: http://gking.harvard.edu/matchit/ #++ NEAREST NEIGHBOR MATCHING: http://gking.harvard.edu/matchit/docs/Nearest_Neighbor_Match.html #++ GENETIC MATCHING: http://gking.harvard.edu/matchit/docs/Genetic_Matching.html # See Jas Sekhon's website for more info on the genetic matching algorithm: # http://sekhon.berkeley.edu/matching/