philips-software / latrend

An R package for clustering longitudinal datasets in a standardized way, providing interfaces to various R packages for longitudinal clustering, and facilitating the rapid implementation and evaluation of new methods
https://philips-software.github.io/latrend/
GNU General Public License v2.0
28 stars 5 forks source link

Error in get(time) : invalid first argument #151

Closed jlhanson5 closed 6 months ago

jlhanson5 commented 6 months ago

Hello Colleagues,

I'm trying to test drive latrend, but ran into issues. I can run the demo, but with real data, I get the following output/error...

> method <- lcMethodKML("BrainAgeGap", id = "PSC2", time = "time", nClusters = 3)
> model <- latrend(method, IMAGEN_long)
             --------------------------------------------------------------
             - Longitudinal clustering using: longitudinal k-means (KML)
             --------------------------------------------------------------
             Method arguments:
              time:           "time"
              id:             "PSC2"
              nClusters:      3
              nbRedrawing:    20
              maxIt:          200
              imputationMethod:"copyMean"
              distanceName:   "euclidean"
              power:          2
              distance:       function() {}
              centerMethod:   meanNA
              startingCond:   "nearlyAll"
              nbCriterion:    1000
              scale:          TRUE
              response:       "BrainAgeGap"
             --------------------------------------------------------------
             Checking and transforming the training data format.
Error in get(time) : invalid first argument

here's my session info.

> sessionInfo()
R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.3.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

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

time zone: America/New_York
tzcode source: internal

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

other attached packages:
 [1] lubridate_1.9.3 forcats_1.0.0   stringr_1.5.1   dplyr_1.1.4     purrr_1.0.2    
 [6] readr_2.1.5     tidyr_1.3.1     tibble_3.2.1    tidyverse_2.0.0 ggplot2_3.4.4  
[11] latrend_1.5.1  

loaded via a namespace (and not attached):
 [1] gtable_0.3.4             xfun_0.42                htmlwidgets_1.6.4       
 [4] remotes_2.4.2.1          processx_3.8.3           callr_3.7.3             
 [7] tzdb_0.4.0               ps_1.7.6                 vctrs_0.6.5             
[10] tools_4.3.2              Rdpack_2.6               generics_0.1.3          
[13] curl_5.2.0               rgl_1.3.1                fansi_1.0.6             
[16] cluster_2.1.4            pkgconfig_2.0.3          R.oo_1.26.0             
[19] data.table_1.15.0        longitudinalData_2.4.5.1 desc_1.4.3              
[22] assertthat_0.2.1         lifecycle_1.0.4          compiler_4.3.2          
[25] farver_2.1.1             munsell_0.5.0            codetools_0.2-19        
[28] misc3d_0.9-1             htmltools_0.5.7          class_7.3-22            
[31] pillar_1.9.0             R.utils_2.12.3           iterators_1.0.14        
[34] foreach_1.5.2            tidyselect_1.2.0         digest_0.6.34           
[37] stringi_1.8.3            labeling_0.4.3           fastmap_1.1.1           
[40] grid_4.3.2               colorspace_2.1-0         cli_3.6.2               
[43] magrittr_2.0.3           base64enc_0.1-3          pkgbuild_1.4.3          
[46] utf8_1.2.4               withr_3.0.0              scales_1.3.0            
[49] timechange_0.3.0         rmarkdown_2.25           matrixStats_1.2.0       
[52] clv_0.3-2.4              R.methodsS3_1.8.2        hms_1.1.3               
[55] kml_2.4.6.1              evaluate_0.23            knitr_1.45              
[58] rbibutils_2.2.16         tcltk_4.3.2              rlang_1.1.3             
[61] glue_1.7.0               rstudioapi_0.15.0        jsonlite_1.8.8          
[64] R6_2.5.1 

I don't really have more info about why the error is happening. I changed my data from a tbl_df to a data.frame, but that didn't solve the issue. And here's what my data.frame looks like:

> head(IMAGEN_long)
    PSC2 BrainAgeGap time
1 112288    0.814929    1
2 112288    1.083334    2
3 112288    1.249620    3
4 215284    2.034363    1
5 215284   -0.831310    2
6 215284    0.430477    3

Any suggestions about how to troubleshoot?

Many thanks, Jamie.

niekdt commented 6 months ago

Hi Jamie, thanks for reporting this issue. This turned out to be a bug, caused by older code not handling dynamic data column evaluation in a robust way. Using an id or time variable with the name of a function would resolve to that function instead (stats::time in your case).

It should be fixed now in the latest commit. To install, use:

remotes::install_github('philips-software/latrend')
jlhanson5 commented 6 months ago

Perfect, that looks to have worked! Thanks for the quick change here! I really appreciate it!