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

Error: object 'xxx' not found #40

Closed MiguelGodinhoMatos closed 3 years ago

MiguelGodinhoMatos commented 4 years ago

love.plot and bal.tab are not dealing correctly with the formula object when there are inline operations applied to a variable. The bug occurs only when the raw variable is not included in the formula object.

Replicable code:

library(MatchIt)
data("lalonde", package = "cobalt")

#Works
m.out1 <- matchit(treat ~ age + educ + race + married + nodegree + re74 + re75, data = lalonde)
love.plot(m.out1, var.order = "unadjusted") 

#Does not work
m.out1 <- matchit(treat ~ log(age) + educ  + race + married + nodegree + re74 + re75,data = lalonde)
love.plot(m.out1, var.order = "unadjusted") #KO

#Works again
m.out1 <- matchit(treat ~ log(age) + age + educ  + race + married + nodegree + re74 + re75,data = lalonde)
love.plot(m.out1, var.order = "unadjusted")

The behavior is true in both versions: cobalt_4.2.2 and cobalt_4.2.1

Environment for replication (also works on 4.x).

R version 3.6.1 (2019-07-05)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Catalina 10.15.5

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] cobalt_4.2.1  MatchIt_3.0.2

loaded via a namespace (and not attached):
 [1] rstudioapi_0.11  knitr_1.29       magrittr_1.5     MASS_7.3-51.6    tidyselect_1.1.0 munsell_0.5.0    colorspace_1.4-1 R6_2.4.1         rlang_0.4.6      dplyr_1.0.0     
[11] tools_3.6.1      grid_3.6.1       gtable_0.3.0     xfun_0.15        htmltools_0.5.0  ellipsis_0.3.1   yaml_2.2.1       digest_0.6.25    tibble_3.0.1     lifecycle_0.2.0 
[21] crayon_1.3.4     purrr_0.3.4      ggplot2_3.3.2    vctrs_0.3.1      glue_1.4.1       evaluate_0.14    rmarkdown_2.3    pillar_1.4.4     compiler_3.6.1   backports_1.1.8 
[31] generics_0.0.2   scales_1.1.1     pkgconfig_2.0.3 
ngreifer commented 4 years ago

Thank you for letting me know! I'll look into that right away.

MiguelGodinhoMatos commented 4 years ago

@ngreifer thanks for being so quick. I already tested your solution. However, I think it does not work for factors. You are not taking factor levels into account. These are only known after you create the model.matrix (the model.frame is not enough). Ideally, you would always work with the formula functions I suppose.

#Still does not work
m.out1 <- matchit(treat ~ factor(age) + educ  + race + married + nodegree + re74 + re75,data = lalonde)
love.plot(m.out1, var.order = "unadjusted") 

Error in .rowNamesDF<-(x, value = value) : duplicate 'row.names' are not allowed In addition: Warning message: non-unique value when setting 'row.names': ‘factor(age)’

ngreifer commented 4 years ago

Thank you for finding this! I'm pretty sure I've fixed it now. Dealing with formulas is such a pain.