kosukeimai / MatchIt

R package MatchIt
210 stars 43 forks source link

Combine diagnostic plots for multiple models #143

Closed foogee closed 1 year ago

foogee commented 1 year ago

MatchIt is a great tool with such useful diagnostic output!

I'd like to be able to compare diagnostic plots across models by combining the plots into a single figure. I've tried using approaches like setting mfrow (as in the below example), and using packages like cowplot and GridExtra. Nothing seems to work! I think because the plot outputted by MatchIt is a non-standard plot type.

Do you have any suggestions for how I can better cobble together these diagnostic plots for easier review of them?

library(tidyverse) 
library(MatchIt)

data("lalonde")

formula1=treat ~ age + educ + race + married + 
  nodegree + re74 + re75
formula2=treat ~ age + educ + race + married + 
  nodegree 
formulas=list(formula1,formula2)

results <- formulas %>% map(~matchit(formula=.x, data=lalonde, method="nearest", distance="glm"))

opar <- par(no.readonly = TRUE)

# set 2 x 1 layout
dev.off()
par(mfrow = c(2,2))

for (i in 1:1) plot(summary(results[[i]]))

par(opar)
ngreifer commented 1 year ago

These are inherent limitations in R's plotting functionality. Use cobalt instead, which creates highly customizable plots using ggplot2 and is compatible with matchit objects. love.plot() creates a very similar plot to plot(summary(.)).

You might be able to do something like:

library(patchwork)
do.call(wrap_plots, c(lapply(results, love.plot), list(nrow = 2)))

patchwork also has functionality for combining base R plots, but it might not be so easy to do in a loop.

foogee commented 1 year ago

That worked - thanks much!