(This article by Philip Parker was originally published at Adventures in R.)
I have reworked the graphing and summarizing functions I wrote for the matchit package to be cleaner and to provide more information.
Currently I have functions for a sorted by size summary table of the post matching mean differences:
meandifftable<- function (x){ post<-data.frame(x$sum.matched[4]) matchID <- as.vector (row.names (post) ) names(post)[1]<-c("m_mean_diff") post$absolute<- abs(post[1]) total2<-post[order (-post$absolute, na.last=NA) ,] meandiffover1<- subset(total2[1], total2[1]> .1 | total2[1]< -.1) meandiffover1 }
A graph function (histogram and density plot) for both pre-matched mean differences:
all_meandiffplot <- function (x) { adiff<-data.frame(x$sum.all) names(adiff)[4]<-c("all_mean_diff") diffplot<-ggplot(adiff, aes(all_mean_diff) ) diffplot<- diffplot+ geom_histogram (fill="grey") diffplot<- diffplot+ geom_density (colour="red") diffplot<-diffplot+xlim(-.5, .5) diffplot }
and after matching mean differences:
matched_meandiffplot <- function (x) { mdiff<-data.frame(x$sum.matched) names(mdiff)[4]<-c("matched_mean_diff") diffplot<-ggplot(mdiff, aes(matched_mean_diff) ) diffplot<- diffplot+ geom_histogram (fill="grey") diffplot<- diffplot+ geom_density (colour="red") diffplot<-diffplot+xlim(-.5, .5) diffplot }
Not that both plots are on a scale from -.5 to .5 so that they can easily be compared.
Finally I have a set of simple tables which indicate how many “large” (>.25), “medium” (>.20) and “small” (<.20) standardized mean differences there were before matching:
all_meandiffcount<-function (x){ all<-data.frame(x$sum.all[4]) all$all_group[all[1] > .25]<- "Large" all$all_group[all[1] < -.25] <- "Large" all$all_group[all[1] > .20 & all[1] < .25 ] <- "Medium" all$all_group[all[1] < -.20 & all[1] > -.25] <- "Medium" all$all_group[all[1] < .20 & all[1] > .00]<- "Small" all$all_group[all[1] > -.20 & all[1] < .00] <- "Small" table(all$all_group) }
and after matching:
matched_meandiffcount<-function (x){ matched<-data.frame(x$sum.matched[4]) matched$matched_group[matched[1] > .25]<- "Large" matched$matched_group[matched[1] < -.25] <- "Large" matched$matched_group[matched[1] > .20 & matched[1] < .25 ] <- "Medium" matched$matched_group[matched[1] < -.20 & matched[1] > -.25] <- "Medium" matched$matched_group[matched[1] < .20 & matched[1] > .00]<- "Small" matched$matched_group[matched[1] > -.20 & matched[1] < .00] <- "Small" table(matched$matched_group) }
Please comment on the article here: http://adventuresinr.wordpress.com/2010/11/13/additional-functions-for-the-matchit-package-part-2/
