Closed sunhuaiyu closed 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
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! 🙌
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.
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.
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.