ngreifer / cobalt

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

Bal.tab does not work with tibbles #1

Closed ganesh-krishnan closed 7 years ago

ganesh-krishnan commented 7 years ago

For some strange reason, bal.tab does not work with tibbles. Minimal reproducible example:

df <- tbl_df(lalonde)

treat <- "treat"
outcome <- "re78"
covs <- setdiff(names(df), c(treat, outcome))
covs_df <- dplyr::select(df, -treat, -re78, -nodegree, -married)
bal.tab(covs_df, treat = df[[treat]], method = "weighting")

This results in:

Note: estimand and s.d.denom not specified; assuming ATT and treated.
Error: No names in var.name are names of factor variables in data.

But this works (note that the only difference is that I'm not using tibbles):

data("lalonde", package = "cobalt")
df <- lalonde

treat <- "treat"
outcome <- "re78"
covs <- setdiff(names(df), c(treat, outcome))
covs_df <- dplyr::select(df, -treat, -re78, -nodegree, -married)
bal.tab(covs_df, treat = df[[treat]], method = "weighting")

This gives the desired result:

Note: estimand and s.d.denom not specified; assuming ATT and treated.
Balance Measures:
               Type Diff.Un
age         Contin. -0.3094
educ        Contin.  0.0550
race_black   Binary  0.6404
race_hispan  Binary -0.0827
race_white   Binary -0.5577
re74        Contin. -0.7211
re75        Contin. -0.2903

Sample sizes:
    Control Treated
All     429     185
ngreifer commented 7 years ago

You're absolutely right. I have struggled with tibbles myself, and as they get more popular (thanks in part to the haven package, I'm sure), I know I'll need to fix my code to accommodate them. The reason they don't work is that the syntax for calling a vector from a data.frame differs between tibbles and non-tibbles. What's nice is that the syntax used for tibbles also works for regular data.frames, but is less intuitive in my opinion. The next update to cobalt will include support for tibbles. For now, you will need to remove the tibble class and stick with old-fashioned data.frames.

ngreifer commented 7 years ago

I just updated the development version to support tibbles. Give it a try and let me know if you find any bugs.

ganesh-krishnan commented 7 years ago

Sounds good. I'll check it out and let you know. Thanks!

ganesh-krishnan commented 7 years ago

Gave it a shot yesterday. Works with both bal.tab and love.plot! Awesome!

Didn't give bal.plot a shot though.