tidymodels / TMwR

Code and content for "Tidy Modeling with R"
https://tmwr.org
Other
604 stars 285 forks source link

cannot load "tidyposterior" due to Error: object ‘posterior_epred’ is not exported by 'namespace:rstanarm' #352

Closed sunhuaiyu closed 1 year ago

sunhuaiyu commented 1 year ago

It happened in Chapter 11 Comparing Models with Resampling. The cause is likely due to posterior_epred() being moved to rstantools and no longer under rstanarm.


> library('tidyposterior') 
Error: package or namespace load failed for ‘tidyposterior’:  object ‘posterior_epred’ is not exported by 'namespace:rstanarm'

> packageVersion('rstanarm')
[1] ‘2.21.3’
> packageVersion('tidyposterior')
[1] ‘1.0.0’
> packageVersion('rstantools')
[1] ‘2.2.0’

> sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.6 LTS

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3
LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.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 datasets  utils     methods   base     

other attached packages:
 [1] rstantools_2.2.0   posterior_1.3.1    ggrepel_0.9.2      forcats_0.5.2      kableExtra_1.3.4   rstanarm_2.21.3   
 [7] Rcpp_1.0.9         corrr_0.4.4        yardstick_1.1.0    workflowsets_1.0.0 workflows_1.1.2    tune_1.0.1        
[13] tidyr_1.2.1        tibble_3.1.8       rsample_1.1.1      recipes_1.0.3      purrr_1.0.0        parsnip_1.0.3     
[19] modeldata_1.0.1    infer_1.0.4        ggplot2_3.4.0      dplyr_1.0.10       dials_1.1.0        scales_1.2.1      
[25] broom_1.0.2        tidymodels_1.0.0  

loaded via a namespace (and not attached):
  [1] backports_1.4.1      systemfonts_1.0.4    plyr_1.8.8           igraph_1.3.5         splines_4.1.2        crosstalk_1.2.0     
  [7] listenv_0.9.0        inline_0.3.19        digest_0.6.31        foreach_1.5.2        htmltools_0.5.4      fansi_1.0.3         
 [13] checkmate_2.1.0      memoise_2.0.1        magrittr_2.0.3       globals_0.16.2       gower_1.0.1          RcppParallel_5.1.5  
 [19] matrixStats_0.63.0   svglite_2.1.0        xts_0.12.2           hardhat_1.2.0        timechange_0.1.1     prettyunits_1.1.1   
 [25] colorspace_2.0-3     rvest_1.0.3          xfun_0.36            jsonlite_1.8.4       callr_3.7.3          crayon_1.5.2        
 [31] lme4_1.1-31          survival_3.4-0       zoo_1.8-11           iterators_1.0.14     glue_1.6.2           gtable_0.3.1        
 [37] ipred_0.9-13         webshot_0.5.3        distributional_0.3.1 pkgbuild_1.4.0       rstan_2.21.7         future.apply_1.10.0 
 [43] abind_1.4-5          DBI_1.1.3            miniUI_0.1.1.1       viridisLite_0.4.1    xtable_1.8-4         GPfit_1.0-8         
 [49] stats4_4.1.2         lava_1.7.0           StanHeaders_2.21.0-7 prodlim_2019.11.13   DT_0.26              httr_1.4.4          
 [55] htmlwidgets_1.6.0    threejs_0.3.3        ellipsis_0.3.2       farver_2.1.1         pkgconfig_2.0.3      loo_2.5.1           
 [61] sass_0.4.4           nnet_7.3-18          utf8_1.2.2           labeling_0.4.2       tidyselect_1.2.0     rlang_1.0.6         
 [67] DiceDesign_1.9       reshape2_1.4.4       later_1.3.0          cachem_1.0.6         munsell_0.5.0        tools_4.1.2         
 [73] cli_3.5.0            generics_0.1.3       evaluate_0.19        stringr_1.5.0        fastmap_1.1.0        yaml_2.3.6          
 [79] processx_3.8.0       knitr_1.41           future_1.30.0        nlme_3.1-161         mime_0.12            xml2_1.3.3          
 [85] compiler_4.1.2       bayesplot_1.10.0     shinythemes_1.2.0    rstudioapi_0.14      lhs_1.1.6            bslib_0.4.2         
 [91] stringi_1.7.8        ps_1.7.2             lattice_0.20-45      Matrix_1.5-3         nloptr_2.0.3         markdown_1.4        
 [97] tensorA_0.36.2       conflicted_1.1.0     shinyjs_2.1.0        vctrs_0.5.1          pillar_1.8.1         lifecycle_1.0.3     
[103] furrr_0.3.1          jquerylib_0.1.4      httpuv_1.6.7         R6_2.5.1             bookdown_0.30        promises_1.2.0.1    
[109] renv_0.16.0          gridExtra_2.3        parallelly_1.33.0    codetools_0.2-18     boot_1.3-28.1        colourpicker_1.2.0  
[115] MASS_7.3-58.1        gtools_3.9.4         assertthat_0.2.1     withr_2.5.0          shinystan_2.6.0      parallel_4.1.2      
[121] grid_4.1.2           rpart_4.1.19         timeDate_4021.107    class_7.3-20         minqa_1.2.5          rmarkdown_2.19      
[127] shiny_1.7.4          lubridate_1.9.0      base64enc_0.1-3      dygraphs_1.1.1.6  
juliasilge commented 1 year ago

Hmmm, I can't seem to reproduce this problem. I can load library(tidyposterior) just fine, and also I can access this function from rstanarm directly:

library(rstanarm)
#> Loading required package: Rcpp
#> This is rstanarm version 2.21.3
#> - See https://mc-stan.org/rstanarm/articles/priors for changes to default priors!
#> - Default priors may change, so it's safest to specify priors, even if equivalent to the defaults.
#> - For execution on a local, multicore CPU with excess RAM we recommend calling
#>   options(mc.cores = parallel::detectCores())
example(example_model)
#> 
#> exmpl_> if (.Platform$OS.type != "windows" || .Platform$r_arch != "i386") {
#> exmpl_+ example_model <- 
#> exmpl_+   stan_glmer(cbind(incidence, size - incidence) ~ size + period + (1|herd),
#> exmpl_+              data = lme4::cbpp, family = binomial, QR = TRUE,
#> exmpl_+              # this next line is only to keep the example small in size!
#> exmpl_+              chains = 2, cores = 1, seed = 12345, iter = 1000, refresh = 0)
#> exmpl_+ example_model
#> exmpl_+ }
#> stan_glmer
#>  family:       binomial [logit]
#>  formula:      cbind(incidence, size - incidence) ~ size + period + (1 | herd)
#>  observations: 56
#> ------
#>             Median MAD_SD
#> (Intercept) -1.5    0.6  
#> size         0.0    0.0  
#> period2     -1.0    0.3  
#> period3     -1.1    0.4  
#> period4     -1.5    0.4  
#> 
#> Error terms:
#>  Groups Name        Std.Dev.
#>  herd   (Intercept) 0.79    
#> Num. levels: herd 15 
#> 
#> ------
#> * For help interpreting the printed output see ?print.stanreg
#> * For info on the priors used see ?prior_summary.stanreg
probs <- posterior_epred(example_model) 
head(probs)
#>           
#> iterations         1         2          3          4          5          6
#>       [1,] 0.2207217 0.1530571 0.12234923 0.05634765 0.08394297 0.05237306
#>       [2,] 0.2643940 0.0882392 0.08943436 0.08868958 0.13345755 0.03881754
#>       [3,] 0.3408261 0.1498225 0.11563975 0.08203311 0.17867397 0.06522078
#>       [4,] 0.2668656 0.1094357 0.07376019 0.07605865 0.18549509 0.06812528
#>       [5,] 0.2781390 0.1657598 0.19688502 0.10209919 0.13261954 0.07441391
#>       [6,] 0.5156173 0.3758614 0.34285073 0.22466065 0.14451492 0.09202992
#>           
#> iterations          7         8          9         10         11         12
#>       [1,] 0.04804851 0.2507230 0.16021108 0.13802408 0.07910548 0.08527703
#>       [2,] 0.04249072 0.2164998 0.06591273 0.06934889 0.07586204 0.13502345
#>       [3,] 0.05849518 0.3214503 0.12509019 0.10411809 0.09197524 0.24878742
#>       [4,] 0.05222510 0.3912990 0.16403357 0.12059928 0.14777742 0.20220190
#>       [5,] 0.08555815 0.1503515 0.08665657 0.10208459 0.04651156 0.13645889
#>       [6,] 0.06855823 0.1446265 0.09714426 0.07863249 0.03610728 0.20382840
#>           
#> iterations         13         14         15         16         17         18
#>       [1,] 0.05920040 0.04884253 0.02212247 0.09543073 0.07979059 0.06608325
#>       [2,] 0.04137458 0.04304230 0.04321009 0.20606241 0.07298728 0.07582843
#>       [3,] 0.10710373 0.08641085 0.06247228 0.16000097 0.07863660 0.06305528
#>       [4,] 0.08255268 0.05778030 0.06104030 0.12869259 0.05886370 0.04088313
#>       [5,] 0.07400290 0.08817357 0.04252862 0.14134728 0.07215356 0.08600300
#>       [6,] 0.12015038 0.10035372 0.05674287 0.14959812 0.07095375 0.05872201
#>           
#> iterations         19         20         21         22         23        24
#>       [1,] 0.01895617 0.07257785 0.05017147 0.04361163 0.02260156 0.2777746
#>       [2,] 0.06171402 0.13628868 0.04180469 0.04460440 0.04769786 0.3998855
#>       [3,] 0.02750133 0.11706932 0.04582300 0.03868456 0.03194950 0.3539126
#>       [4,] 0.02860815 0.09251378 0.03492879 0.02528439 0.03024549 0.4903493
#>       [5,] 0.04852886 0.11810219 0.06343075 0.07439016 0.03396719 0.2303489
#>       [6,] 0.05255632 0.19553953 0.11477381 0.09079198 0.04442185 0.4463628
#>           
#> iterations        25        26         27        28         29         30
#>       [1,] 0.1798409 0.1517807 0.07119011 0.4106123 0.12821626 0.08360722
#>       [2,] 0.1454181 0.1506207 0.14944998 0.3912832 0.07354313 0.02065728
#>       [3,] 0.1418687 0.1153266 0.08180254 0.2504336 0.16182118 0.06000107
#>       [4,] 0.2270104 0.1667689 0.17142924 0.2659450 0.28451579 0.11575210
#>       [5,] 0.1382795 0.1625971 0.08262025 0.2791849 0.06330440 0.03399571
#>       [6,] 0.3390882 0.2953264 0.18881210 0.3579318 0.05363374 0.03196342
#>           
#> iterations         31         32         33         34         35         36
#>       [1,] 0.07493902 0.03540819 0.07700188 0.05330822 0.04052117 0.02215409
#>       [2,] 0.02236110 0.02274271 0.10452279 0.03126380 0.03130984 0.03438241
#>       [3,] 0.05225661 0.03850227 0.09665639 0.03730809 0.02713786 0.02373503
#>       [4,] 0.08776999 0.09469265 0.08762195 0.03297124 0.02109144 0.02652088
#>       [5,] 0.03971398 0.01846495 0.12893065 0.06964239 0.08530537 0.03847525
#>       [6,] 0.02409842 0.01276578 0.11961790 0.06757667 0.06073522 0.02759910
#>           
#> iterations        37         38         39         40         41         42
#>       [1,] 0.2286328 0.17467328 0.13369450 0.06888231 0.08691536 0.05725664
#>       [2,] 0.1512191 0.04814670 0.04761210 0.04963826 0.18794703 0.05866380
#>       [3,] 0.2458843 0.11150385 0.08056771 0.06334494 0.13532498 0.05063966
#>       [4,] 0.2084151 0.08950364 0.05711535 0.06476864 0.12208132 0.04483905
#>       [5,] 0.1403970 0.07493629 0.09248681 0.04348682 0.13947350 0.07713402
#>       [6,] 0.1626815 0.08901590 0.08238722 0.04244810 0.21122455 0.13156393
#>           
#> iterations         43         44         45         46         47         48
#>       [1,] 0.04597192 0.02196482 0.04113437 0.03054343 0.02245778 0.01217443
#>       [2,] 0.06022992 0.06198303 0.11432849 0.03580827 0.03540557 0.03886394
#>       [3,] 0.03920162 0.02964104 0.11013113 0.04680672 0.03315072 0.02901642
#>       [4,] 0.03025374 0.03361148 0.10323972 0.04221861 0.02644419 0.03320481
#>       [5,] 0.09266039 0.04398054 0.09835647 0.05085476 0.06315027 0.02810888
#>       [6,] 0.11303781 0.06085832 0.12703481 0.06635053 0.06129609 0.02786304
#>           
#> iterations        49        50         51         52         53         54
#>       [1,] 0.3823092 0.2056829 0.18270127 0.09435409 0.06149131 0.03801479
#>       [2,] 0.4264747 0.1409994 0.14940643 0.15333168 0.10368633 0.02944098
#>       [3,] 0.3599543 0.1083302 0.09239717 0.07081941 0.14812581 0.05282238
#>       [4,] 0.4255091 0.1463072 0.10939550 0.12044591 0.11040906 0.03831419
#>       [5,] 0.3328601 0.2294436 0.26109017 0.13731581 0.11915153 0.06640437
#>       [6,] 0.5676608 0.5359114 0.47072608 0.31140314 0.08811018 0.05479811
#>           
#> iterations         55         56
#>       [1,] 0.03210336 0.01565062
#>       [2,] 0.03103897 0.03238384
#>       [3,] 0.04336585 0.03380326
#>       [4,] 0.02709025 0.03085190
#>       [5,] 0.07853680 0.03662929
#>       [6,] 0.04396288 0.02220020

Created on 2023-01-09 with reprex v2.0.2

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.2.1 (2022-06-23) #> os macOS Ventura 13.1 #> system aarch64, darwin20 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/Denver #> date 2023-01-09 #> pandoc 2.19.2 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown) #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date (UTC) lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.2.0) #> base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.2.0) #> bayesplot 1.10.0 2022-11-16 [1] CRAN (R 4.2.1) #> boot 1.3-28.1 2022-11-22 [1] CRAN (R 4.2.1) #> callr 3.7.3 2022-11-02 [1] CRAN (R 4.2.0) #> cli 3.5.0 2022-12-20 [1] CRAN (R 4.2.0) #> codetools 0.2-18 2020-11-04 [1] CRAN (R 4.2.1) #> colorspace 2.0-3 2022-02-21 [1] CRAN (R 4.2.0) #> colourpicker 1.2.0 2022-10-28 [1] CRAN (R 4.2.0) #> crayon 1.5.2 2022-09-29 [1] CRAN (R 4.2.0) #> crosstalk 1.2.0 2021-11-04 [1] CRAN (R 4.2.0) #> DBI 1.1.3 2022-06-18 [1] CRAN (R 4.2.0) #> digest 0.6.31 2022-12-11 [1] CRAN (R 4.2.0) #> dplyr 1.0.10 2022-09-01 [1] CRAN (R 4.2.0) #> DT 0.26 2022-10-19 [1] CRAN (R 4.2.0) #> dygraphs 1.1.1.6 2018-07-11 [1] CRAN (R 4.2.0) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.2.0) #> evaluate 0.19 2022-12-13 [1] CRAN (R 4.2.1) #> fansi 1.0.3 2022-03-24 [1] CRAN (R 4.2.0) #> fastmap 1.1.0 2021-01-25 [1] CRAN (R 4.2.0) #> fs 1.5.2 2021-12-08 [1] CRAN (R 4.2.0) #> generics 0.1.3 2022-07-05 [1] CRAN (R 4.2.0) #> ggplot2 3.4.0 2022-11-04 [1] CRAN (R 4.2.1) #> glue 1.6.2 2022-02-24 [1] CRAN (R 4.2.0) #> gridExtra 2.3 2017-09-09 [1] CRAN (R 4.2.0) #> gtable 0.3.1 2022-09-01 [1] CRAN (R 4.2.0) #> gtools 3.9.4 2022-11-27 [1] CRAN (R 4.2.0) #> highr 0.10 2022-12-22 [1] CRAN (R 4.2.0) #> htmltools 0.5.4 2022-12-07 [1] CRAN (R 4.2.0) #> htmlwidgets 1.6.1 2023-01-07 [1] CRAN (R 4.2.1) #> httpuv 1.6.7 2022-12-14 [1] CRAN (R 4.2.0) #> igraph 1.3.5 2022-09-22 [1] CRAN (R 4.2.0) #> inline 0.3.19 2021-05-31 [1] CRAN (R 4.2.0) #> knitr 1.41 2022-11-18 [1] CRAN (R 4.2.0) #> later 1.3.0 2021-08-18 [1] CRAN (R 4.2.0) #> lattice 0.20-45 2021-09-22 [1] CRAN (R 4.2.1) #> lifecycle 1.0.3 2022-10-07 [1] CRAN (R 4.2.0) #> lme4 1.1-31 2022-11-01 [1] CRAN (R 4.2.0) #> loo 2.5.1 2022-03-24 [1] CRAN (R 4.2.0) #> magrittr 2.0.3 2022-03-30 [1] CRAN (R 4.2.0) #> markdown 1.4 2022-11-16 [1] CRAN (R 4.2.1) #> MASS 7.3-58.1 2022-08-03 [1] CRAN (R 4.2.0) #> Matrix 1.5-3 2022-11-11 [1] CRAN (R 4.2.0) #> matrixStats 0.63.0 2022-11-18 [1] CRAN (R 4.2.0) #> mime 0.12 2021-09-28 [1] CRAN (R 4.2.0) #> miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.2.0) #> minqa 1.2.5 2022-10-19 [1] CRAN (R 4.2.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.2.0) #> nlme 3.1-161 2022-12-15 [1] CRAN (R 4.2.0) #> nloptr 2.0.3 2022-05-26 [1] CRAN (R 4.2.0) #> pillar 1.8.1 2022-08-19 [1] CRAN (R 4.2.0) #> pkgbuild 1.4.0 2022-11-27 [1] CRAN (R 4.2.1) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.2.0) #> plyr 1.8.8 2022-11-11 [1] CRAN (R 4.2.0) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.2.0) #> processx 3.8.0 2022-10-26 [1] CRAN (R 4.2.0) #> promises 1.2.0.1 2021-02-11 [1] CRAN (R 4.2.0) #> ps 1.7.2 2022-10-26 [1] CRAN (R 4.2.0) #> purrr 1.0.0 2022-12-20 [1] CRAN (R 4.2.0) #> R.cache 0.16.0 2022-07-21 [1] CRAN (R 4.2.0) #> R.methodsS3 1.8.2 2022-06-13 [1] CRAN (R 4.2.0) #> R.oo 1.25.0 2022-06-12 [1] CRAN (R 4.2.0) #> R.utils 2.12.2 2022-11-11 [1] CRAN (R 4.2.1) #> R6 2.5.1 2021-08-19 [1] CRAN (R 4.2.0) #> Rcpp * 1.0.9 2022-07-08 [1] CRAN (R 4.2.0) #> RcppParallel 5.1.5 2022-01-05 [1] CRAN (R 4.2.0) #> reprex 2.0.2 2022-08-17 [1] CRAN (R 4.2.1) #> reshape2 1.4.4 2020-04-09 [1] CRAN (R 4.2.0) #> rlang 1.0.6 2022-09-24 [1] CRAN (R 4.2.0) #> rmarkdown 2.19 2022-12-15 [1] CRAN (R 4.2.1) #> rstan 2.21.7 2022-09-08 [1] CRAN (R 4.2.0) #> rstanarm * 2.21.3 2022-04-09 [1] CRAN (R 4.2.0) #> rstantools 2.2.0 2022-04-08 [1] CRAN (R 4.2.0) #> rstudioapi 0.14 2022-08-22 [1] CRAN (R 4.2.1) #> scales 1.2.1 2022-08-20 [1] CRAN (R 4.2.0) #> sessioninfo 1.2.2 2021-12-06 [1] CRAN (R 4.2.0) #> shiny 1.7.4 2022-12-15 [1] CRAN (R 4.2.1) #> shinyjs 2.1.0 2021-12-23 [1] CRAN (R 4.2.0) #> shinystan 2.6.0 2022-03-03 [1] CRAN (R 4.2.0) #> shinythemes 1.2.0 2021-01-25 [1] CRAN (R 4.2.0) #> StanHeaders 2.21.0-7 2020-12-17 [1] CRAN (R 4.2.0) #> stringi 1.7.8 2022-07-11 [1] CRAN (R 4.2.0) #> stringr 1.5.0 2022-12-02 [1] CRAN (R 4.2.0) #> styler 1.8.1 2022-11-07 [1] CRAN (R 4.2.1) #> survival 3.4-0 2022-08-09 [1] CRAN (R 4.2.0) #> threejs 0.3.3 2020-01-21 [1] CRAN (R 4.2.0) #> tibble 3.1.8 2022-07-22 [1] CRAN (R 4.2.0) #> tidyselect 1.2.0 2022-10-10 [1] CRAN (R 4.2.1) #> utf8 1.2.2 2021-07-24 [1] CRAN (R 4.2.0) #> vctrs 0.5.1 2022-11-16 [1] CRAN (R 4.2.0) #> withr 2.5.0 2022-03-03 [1] CRAN (R 4.2.0) #> xfun 0.36 2022-12-21 [1] CRAN (R 4.2.0) #> xtable 1.8-4 2019-04-21 [1] CRAN (R 4.2.0) #> xts 0.12.2 2022-10-16 [1] CRAN (R 4.2.0) #> yaml 2.3.6 2022-10-18 [1] CRAN (R 4.2.0) #> zoo 1.8-11 2022-09-17 [1] CRAN (R 4.2.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library #> #> ────────────────────────────────────────────────────────────────────────────── ```

This works for me also if I namespace this directly like rstanarm::posterior_epred(). Could you update your example to use the reprex package, and show directly the code that is causing you a problem? Using reprex makes it easier to see both the input and output, and for us to re-run the code in a local session. Thanks! 🙌

sunhuaiyu commented 1 year ago

Thanks for the response. But the only code I would have is reprex::reprex({library(tidyposterior)}). I am pretty sure it has to with posterior_epred() no longer being exported by rstanarm 2.21.3. (https://github.com/stan-dev/rstanarm/tree/master/R)

I down-graded rstanarm to 2.21.1 and it seems to solve the issue.

github-actions[bot] commented 1 year ago

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.