DiogoFerrari / hdpGLM

Hierarchical Dirichlet Process Generalized Linear Models
http://www.diogoferrari.com/hdpGLM/
Other
11 stars 4 forks source link

hdpGLM methods fail on model objects loaded from .rds or .RData #3

Closed stnorton closed 5 years ago

stnorton commented 5 years ago

Model objects (i.e. objects created by a call to hdpGLM()) that are saved as .rds or .RData no longer seem to work with .hdpGLM methods after being loaded into a new R session.

After loading the saved model object, both plot.hdpGLM and summary.hdpGLM throw the following error:

Error in as.data.frame.default(value, stringsAsFactors = FALSE) : 
  cannot coerce class ‘"mcmc"’ to a data.frame

This occurs despite the fact that the object is correctly read as class hdpGLM.

stnorton commented 5 years ago

Turns out this is because the 'coda' package doesn't appear to be attached with hdpGLM. If coda is attached, it's possible to work with saved model objects.

DiogoFerrari commented 5 years ago

Have you loaded the hdpGLM library as well? You need to load the hdpGLM library because the package contains the functions that recognize the hdpGLM object. If you only load the Rdata you saved without loading the hdpGLM library, it probably won't work.

stnorton commented 5 years ago

Yes, I should've mentioned that. If I load hdpGLM before loading the saved model object, I receive the above error. If I load hdpGLM and coda, the summary.hdpGLM() method works.

Looking at the code for hdpGLM, I'm noticing that coda is referenced as a namespace with the :: on line 100 of that function (if I open with View(hdpGLM). This loads coda via the namespace when hdpGLM() is called.

Going to dump a lot of code here but running getting session info both before and after a call to hdpGLM confirms this. coda isn't attached before the call, but is attached after.

sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.10

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.3.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
 [1] lme4_1.1-19       Matrix_1.2-14     hdpGLM_1.0.0.0000 forcats_0.3.0     stringr_1.3.1     dplyr_0.7.8       purrr_0.3.0      
 [8] readr_1.3.1       tidyr_0.8.2       tibble_2.0.1      ggplot2_3.1.0     tidyverse_1.2.1  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0          lubridate_1.7.4     lattice_0.20-35     assertthat_0.2.0    digest_0.6.18       R6_2.3.0           
 [7] cellranger_1.1.0    plyr_1.8.4          backports_1.1.3     acepack_1.4.1       httr_1.4.0          pillar_1.3.1       
[13] rlang_0.3.1         lazyeval_0.2.1      readxl_1.2.0        minqa_1.2.4         rstudioapi_0.9.0    data.table_1.11.8  
[19] nloptr_1.2.1        rpart_4.1-13        checkmate_1.9.0     splines_3.5.1       foreign_0.8-71      htmlwidgets_1.3    
[25] munsell_0.5.0       broom_0.5.1         compiler_3.5.1      modelr_0.1.2        xfun_0.4            pkgconfig_2.0.2    
[31] base64enc_0.1-3     htmltools_0.3.6     nnet_7.3-12         tidyselect_0.2.5    gridExtra_2.3       htmlTable_1.13.1   
[37] Hmisc_4.1-1         withr_2.1.2         crayon_1.3.4        MASS_7.3-50         grid_3.5.1          nlme_3.1-137       
[43] jsonlite_1.6        gtable_0.2.0        magrittr_1.5        scales_1.0.0        cli_1.0.1           stringi_1.2.4      
[49] bindrcpp_0.2.2      latticeExtra_0.6-28 xml2_1.2.0          generics_0.0.2      Formula_1.2-3       RColorBrewer_1.1-2 
[55] tools_3.5.1         glue_1.3.0          hms_0.4.2           survival_2.42-6     yaml_2.2.0          colorspace_1.3-2   
[61] cluster_2.0.7-1     rvest_0.3.2         knitr_1.21          bindr_0.1.1         haven_2.0.0        
> model_res <- hdpGLM(data = analysis_df,
+             formula1 = opp_vote_pct ~ mean_dist_ren + turnout + m_opp_vote_pct 
+                     + dm_opp_vote_pct + dp_opp_vote_pct + kremlin_dist,
+             formula2 = ~ govt_dep + higher_ed + pop_total + pay,
+             context.id = 'region',
+             mcmc = list(burn.in = 100, n.iter = 100),
+             K = 30,
+             family = 'gaussian')

Preparing for estimation ...

Estimation in progress ...

[=======================================================================] 100 %
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.10

Matrix products: default
BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.3.3.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
 [1] bindrcpp_0.2.2    lme4_1.1-19       Matrix_1.2-14     hdpGLM_1.0.0.0000 forcats_0.3.0     stringr_1.3.1     dplyr_0.7.8      
 [8] purrr_0.3.0       readr_1.3.1       tidyr_0.8.2       tibble_2.0.1      ggplot2_3.1.0     tidyverse_1.2.1  

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0           lubridate_1.7.4      lattice_0.20-35      formula.tools_1.7.1  assertthat_0.2.0     digest_0.6.18       
 [7] R6_2.3.0             cellranger_1.1.0     plyr_1.8.4           backports_1.1.3      acepack_1.4.1        coda_0.19-2         
[13] httr_1.4.0           pillar_1.3.1         rlang_0.3.1          lazyeval_0.2.1       readxl_1.2.0         minqa_1.2.4         
[19] rstudioapi_0.9.0     data.table_1.11.8    nloptr_1.2.1         rpart_4.1-13         checkmate_1.9.0      splines_3.5.1       
[25] foreign_0.8-71       htmlwidgets_1.3      munsell_0.5.0        broom_0.5.1          compiler_3.5.1       modelr_0.1.2        
[31] xfun_0.4             pkgconfig_2.0.2      base64enc_0.1-3      htmltools_0.3.6      nnet_7.3-12          tidyselect_0.2.5    
[37] gridExtra_2.3        htmlTable_1.13.1     Hmisc_4.1-1          withr_2.1.2          crayon_1.3.4         MASS_7.3-50         
[43] grid_3.5.1           nlme_3.1-137         jsonlite_1.6         gtable_0.2.0         magrittr_1.5         scales_1.0.0        
[49] cli_1.0.1            stringi_1.2.4        latticeExtra_0.6-28  xml2_1.2.0           generics_0.0.2       Formula_1.2-3       
[55] RColorBrewer_1.1-2   tools_3.5.1          glue_1.3.0           hms_0.4.2            survival_2.42-6      yaml_2.2.0          
[61] colorspace_1.3-2     cluster_2.0.7-1      operator.tools_1.6.3 rvest_0.3.2          knitr_1.21           bindr_0.1.1         
[67] haven_2.0.0 

I'm guessing coda contains the function to coerce to dataframe, so this is just a really small dependency issue.

DiogoFerrari commented 5 years ago

done