Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_parameters() and find_parameters() should indicate fixed and random effects for BayesFactor::anovaBF #169

Closed
IndrajeetPatil opened this issue Dec 17, 2019 · 7 comments

Comments

@IndrajeetPatil
Copy link
Member

library(BayesFactor)
#> Loading required package: coda
#> Loading required package: Matrix
#> ************
#> Welcome to BayesFactor 0.9.12-4.2. If you have questions, please contact Richard Morey ([email protected]).
#> 
#> Type BFManual() to open the manual.
#> ************
library(parameters)

model <- anovaBF(extra ~ group + ID, data = sleep, whichRandom = "ID",
        progress = FALSE)

model_parameters(model) 
#> Error in UseMethod("point_estimate"): no applicable method for 'point_estimate' applied to an object of class "NULL"

Created on 2019-12-17 by the reprex package (v0.3.0)

Traceback:

> traceback()
8: point_estimate(insight::get_parameters(x), centrality = centrality, 
       dispersion = dispersion, ...)
7: point_estimate.BFBayesFactor(x, centrality = centrality, dispersion = dispersion, 
       ...)
6: point_estimate(x, centrality = centrality, dispersion = dispersion, 
       ...)
5: .describe_posterior(posteriors, centrality = centrality, dispersion = dispersion, 
       ci = ci, ci_method = ci_method, test = test, rope_range = rope_range, 
       rope_ci = rope_ci, ...)
4: describe_posterior.BFBayesFactor(model, centrality = centrality, 
       dispersion = dispersion, ci = ci, ci_method = ci_method, 
       test = test, rope_range = rope_range, rope_ci = rope_ci, 
       priors = priors, ...)
3: bayestestR::describe_posterior(model, centrality = centrality, 
       dispersion = dispersion, ci = ci, ci_method = ci_method, 
       test = test, rope_range = rope_range, rope_ci = rope_ci, 
       priors = priors, ...)
2: model_parameters.BFBayesFactor(model)
1: model_parameters(model)
Session info
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 3.6.2 (2019-12-12)
#>  os       macOS Mojave 10.14.6        
#>  system   x86_64, darwin15.6.0        
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Europe/Berlin               
#>  date     2019-12-17                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date       lib source                               
#>  assertthat     0.2.1      2019-03-21 [1] CRAN (R 3.6.0)                       
#>  backports      1.1.5      2019-10-02 [1] CRAN (R 3.6.0)                       
#>  BayesFactor  * 0.9.12-4.2 2018-05-19 [1] CRAN (R 3.6.0)                       
#>  bayestestR     0.4.9      2019-12-17 [1] Github (easystats/bayestestR@392c78c)
#>  callr          3.4.0      2019-12-09 [1] CRAN (R 3.6.0)                       
#>  cli            2.0.0      2019-12-09 [1] CRAN (R 3.6.0)                       
#>  coda         * 0.19-3     2019-07-05 [1] CRAN (R 3.6.0)                       
#>  crayon         1.3.4      2017-09-16 [1] CRAN (R 3.6.0)                       
#>  desc           1.2.0      2018-05-01 [1] CRAN (R 3.6.0)                       
#>  devtools       2.2.1      2019-09-24 [1] CRAN (R 3.6.0)                       
#>  digest         0.6.23     2019-11-23 [1] CRAN (R 3.6.0)                       
#>  ellipsis       0.3.0      2019-09-20 [1] CRAN (R 3.6.0)                       
#>  evaluate       0.14       2019-05-28 [1] CRAN (R 3.6.0)                       
#>  fansi          0.4.0      2018-10-05 [1] CRAN (R 3.6.0)                       
#>  fs             1.3.1      2019-05-06 [1] CRAN (R 3.6.0)                       
#>  glue           1.3.1      2019-03-12 [1] CRAN (R 3.6.0)                       
#>  gtools         3.8.1      2018-06-26 [1] CRAN (R 3.6.0)                       
#>  highr          0.8        2019-03-20 [1] CRAN (R 3.6.0)                       
#>  htmltools      0.4.0      2019-10-04 [1] CRAN (R 3.6.0)                       
#>  insight        0.7.1.1    2019-12-17 [1] Github (easystats/insight@5be7b4d)   
#>  knitr          1.26       2019-11-12 [1] CRAN (R 3.6.0)                       
#>  lattice        0.20-38    2018-11-04 [2] CRAN (R 3.6.2)                       
#>  magrittr       1.5        2014-11-22 [1] CRAN (R 3.6.0)                       
#>  Matrix       * 1.2-18     2019-11-27 [2] CRAN (R 3.6.2)                       
#>  MatrixModels   0.4-1      2015-08-22 [1] CRAN (R 3.6.0)                       
#>  memoise        1.1.0      2017-04-21 [1] CRAN (R 3.6.0)                       
#>  mvtnorm        1.0-11     2019-06-19 [1] CRAN (R 3.6.0)                       
#>  parameters   * 0.3.0.1    2019-12-17 [1] Github (easystats/parameters@29d23a4)
#>  pbapply        1.4-2      2019-08-31 [1] CRAN (R 3.6.0)                       
#>  pkgbuild       1.0.6      2019-10-09 [1] CRAN (R 3.6.0)                       
#>  pkgload        1.0.2      2018-10-29 [1] CRAN (R 3.6.0)                       
#>  prettyunits    1.0.2      2015-07-13 [1] CRAN (R 3.6.0)                       
#>  processx       3.4.1      2019-07-18 [1] CRAN (R 3.6.0)                       
#>  ps             1.3.0      2018-12-21 [1] CRAN (R 3.6.0)                       
#>  R6             2.4.1      2019-11-12 [1] CRAN (R 3.6.0)                       
#>  Rcpp           1.0.3      2019-11-08 [1] CRAN (R 3.6.1)                       
#>  remotes        2.1.0      2019-06-24 [1] CRAN (R 3.6.0)                       
#>  rlang          0.4.2.9000 2019-12-16 [1] Github (r-lib/rlang@ec7c1ed)         
#>  rmarkdown      2.0        2019-12-12 [1] CRAN (R 3.6.0)                       
#>  rprojroot      1.3-2      2018-01-03 [1] CRAN (R 3.6.0)                       
#>  sessioninfo    1.1.1      2018-11-05 [1] CRAN (R 3.6.0)                       
#>  stringi        1.4.3      2019-03-12 [1] CRAN (R 3.6.0)                       
#>  stringr        1.4.0      2019-02-10 [1] CRAN (R 3.6.0)                       
#>  testthat       2.3.1      2019-12-01 [1] CRAN (R 3.6.0)                       
#>  usethis        1.5.1      2019-07-04 [1] CRAN (R 3.6.0)                       
#>  withr          2.1.2      2018-03-15 [1] CRAN (R 3.6.0)                       
#>  xfun           0.11       2019-11-12 [1] CRAN (R 3.6.0)                       
#>  yaml           2.2.0      2018-07-25 [1] CRAN (R 3.6.0)                       
#> 
#> [1] /Users/patil/Library/R/3.6/library
#> [2] /Library/Frameworks/R.framework/Versions/3.6/Resources/library
@strengejacke
Copy link
Member

This errors due to insight::get_parameters(). What kind of parameters would make sense in this case?

library(insight)
library(BayesFactor)
#> Loading required package: coda
#> Loading required package: Matrix
#> ************
#> Welcome to BayesFactor 0.9.12-4.2. If you have questions, please contact Richard Morey ([email protected]).
#> 
#> Type BFManual() to open the manual.
#> ************

model <- anovaBF(extra ~ group + ID, data = sleep, whichRandom = "ID", progress = FALSE)

head(get_parameters(model))
#>         mu    group-1   group-2        ID-1       ID-2       ID-3      ID-4
#> 1 1.349286 -0.6724627 0.6724627  0.05559608 -2.1598342 -0.6760759 -1.047968
#> 2 1.692076 -0.7261666 0.7261666  0.04946193 -1.3462277 -1.2668723 -1.251121
#> 3 2.110250 -0.8819996 0.8819996 -0.12487609 -0.7093962 -1.1001693 -2.061756
#> 4 1.656555 -0.8587893 0.8587893  0.79552914 -1.1983304 -0.1355185 -1.472216
#> 5 1.895765 -0.4498286 0.4498286  0.95904814 -1.9519772 -0.9481232 -1.507108
#> 6 1.631278 -0.8018071 0.8018071  0.21136784 -1.0153058 -1.1680062 -1.009764
#>         ID-5     ID-6     ID-7       ID-8        ID-9       ID-10      sig2
#> 1 -0.7875713 1.568064 1.651395  0.3938788  1.65393017  1.15372957 0.9994526
#> 2 -0.7433016 2.044083 1.709569 -0.4861043  0.92190880  0.48956802 1.3592481
#> 3 -2.1731418 1.148342 1.158459 -0.2194751  0.48845869 -0.02081864 0.9389592
#> 4 -1.4777671 1.743779 1.229966 -0.5381937  0.32094642  1.06300767 0.8146430
#> 5 -1.6224734 1.504265 2.105043 -0.8283088 -0.03518938  1.02143941 0.9147814
#> 6 -1.4372970 2.115739 3.690448 -1.1648361 -0.06883875  0.99407781 1.0693874
#>     g_group      g_ID
#> 1 0.5444569 0.9252051
#> 2 0.8492134 1.3423385
#> 3 6.6524189 1.0730355
#> 4 0.7506118 1.1278043
#> 5 0.8147826 2.4118328
#> 6 1.8295035 2.0901768

find_parameters(model)
#> $conditional
#>  [1] "mu"      "group-1" "group-2" "ID-1"    "ID-2"    "ID-3"    "ID-4"   
#>  [8] "ID-5"    "ID-6"    "ID-7"    "ID-8"    "ID-9"    "ID-10"   "sig2"   
#> [15] "g_group" "g_ID"

Created on 2019-12-17 by the reprex package (v0.3.0)

@mattansb
Copy link
Member

@strengejacke note that BayesFactor::posterior needs index passed to it when there are more than one model (this is also true for directional ttests) - you might want to make this explicit in the documentation somehow?

library(insight)
library(BayesFactor)
#> Loading required package: coda
#> Loading required package: Matrix
#> ************
#> Welcome to BayesFactor 0.9.12-4.2. If you have questions, please contact Richard Morey ([email protected]).
#> 
#> Type BFManual() to open the manual.
#> ************
data("puzzles")

result <- anovaBF(
  RT ~ shape * color + ID,
  data = puzzles,
  whichRandom = "ID",
  progress = FALSE
)

ttest_res <-
  ttestBF(
    x = sleep$extra[sleep$group == 1],
    y = sleep$extra[sleep$group == 2],
    paired = TRUE,
    nullInterval = c(-Inf, 0)
  )

posterior(result, iterations = 10)
#> Error in posterior(result, iterations = 10): Index argument required for posterior with multiple numerators.
posterior(ttest_res, iterations = 10)
#> Error in posterior(ttest_res, iterations = 10): Index argument required for posterior with multiple numerators.

Created on 2019-12-17 by the reprex package (v0.3.0)

Also, if you wan find_parameters() to separate the parameters into random, then you can find the random factors in each model:

result@numerator$`shape + ID`@dataTypes
#>       ID    shape 
#> "random"  "fixed"

(But this is a whole mess on-to itself...)

@strengejacke
Copy link
Member

Also, if you wan find_parameters() to separate the parameters into random, then you can find the random factors in each model:

Would be nice, and I can even use insight::find_formula(result) to get fixed and random effects - but then I need to reconstruct the column names for the parameters... 😱

@strengejacke strengejacke transferred this issue from easystats/parameters Dec 17, 2019
@strengejacke
Copy link
Member

I think it's now mainly an insight issue, as it works with model_parameters() now.

Now we have to extract information on which parameter is random, and which is fixed, and make find_parameters() and get_parameters()´ work with the effects`-argument.

@strengejacke strengejacke changed the title model_parameters doesn't work with BayesFactor::anovaBF get_parameters() and find_parameters() should indicate fixed and random effects for BayesFactor::anovaBF Dec 17, 2019
@mattansb
Copy link
Member

I've been down that rabbit hole... Once you open that door...
Good luck!

@strengejacke
Copy link
Member

library(insight)
library(parameters)
#> 
#> Attaching package: 'parameters'
#> The following object is masked from 'package:insight':
#> 
#>     format_ci
library(BayesFactor)
#> Loading required package: coda
#> Loading required package: Matrix
#> ************
#> Welcome to BayesFactor 0.9.12-4.2. If you have questions, please contact Richard Morey ([email protected]).
#> 
#> Type BFManual() to open the manual.
#> ************

model <- anovaBF(extra ~ group + ID, data = sleep, whichRandom = "ID", progress = FALSE)

head(get_parameters(model))
#>         mu    group-1   group-2       ID-1      ID-2       ID-3       ID-4
#> 1 1.388413 -0.9373772 0.9373772 -0.3358237 -1.249506 -0.6663588 -0.1712864
#> 2 2.092704 -1.0004152 1.0004152 -0.4030889 -0.802145 -2.0117398 -2.2269061
#> 3 1.862832 -0.8652697 0.8652697 -1.4377261 -1.754347 -1.4568715 -1.8716563
#> 4 1.784898 -0.8077692 0.8077692 -0.2117907 -2.042515 -0.6654858 -2.3811870
#> 5 2.685579 -0.7216999 0.7216999 -0.8455328 -2.992526 -1.7699278 -3.3533533
#> 6 1.304258 -0.9785429 0.9785429  0.1042850 -2.167270 -1.0909156 -0.9915559
#>         ID-5      ID-6     ID-7       ID-8        ID-9     ID-10      sig2
#> 1 -0.7255611 1.8931277 1.899339  0.2131550  0.56946258 1.5719684 0.9627126
#> 2 -2.2192400 0.9967290 2.697733 -1.7158945  0.48580661 0.6370042 1.1319091
#> 3 -1.8016265 0.4559148 2.257765 -1.0178970 -0.03012636 1.3148016 0.6901860
#> 4 -1.6334668 1.6687262 2.523719 -0.6269279  0.56978852 1.4394503 0.7274266
#> 5 -2.7867715 1.8316788 2.167131  0.3504694  0.26296890 0.9842533 0.7864430
#> 6 -0.7278862 1.9251050 2.924684 -0.2444156  1.68773453 0.9569678 0.6179961
#>     g_group     g_ID
#> 1 0.5071210 1.526206
#> 2 5.5938528 2.530137
#> 3 1.6020113 2.011088
#> 4 0.2738577 5.318503
#> 5 0.7436001 7.866674
#> 6 1.9593037 2.744540
find_parameters(model)
#> $conditional
#> [1] "group-1" "group-2"
#> 
#> $random
#>  [1] "ID-1"  "ID-2"  "ID-3"  "ID-4"  "ID-5"  "ID-6"  "ID-7"  "ID-8"  "ID-9" 
#> [10] "ID-10"
#> 
#> $extra
#> [1] "mu"      "sig2"    "g_group" "g_ID"

model_parameters(model)
#> # Fixed Effects (Count Model) 
#> 
#> Parameter | Median |         89% CI |     pd | % in ROPE |    BF
#> ----------------------------------------------------------------
#> group-1   |  -0.67 | [-1.06, -0.28] | 99.30% |        0% | 11.73
#> group-2   |   0.67 | [ 0.28,  1.06] | 99.30% |        0% | 11.73
#> 
#> # Extra Parameters 
#> 
#> Parameter | Median |       89% CI |     pd | % in ROPE |    BF
#> --------------------------------------------------------------
#> mu        |   1.55 | [0.62, 2.44] | 99.28% |        0% | 11.73
#> sig2      |   1.00 | [0.34, 1.98] |   100% |        0% | 11.73
#> g_group   |   0.84 | [0.02, 5.73] |   100% |     3.17% | 11.73
#> g_ID      |   2.61 | [0.16, 6.71] |   100% |        0% | 11.73
#> 
#> # Random Effects (Count Model) 
#> 
#> Parameter | Median |         89% CI |     pd | % in ROPE |    BF
#> ----------------------------------------------------------------
#> ID-1      |  -0.21 | [-1.58,  1.07] | 59.33% |    11.60% | 11.73
#> ID-2      |  -1.54 | [-2.90, -0.21] | 96.28% |        0% | 11.73
#> ID-3      |  -0.88 | [-2.25,  0.46] | 86.02% |     6.57% | 11.73
#> ID-4      |  -1.68 | [-3.05, -0.30] | 97.40% |        0% | 11.73
#> ID-5      |  -1.33 | [-2.65,  0.01] | 94.55% |     2.81% | 11.73
#> ID-6      |   1.87 | [ 0.39,  3.17] | 98.28% |        0% | 11.73
#> ID-7      |   2.45 | [ 0.91,  3.80] | 99.48% |        0% | 11.73
#> ID-8      |  -0.29 | [-1.58,  1.03] | 64.05% |    11.06% | 11.73
#> ID-9      |   0.61 | [-0.69,  1.91] | 78.62% |     7.78% | 11.73
#> ID-10     |   0.90 | [-0.38,  2.31] | 87.02% |     5.48% | 11.73

Created on 2019-12-18 by the reprex package (v0.3.0)

@IndrajeetPatil
Copy link
Member Author

Awesome, thanks for fixing this so quickly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants