ASKurz / Statistical_Rethinking_with_brms_ggplot2_and_the_tidyverse_2_ed

The bookdown version lives here:
https://bookdown.org/content/4857/
Creative Commons Zero v1.0 Universal
125 stars 37 forks source link

3.4 brms::fitted() results in "not an exported object from 'namespace:brms'" error #49

Open petzi53 opened 1 year ago

petzi53 commented 1 year ago

I am new to Bayesian statistics and generally do not have much experience with statistics. In reading McElreath Statistical Rethinking, I enjoy very much your translation to the tidyverse approach. Thank you!

You mentioned in section 3.4. in the function brms::fitted() in one paragraph twice.

Much like the way we used the samples() function to simulate probability values, above, we can do so with the brms::fitted() function. But we will have to specify scale = "linear" in order to return results in the probability metric. By default, brms::fitted() will return summary information. Since we want actual simulation draws, we’ll specify summary = F.

But brms::fitted() results in an error:

Error: 'fitted' is not an exported object from 'namespace:brms'

There is no error in the following code chunk, but it just used fitted(). Could it be that here the base R function stats::fitted() is called and that brms::fitted() does not exist?

hamedbh commented 1 year ago

The error message is correct that brms::fitted() doesn't exist, at least not exactly. What exists is a function brms::fitted.brmsfit(), which is an S3 method. R then knows "when an object has the class brmsfit and the user calls fitted() on it, despatch that call to the fitted.brmsfit() method in the brms package`".

This is why when you have such an object (here that's b3.1) you can just call fitted(b3.1) and everything works, but adding the namespace explicitly doesn't. The S3 system is helping to make your life a bit easier.

petzi53 commented 1 year ago

Thank you for the wonderful explication! Indeed, the b3.1 object has the class "brmsfit". But shouldn't then brms::fitted.brmsfit() work? (It generates for me the same error: 'fitted.brmsfit' is not an exported object from 'namespace:brms') This is weird for me, as the {brms} help file clearly shows that fitted.brmsfit() exists. Could it be that there is something wrong with my installation? (I add details at the end of this post.)

Using just fitted() is working, but my result does not conform with Kurtz's p values: 0.6318994, 0.8105015, 0.7677781, 0.7250286, 0.7265799, 0.7376768, 0.5400708, 0.6697464,… in contrast to Kurtz's values: 0.6878563, 0.5386513, 0.7030050, 0.6889854, 0.4738290, 0.5879676, 0.5843004, 0.7014505, …

Sorry, I still haven't read the S3 chapter in R Advanced, maybe then I would be able to solve my problem and wouldn't bother you.


Quarto check

[✓] Checking versions of quarto binary dependencies... Pandoc version 3.1.5: OK Dart Sass version 1.55.0: OK Deno version 1.33.2: OK [✓] Checking versions of quarto dependencies......OK [✓] Checking Quarto installation......OK Version: 1.4.232 Path: /Applications/quarto/bin

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK Version: 3.8.3 Path: /Library/Frameworks/Python.framework/Versions/3.8/bin/python3 Jupyter: 5.3.0 Kernels: python3

[✓] Checking Jupyter engine render....OK

(-) Checking R installation...........R version 4.3.1 (2023-06-16) [✓] Checking R installation...........OK Version: 4.3.1 Path: /Library/Frameworks/R.framework/Resources LibPaths:

[✓] Checking Knitr engine render......OK


Session Info

R version 4.3.1 (2023-06-16) Platform: x86_64-apple-darwin20 (64-bit) Running under: macOS Ventura 13.4.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-x86_64/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: Europe/Vienna tzcode source: internal

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

other attached packages: [1] brms_2.19.0 Rcpp_1.0.11 lubridate_1.9.2 forcats_1.0.0 stringr_1.5.0
[6] dplyr_1.1.2 purrr_1.0.1 readr_2.1.4 tidyr_1.3.0 tibble_3.2.1
[11] ggplot2_3.4.2 tidyverse_2.0.0 testthat_3.1.10 devtools_2.4.5 usethis_2.2.2

loaded via a namespace (and not attached): [1] tensorA_0.36.2 rstudioapi_0.15.0 jsonlite_1.8.7 shape_1.4.6
[5] magrittr_2.0.3 TH.data_1.1-2 estimability_1.4.1 farver_2.1.1
[9] nloptr_2.0.3 fs_1.6.3 vctrs_0.6.3 memoise_2.0.1
[13] minqa_1.2.5 base64enc_0.1-3 htmltools_0.5.5 distributional_0.3.2 [17] curl_5.0.1 tidybayes_3.0.4 StanHeaders_2.26.27 htmlwidgets_1.6.2
[21] plyr_1.8.8 sandwich_3.0-2 emmeans_1.8.7 zoo_1.8-12
[25] cachem_1.0.8 igraph_1.5.0.1 mime_0.12 lifecycle_1.0.3
[29] pkgconfig_2.0.3 colourpicker_1.2.0 Matrix_1.6-0 R6_2.5.1
[33] fastmap_1.1.1 shiny_1.7.4.1 digest_0.6.33 colorspace_2.1-0
[37] ps_1.7.5 pkgload_1.3.2.1 crosstalk_1.2.0 projpred_2.6.0
[41] timechange_0.2.0 fansi_1.0.4 abind_1.4-5 mgcv_1.9-0
[45] compiler_4.3.1 remotes_2.4.2.1 withr_2.5.0 backports_1.4.1
[49] inline_0.3.19 shinystan_2.6.0 rethinking_2.31 gamm4_0.2-6
[53] pkgbuild_1.4.2 MASS_7.3-60 sessioninfo_1.2.2 gtools_3.9.4
[57] loo_2.6.0 tools_4.3.1 httpuv_1.6.11 threejs_0.3.3
[61] glue_1.6.2 callr_3.7.3 nlme_3.1-162 promises_1.2.0.1
[65] grid_4.3.1 cmdstanr_0.5.3 checkmate_2.2.0 reshape2_1.4.4
[69] generics_0.1.3 gtable_0.3.3 tzdb_0.4.0 hms_1.1.3
[73] xml2_1.3.5 utf8_1.2.3 pillar_1.9.0 ggdist_3.3.0
[77] markdown_1.7 posterior_1.4.1 later_1.3.1 splines_4.3.1
[81] lattice_0.21-8 survival_3.5-5 tidyselect_1.2.0 miniUI_0.1.1.1
[85] knitr_1.43 arrayhelpers_1.1-0 gridExtra_2.3 V8_4.3.3
[89] rversions_2.1.2 stats4_4.3.1 xfun_0.39 bridgesampling_1.1-2 [93] brio_1.1.3 matrixStats_1.0.0 DT_0.28 rstan_2.26.22
[97] stringi_1.7.12 boot_1.3-28.1 codetools_0.2-19 cli_3.6.1
[101] RcppParallel_5.1.7 shinythemes_1.2.0 xtable_1.8-4 munsell_0.5.0
[105] processx_3.8.2 coda_0.19-4 svUnit_1.0.6 parallel_4.3.1
[109] rstantools_2.3.1.1 ellipsis_0.3.2 prettyunits_1.1.1 dygraphs_1.1.1.6
[113] profvis_0.3.8 urlchecker_1.0.1 bayesplot_1.10.0 Brobdingnag_1.2-9
[117] lme4_1.1-34 mvtnorm_1.2-2 scales_1.2.1 xts_0.13.1
[121] crayon_1.5.2 rlang_1.1.1 multcomp_1.4-25 shinyjs_2.1.0

ASKurz commented 1 year ago

Hey @petzi53, thank you for raising the issue, and thank you, @hamedbh for the helpful response. I have to confess this technical issue is brushing up agains the boundaries of my knowledge. I haven't read the S3 chapter in R Advanced either. I don't have a full solution at the moment, but I may consider dropping the language of brms::fitted() if it's misleading.

hamedbh commented 1 year ago

@petzi53 if you were to try brms:::fitted.brmsfit() it should work. That’s with three colons, which allows you to access objects that aren’t exported. It’s a useful exercise to play with it and figure out the differences between ordinary functions and S3 methods, but in general I would be disinclined to directly access objects that aren’t exported in this way. The package developer provides these methods partly as a convenience to the user (because you can just call fitted(b3.1) or similar) but also to limit the user-facing parts of the package.

@ASKurz I think you’re right that removing the reference to brms::fitted() would be wise. Perhaps a whole explanation on S3 methods would be a distraction though: not sure it would really help things. Perhaps just change the text to read “… we can do so with the fitted() method”, and link to that documentation page for fitted.brmsfit() that I linked above?

petzi53 commented 1 year ago

@hamedbh Yep, this worked! Thank you!

(I always thought that three colons are used for internal – not documented – functions. But fitted.brmsfit() has documentation.)

However, my results are still the same as mentioned above and very different from Kurz's version.


@ASKurz I just noticed that you have published another version. Which one is the newest?

In both versions, you mentioned a vector of 4,000 but in the example published from this repo here we both have 16,000 rows.

hamedbh commented 1 year ago

@petzi53 the three colons are for objects that are not exported: often these are undocumented, but not necessarily.

ASKurz commented 1 year ago

@hamedbh, I have ebooks connected to the first and second edition of McElreath's textbook. This repo is for my ebook connected to his 2nd edition. The other version you linked to is for me ebook connected to his 1st edition. The content in the two is similar, but not completely overlapping.