ngreifer / WeightIt

WeightIt: an R package for propensity score weighting
https://ngreifer.github.io/WeightIt/
102 stars 12 forks source link

Error when weighting #21

Closed statzhero closed 3 years ago

statzhero commented 3 years ago

I get the following error message when running a basic example. I believe all variables are well-behaved and have no missing data. Sorry about the lack of details but maybe there's an obvious answer.

weightit(treatment ~ rev_log,
                    data = d,  
                    method = "ebal")

Error in if (all(as.character(unique.treat.bin) == as.character(unique.treat))) { : 
  missing value where TRUE/FALSE needed
ngreifer commented 3 years ago

This is actually really surprising so I'm not sure what could be going on. Would you be willing to send me the data or a different reproducible example? Is the treatment binary? Does this happen when using other methods besides "ebal"? Is d a tibble (e.g., resulting from a function in haven)?

statzhero commented 3 years ago

Can you try this and let me know if it helps?

dta <- structure(
  list(treatment = c(FALSE, FALSE, TRUE, TRUE), 
       rev_log_match = c(17.8267157645103, 16.4479714107295, 16.9735477181105, 18.1387239441378)), 
          row.names = c(NA,-4L), 
          groups = structure(
            list(treatment = c(FALSE, TRUE), 
                .rows = structure(list(1:2, 3:4), 
                ptype = integer(0),
                class = c("vctrs_list_of","vctrs_vctr", "list"))), 
            row.names = c(NA, -2L),
            class = c("tbl_df","tbl", "data.frame"), .drop = TRUE), 
       class = c("grouped_df", "tbl_df", "tbl", "data.frame"))

# A tibble: 4 x 2
# Groups:   treatment [2]
# treatment rev_log_match
# <lgl>             <dbl>
# 1 FALSE              17.8
# 2 FALSE              16.4
# 3 TRUE               17.0
# 4 TRUE               18.1

WeightIt::weightit(treatment ~ rev_log_match, data = dta) 

# Error in if (all(as.character(unique.treat.bin) == as.character(unique.treat))) { : 
#     missing value where TRUE/FALSE needed
# In addition: Warning message:
#     In min(variable.numeric, na.rm = TRUE) :
#     no non-missing arguments to min; returning Inf

And:

R version 4.0.3 (2020-10-10)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Big Sur 10.16

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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.3.0    WeightIt_0.11.0 tidylog_1.0.2   glmnet_4.1-1   
 [5] Matrix_1.3-2    MatchIt_4.1.0   forcats_0.5.1   stringr_1.4.0  
 [9] dplyr_1.0.5     purrr_0.3.4     readr_1.4.0     tidyr_1.1.3    
[13] tibble_3.0.6    ggplot2_3.3.3   tidyverse_1.3.0
ngreifer commented 3 years ago

Thank you! It turns out that is because the treatment was a logical vector. If you make it 0s and 1s you should not receive this error. Let me know if that works. I'm fixing this now so that logical vectors should work fine.