bayesstuff / stanova

8 stars 0 forks source link

Stanova does not like to be called with :: #4

Closed vandenman closed 4 years ago

vandenman commented 4 years ago

The first example from ?stanova::stanova crashes if you do not call library but instead do stanova::stanova. See below for a reprex.

data("warpbreaks")
fit_warp <- stanova::stanova(breaks ~ wool * tension, data = warpbreaks,
                    prior = rstanarm::student_t(3, 0, 3, autoscale = FALSE),
                    model_fun = "glm",
                    chains = 2, iter = 500)
#> Loading required namespace: rstanarm
#> Contrasts set to contr.bayes for the following variables: wool, tension
#> Error in get(ctr, mode = "function", envir = parent.frame()): object 'contr.bayes' of mode 'function' was not found

Created on 2020-09-18 by the reprex package (v0.3.0)

Session info ``` r devtools::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.0.2 (2020-06-22) #> os Ubuntu 18.04.5 LTS #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz Europe/Amsterdam #> date 2020-09-18 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.0.2) #> backports 1.1.10 2020-09-15 [1] CRAN (R 4.0.2) #> base64enc 0.1-3 2015-07-28 [1] CRAN (R 4.0.2) #> bayesplot 1.7.2 2020-05-28 [1] CRAN (R 4.0.2) #> boot 1.3-25 2020-04-26 [1] CRAN (R 4.0.2) #> callr 3.4.4 2020-09-07 [1] CRAN (R 4.0.2) #> cli 2.0.2 2020-02-28 [1] CRAN (R 4.0.2) #> codetools 0.2-16 2018-12-24 [1] CRAN (R 4.0.2) #> colorspace 1.4-1 2019-03-18 [1] CRAN (R 4.0.2) #> colourpicker 1.1.0 2020-09-14 [1] CRAN (R 4.0.2) #> crayon 1.3.4 2017-09-16 [1] CRAN (R 4.0.2) #> crosstalk 1.1.0.1 2020-03-13 [1] CRAN (R 4.0.2) #> curl 4.3 2019-12-02 [1] CRAN (R 4.0.2) #> desc 1.2.0 2018-05-01 [1] CRAN (R 4.0.2) #> devtools 2.3.2 2020-09-18 [1] CRAN (R 4.0.2) #> digest 0.6.25 2020-02-23 [1] CRAN (R 4.0.2) #> dplyr 1.0.2 2020-08-18 [1] CRAN (R 4.0.2) #> DT 0.15 2020-08-05 [1] CRAN (R 4.0.2) #> dygraphs 1.1.1.6 2018-07-11 [1] CRAN (R 4.0.2) #> ellipsis 0.3.1 2020-05-15 [1] CRAN (R 4.0.2) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.0.2) #> fansi 0.4.1 2020-01-08 [1] CRAN (R 4.0.2) #> fastmap 1.0.1 2019-10-08 [1] CRAN (R 4.0.2) #> fs 1.5.0 2020-07-31 [1] CRAN (R 4.0.2) #> generics 0.0.2 2018-11-29 [1] CRAN (R 4.0.2) #> ggplot2 3.3.2 2020-06-19 [1] CRAN (R 4.0.2) #> ggridges 0.5.2 2020-01-12 [1] CRAN (R 4.0.2) #> glue 1.4.2 2020-08-27 [1] CRAN (R 4.0.2) #> gridExtra 2.3 2017-09-09 [1] CRAN (R 4.0.2) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.0.2) #> gtools 3.8.2 2020-03-31 [1] CRAN (R 4.0.2) #> highr 0.8 2019-03-20 [1] CRAN (R 4.0.2) #> htmltools 0.5.0 2020-06-16 [1] CRAN (R 4.0.2) #> htmlwidgets 1.5.1 2019-10-08 [1] CRAN (R 4.0.2) #> httpuv 1.5.4 2020-06-06 [1] CRAN (R 4.0.2) #> igraph 1.2.5 2020-03-19 [1] CRAN (R 4.0.2) #> inline 0.3.16 2020-09-06 [1] CRAN (R 4.0.2) #> jsonlite 1.7.1 2020-09-07 [1] CRAN (R 4.0.2) #> knitr 1.29 2020-06-23 [1] CRAN (R 4.0.2) #> later 1.1.0.1 2020-06-05 [1] CRAN (R 4.0.2) #> lattice 0.20-41 2020-04-02 [1] CRAN (R 4.0.2) #> lifecycle 0.2.0 2020-03-06 [1] CRAN (R 4.0.2) #> lme4 1.1-23 2020-04-07 [1] CRAN (R 4.0.2) #> loo 2.3.1 2020-07-14 [1] CRAN (R 4.0.2) #> magrittr 1.5 2014-11-22 [1] CRAN (R 4.0.2) #> markdown 1.1 2019-08-07 [1] CRAN (R 4.0.2) #> MASS 7.3-53 2020-09-09 [1] CRAN (R 4.0.2) #> Matrix 1.2-18 2019-11-27 [1] CRAN (R 4.0.2) #> matrixStats 0.56.0 2020-03-13 [1] CRAN (R 4.0.2) #> memoise 1.1.0 2017-04-21 [1] CRAN (R 4.0.2) #> mime 0.9 2020-02-04 [1] CRAN (R 4.0.2) #> miniUI 0.1.1.1 2018-05-18 [1] CRAN (R 4.0.2) #> minqa 1.2.4 2014-10-09 [1] CRAN (R 4.0.2) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.0.2) #> nlme 3.1-149 2020-08-23 [1] CRAN (R 4.0.2) #> nloptr 1.2.2.2 2020-07-02 [1] CRAN (R 4.0.2) #> pillar 1.4.6 2020-07-10 [1] CRAN (R 4.0.2) #> pkgbuild 1.1.0 2020-07-13 [1] CRAN (R 4.0.2) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.0.2) #> pkgload 1.1.0 2020-05-29 [1] CRAN (R 4.0.2) #> plyr 1.8.6 2020-03-03 [1] CRAN (R 4.0.2) #> prettyunits 1.1.1 2020-01-24 [1] CRAN (R 4.0.2) #> processx 3.4.4 2020-09-03 [1] CRAN (R 4.0.2) #> promises 1.1.1 2020-06-09 [1] CRAN (R 4.0.2) #> ps 1.3.4 2020-08-11 [1] CRAN (R 4.0.2) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.0.2) #> R6 2.4.1 2019-11-12 [1] CRAN (R 4.0.2) #> Rcpp 1.0.5 2020-07-06 [1] CRAN (R 4.0.2) #> RcppParallel 5.0.2 2020-06-24 [1] CRAN (R 4.0.2) #> remotes 2.2.0 2020-07-21 [1] CRAN (R 4.0.2) #> reshape2 1.4.4 2020-04-09 [1] CRAN (R 4.0.2) #> rlang 0.4.7 2020-07-09 [1] CRAN (R 4.0.2) #> rmarkdown 2.3 2020-06-18 [1] CRAN (R 4.0.2) #> rprojroot 1.3-2 2018-01-03 [1] CRAN (R 4.0.2) #> rsconnect 0.8.16 2019-12-13 [1] CRAN (R 4.0.2) #> rstan 2.21.2 2020-07-27 [1] CRAN (R 4.0.2) #> rstanarm 2.21.1 2020-07-20 [1] CRAN (R 4.0.2) #> rstantools 2.1.1 2020-07-06 [1] CRAN (R 4.0.2) #> scales 1.1.1 2020-05-11 [1] CRAN (R 4.0.2) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.0.2) #> shiny 1.5.0 2020-06-23 [1] CRAN (R 4.0.2) #> shinyjs 2.0.0 2020-09-09 [1] CRAN (R 4.0.2) #> shinystan 2.5.0 2018-05-01 [1] CRAN (R 4.0.2) #> shinythemes 1.1.2 2018-11-06 [1] CRAN (R 4.0.2) #> StanHeaders 2.21.0-6 2020-08-16 [1] CRAN (R 4.0.2) #> stanova 0.3-0 2020-09-18 [1] Github (Bayesstuff/stanova@7088a67) #> statmod 1.4.34 2020-02-17 [1] CRAN (R 4.0.2) #> stringi 1.5.3 2020-09-09 [1] CRAN (R 4.0.2) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.0.2) #> survival 3.2-3 2020-06-13 [1] CRAN (R 4.0.2) #> testthat 2.3.2 2020-03-02 [1] CRAN (R 4.0.2) #> threejs 0.3.3 2020-01-21 [1] CRAN (R 4.0.2) #> tibble 3.0.3 2020-07-10 [1] CRAN (R 4.0.2) #> tidyselect 1.1.0 2020-05-11 [1] CRAN (R 4.0.2) #> usethis 1.6.3 2020-09-17 [1] CRAN (R 4.0.2) #> V8 3.2.0 2020-06-19 [1] CRAN (R 4.0.2) #> vctrs 0.3.4 2020-08-29 [1] CRAN (R 4.0.2) #> withr 2.2.0 2020-04-20 [1] CRAN (R 4.0.2) #> xfun 0.17 2020-09-09 [1] CRAN (R 4.0.2) #> xtable 1.8-4 2019-04-21 [1] CRAN (R 4.0.2) #> xts 0.12.1 2020-09-09 [1] CRAN (R 4.0.2) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.0.2) #> zoo 1.8-8 2020-05-02 [1] CRAN (R 4.0.2) #> #> [1] /home/dvdb/R/custom_R/R-4.0.2/library ```
singmann commented 4 years ago

Indeed, but honestly a rather minor issue that can be easily avoided. Nevertheless, it should now (1e764cb7fa9b1a60951dc68f13723138adc6cee7) work. However, you need to pass the contrast function as a function and not as the name of a function to check_contrasts (e.g., check_contrasts = stanova::contr.bayes):

fit_warp <- stanova::stanova(breaks ~ wool * tension, data = warpbreaks,
                    prior = rstanarm::student_t(3, 0, 3, autoscale = FALSE),
                    model_fun = "glm",
                    chains = 2, iter = 500)
#> Loading required namespace: rstanarm
#> Contrasts set to contr.bayes for the following variables: wool, tension
#> Error in get(ctr, mode = "function", envir = parent.frame()): object 'contr.bayes' of mode 'function' was not found

fit_warp <- stanova::stanova(breaks ~ wool * tension, data = warpbreaks,
                    prior = rstanarm::student_t(3, 0, 3, autoscale = FALSE),
                    model_fun = "glm",
                    chains = 2, iter = 500, 
                    check_contrasts = stanova::contr.bayes)
#> Contrasts set to passed contrast function for the following variables: wool, tension
#> 
#> SAMPLING FOR MODEL 'continuous' NOW (CHAIN 1).
#> Chain 1: 
#> Chain 1: Gradient evaluation took 0 seconds
#> Chain 1: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
#> Chain 1: Adjust your expectations accordingly!
#> Chain 1: 
#> Chain 1: 
#> Chain 1: Iteration:   1 / 500 [  0%]  (Warmup)
#> Chain 1: Iteration:  50 / 500 [ 10%]  (Warmup)
#> Chain 1: Iteration: 100 / 500 [ 20%]  (Warmup)
#> Chain 1: Iteration: 150 / 500 [ 30%]  (Warmup)
#> Chain 1: Iteration: 200 / 500 [ 40%]  (Warmup)
#> Chain 1: Iteration: 250 / 500 [ 50%]  (Warmup)
#> Chain 1: Iteration: 251 / 500 [ 50%]  (Sampling)
#> Chain 1: Iteration: 300 / 500 [ 60%]  (Sampling)
#> Chain 1: Iteration: 350 / 500 [ 70%]  (Sampling)
#> Chain 1: Iteration: 400 / 500 [ 80%]  (Sampling)
#> Chain 1: Iteration: 450 / 500 [ 90%]  (Sampling)
#> Chain 1: Iteration: 500 / 500 [100%]  (Sampling)
#> Chain 1: 
#> Chain 1:  Elapsed Time: 0.061 seconds (Warm-up)
#> Chain 1:                0.036 seconds (Sampling)
#> Chain 1:                0.097 seconds (Total)
#> Chain 1: 
#> 
#> SAMPLING FOR MODEL 'continuous' NOW (CHAIN 2).
#> Chain 2: 
#> Chain 2: Gradient evaluation took 0 seconds
#> Chain 2: 1000 transitions using 10 leapfrog steps per transition would take 0 seconds.
#> Chain 2: Adjust your expectations accordingly!
#> Chain 2: 
#> Chain 2: 
#> Chain 2: Iteration:   1 / 500 [  0%]  (Warmup)
#> Chain 2: Iteration:  50 / 500 [ 10%]  (Warmup)
#> Chain 2: Iteration: 100 / 500 [ 20%]  (Warmup)
#> Chain 2: Iteration: 150 / 500 [ 30%]  (Warmup)
#> Chain 2: Iteration: 200 / 500 [ 40%]  (Warmup)
#> Chain 2: Iteration: 250 / 500 [ 50%]  (Warmup)
#> Chain 2: Iteration: 251 / 500 [ 50%]  (Sampling)
#> Chain 2: Iteration: 300 / 500 [ 60%]  (Sampling)
#> Chain 2: Iteration: 350 / 500 [ 70%]  (Sampling)
#> Chain 2: Iteration: 400 / 500 [ 80%]  (Sampling)
#> Chain 2: Iteration: 450 / 500 [ 90%]  (Sampling)
#> Chain 2: Iteration: 500 / 500 [100%]  (Sampling)
#> Chain 2: 
#> Chain 2:  Elapsed Time: 0.093 seconds (Warm-up)
#> Chain 2:                0.035 seconds (Sampling)
#> Chain 2:                0.128 seconds (Total)
#> Chain 2:
summary(fit_warp)
#> 
#> Model Info:
#>  function:     stanova_glm
#>  family:       gaussian [identity]
#>  formula:      breaks ~ wool * tension
#>  algorithm:    sampling
#>  chains:       2
#>  sample:       500 (posterior sample size)
#>  priors:       see help('prior_summary', package = 'rstanarm')
#>  observations: 54
#> 
#> Estimate Intercept:
#>      Variable   Mean MAD_SD     5%    50%    95%  rhat ess_bulk ess_tail
#> 1 (Intercept) 28.027  1.662 25.514 28.069 30.656 1.004  482.179  357.213
#> 
#> 
#> Estimates 'wool' - difference from intercept:
#>   Variable   Mean MAD_SD     5%    50%   95%  rhat ess_bulk ess_tail
#> 1   wool A  1.963  1.328 -0.162  1.899 4.131 1.002  353.813  377.590
#> 2   wool B -1.963  1.328 -4.131 -1.899 0.162 1.002  353.813  377.590
#> 
#> Estimates 'tension' - difference from intercept:
#>    Variable   Mean MAD_SD     5%    50%    95%  rhat ess_bulk ess_tail
#> 1 tension L  6.312  2.390  2.640  6.252 10.133 1.003  398.264  357.729
#> 2 tension M -1.735  1.968 -4.888 -1.736  1.052 1.009  564.715  379.546
#> 3 tension H -4.576  1.803 -7.760 -4.511 -1.803 1.003  402.858  408.003
#> 
#> Estimates 'wool:tension' - difference from intercept:
#>            Variable   Mean MAD_SD      5%    50%    95%  rhat ess_bulk ess_tail
#> 1 wool A, tension L 11.212  3.536   5.837 11.145 16.415 1.006  340.548  336.503
#> 2 wool B, tension L  1.411  3.378  -3.605  1.329  6.487 0.998  418.178  349.710
#> 3 wool A, tension M -2.404  2.951  -7.211 -2.604  2.581 1.006  376.438  310.679
#> 4 wool B, tension M -1.067  2.615  -5.217 -1.201  3.377 1.002  476.715  424.271
#> 5 wool A, tension H -2.919  2.942  -7.445 -3.009  1.839 1.006  422.376  441.116
#> 6 wool B, tension H -6.233  2.745 -10.941 -6.019 -1.574 0.999  442.895  353.688

Created on 2020-09-18 by the reprex package (v0.3.0)