ngreifer / cobalt

Covariate Balance Tables and Plots - An R package for assessing covariate balance
https://ngreifer.github.io/cobalt/
73 stars 12 forks source link

Using love.plot() with a list of `matchit` objects #30

Closed the-Zian closed 4 years ago

the-Zian commented 4 years ago

I have a list of matchit objects and want to create love plots from them. I'm getting an error using love.plot() with purrr::map().

library(purrr)
library(cobalt)

data("lalonde")

matchits <- vector(mode = "list")
ps_form <- formula(treat ~ age + educ + black + hispan + married)
matchits$nn.wo <- matchit(ps_form, lalonde, method = "nearest", replace = FALSE)
matchits$nn.wr <- matchit(ps_form, lalonde, method = "nearest", replace = TRUE)
matchits$opt.r1 <- matchit(ps_form, lalonde, method = "optimal", ratio = 1)

# These work
love.plot(matchits$nn.wr)
love.plot(matchits[[1]])

# These do not work
map(matchits, love.plot)
# Error in .f(x = bal.tab(.x[[i]])) : could not find function ".f"

map(matchits, ~love.plot(.))
# Error in mc[["x"]][[1]] : object of type 'symbol' is not subsettable

map(matchits, function(x) love.plot(x))
#  Error: covs must be a data.frame of covariates. 
ngreifer commented 4 years ago

Thank you very much! I think I've fixed this. A new version of cobalt with many new features will be released soon. My only suggestion to solve this right now is to use a for loop just as you did to create matchits. Also, I hope you know that you can display balance for multiply matching specifications in the same plot rather than having a plot for each specification. You would do this as follows:

love.plot(ps_form, data = lalonde, weights = map(matchits, get.w))

I'm glad you're finding cobalt useful and I'm sorry for this bug!

ngreifer commented 4 years ago

Fixed in development version, will be fixed with release of version 4.0.0.