nt-williams / lmtp

:package: Non-parametric Causal Effects Based on Modified Treatment Policies :crystal_ball:
http://www.beyondtheate.com
GNU Affero General Public License v3.0
55 stars 16 forks source link

Error when running lmtp_contrast() #117

Closed upulcooray closed 1 year ago

upulcooray commented 2 years ago

Thank you very much for this great package. I have been using your package for several epidemiological studies and didn't have any issues. I had to move my code to a Mac PC recently (from linux) and lmtp_contrast() started to break. I cannot figure out the issue. Any help would be greatly appreciated!

I have lmtp estimates with in a tibble like this;

> tmle_sl
# A tibble: 5 × 7
    imp d0     d1     d2     d3     d4     d5    
  <int> <list> <list> <list> <list> <list> <list>
1     1 <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp>
2     2 <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp>
3     3 <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp>
4     4 <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp>
5     5 <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp>

I want to use map2 to get contrasts like this;

tmle_sl %>%
  mutate(
    d0_vs_d1= map2(d1,d0,~lmtp::lmtp_contrast(.x,ref = .y, type="or")),
    d0_vs_d2= map2(d2,d0,~lmtp::lmtp_contrast(.x,ref = .y, type="or")),
    d0_vs_d3= map2(d3,d0,~lmtp::lmtp_contrast(.x,ref = .y, type="or")),
    d0_vs_d4= map2(d4,d0,~lmtp::lmtp_contrast(.x,ref = .y, type="or")),
    d0_vs_d5= map2(d5,d0,~lmtp::lmtp_contrast(.x,ref = .y, type="or")) )

Getting this error;

Error in `mutate()`:
! Problem while computing `d0_vs_d1 = map2(d1, d0,
  ~lmtp::lmtp_contrast(.x, ref = .y, type = "or"))`.
Caused by error in `split.default()`:
! group length is 0 but data length > 0

Backtrace:
     ▆
  1. ├─tmle_sl %>% ...
  2. ├─dplyr::mutate(...)
  3. ├─dplyr:::mutate.data.frame(...)
  4. │ └─dplyr:::mutate_cols(.data, dplyr_quosures(...), caller_env = caller_env())
  5. │   ├─base::withCallingHandlers(...)
  6. │   └─mask$eval_all_mutate(quo)
  7. ├─purrr::map2(d1, d0, ~lmtp::lmtp_contrast(.x, ref = .y, type = "or"))
  8. │ └─.f(.x[[i]], .y[[i]], ...)
  9. │   └─lmtp::lmtp_contrast(.x, ref = .y, type = "or")
 10. │     └─lmtp:::contrast_or(fits = fits, ref = ref)
 11. │       └─base::lapply(fits, function(x) contrast_or_single(x, ref))
 12. │         └─lmtp FUN(X[[i]], ...)
 13. │           └─lmtp:::contrast_or_single(x, ref)
 14. │             ├─base::split(log_eif, fit$id)
 15. │             └─base::split.default(log_eif, fit$id)
 16. └─base::.handleSimpleError(...)
 17.   └─dplyr h(simpleError(msg, call))
 18.     └─rlang::abort(...)
> sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.4

Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.2-arm64/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  
[7] base     

other attached packages:
 [1] forcats_0.5.1   stringr_1.4.0   dplyr_1.0.9    
 [4] purrr_0.3.4     readr_2.1.2     tidyr_1.2.0    
 [7] tibble_3.1.7    ggplot2_3.3.6   tidyverse_1.3.1
[10] targets_0.12.1 

loaded via a namespace (and not attached):
 [1] tidyselect_1.1.2  xfun_0.31         haven_2.5.0      
 [4] colorspace_2.0-3  vctrs_0.4.1       generics_0.1.2   
 [7] yaml_2.3.5        utf8_1.2.2        rlang_1.0.2      
[10] pillar_1.7.0      glue_1.6.2        withr_2.5.0      
[13] DBI_1.1.2         dbplyr_2.2.0      sessioninfo_1.2.2
[16] modelr_0.1.8      readxl_1.4.0      lifecycle_1.0.1  
[19] munsell_0.5.0     gtable_0.3.0      cellranger_1.1.0 
[22] rvest_1.0.2       codetools_0.2-18  knitr_1.39       
[25] callr_3.7.0       tzdb_0.3.0        ps_1.7.0         
[28] fansi_1.0.3       broom_0.8.0       checkmate_2.1.0  
[31] backports_1.4.1   scales_1.2.0      jsonlite_1.8.0   
[34] fs_1.5.2          hms_1.1.1         digest_0.6.29    
[37] stringi_1.7.6     processx_3.6.0    grid_4.2.0       
[40] cli_3.3.0         tools_4.2.0       magrittr_2.0.3   
[43] base64url_1.4     crayon_1.5.1      pkgconfig_2.0.3  
[46] ellipsis_0.3.2    xml2_1.3.3        data.table_1.14.2
[49] lmtp_1.3.0        reprex_2.0.1      lubridate_1.8.0  
[52] rstudioapi_0.13   assertthat_0.2.1  httr_1.4.3       
[55] R6_2.5.1          igraph_1.3.1      compiler_4.2.0 

Removing map2 didn't help either;

> m1 <- tmle_sl$d1[[1]]
> m2 <- tmle_sl$d0[[1]]
> lmtp::lmtp_contrast(m1,ref = m2, type="or")
Error in split.default(log_eif, fit$id) : 
  group length is 0 but data length > 0

Thank you! Upul

kathoffman commented 2 years ago

Hi @upulcooray -- I've actually had this issue too! It arose when I used the newer version of lmtp on lmtp objects created with an older version of lmtp. When I looked at the source code, it looks like @nt-williams added a column to the fit objects (fit$id) in Fall 2021 or so. Not sure if there's a more permanent change/check coming, but what worked for for me was installing an older version of lmtp using this code:

devtools::install_github("nt-williams/lmtp",ref="d5c9d2c348bec0c79b2c5f704e38afb5b6694896", force=T)

upulcooray commented 2 years ago

Hi @kathoffman , It worked! Thank you very much for the quick response!!

# A tibble: 5 × 12
    imp d0     d1     d2     d3     d4     d5     d0_vs_d1   d0_vs_d2   d0_vs_d3   d0_vs_d4   d0_vs_d5  
  <int> <list> <list> <list> <list> <list> <list> <list>     <list>     <list>     <list>     <list>    
1     1 <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt>
2     2 <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt>
3     3 <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt>
4     4 <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt>
5     5 <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt> <lmtp_cnt>

BTW.. I am a big fan of your blog. I think it is the best online resource to understand TMLE and MTP for people like me (I am a dentist- no statistical background).

nt-williams commented 2 years ago

Thanks @kathoffman for the quick fix. Going to re-open this issue and issue a patch.

nt-williams commented 1 year ago

Fixed in devel.