diff --git a/DESCRIPTION b/DESCRIPTION index 6e4cd9d26..e0d072773 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: bayestestR Title: Understand and Describe Bayesian Models and Posterior Distributions -Version: 0.13.1.1 +Version: 0.13.1.2 Authors@R: c(person(given = "Dominique", family = "Makowski", diff --git a/NAMESPACE b/NAMESPACE index 41ee4b250..851bbaedb 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -220,12 +220,14 @@ S3method(format,bayestestR_eti) S3method(format,bayestestR_hdi) S3method(format,bayestestR_si) S3method(format,describe_posterior) +S3method(format,equivalence_test) S3method(format,map_estimate) S3method(format,p_direction) S3method(format,p_map) S3method(format,p_rope) S3method(format,p_significance) S3method(format,point_estimate) +S3method(format,rope) S3method(hdi,BFBayesFactor) S3method(hdi,BGGM) S3method(hdi,MCMCglmm) diff --git a/NEWS.md b/NEWS.md index 898864459..87219d7cd 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,11 @@ * Retrieving models from the environment was improved. +## Bug fixes + +* Fixed issues in various `format()` methods, which did not work properly for + some few functions (like `p_direction()`). + # bayestestR 0.13.1 ## Changes diff --git a/R/bayesfactor_parameters.R b/R/bayesfactor_parameters.R index e13f4367c..ad82ee7bc 100644 --- a/R/bayesfactor_parameters.R +++ b/R/bayesfactor_parameters.R @@ -136,13 +136,13 @@ #' bayesfactor_parameters(group_diff, prior = stan_model, verbose = FALSE) #' #' # Or -#' group_diff_prior <- pairs(emmeans(unupdate(stan_model), ~group)) -#' bayesfactor_parameters(group_diff, prior = group_diff_prior, verbose = FALSE) +#' # group_diff_prior <- pairs(emmeans(unupdate(stan_model), ~group)) +#' # bayesfactor_parameters(group_diff, prior = group_diff_prior, verbose = FALSE) #' } #' #' # brms models #' # ----------- -#' if (require("brms")) { +#' if (require("brms") && require("logspline")) { #' contrasts(sleep$group) <- contr.equalprior_pairs # see vingette #' my_custom_priors <- #' set_prior("student_t(3, 0, 1)", class = "b") + diff --git a/R/equivalence_test.R b/R/equivalence_test.R index 5d6fbe048..6edf82853 100644 --- a/R/equivalence_test.R +++ b/R/equivalence_test.R @@ -118,13 +118,17 @@ equivalence_test.numeric <- function(x, range = "default", ci = 0.95, verbose = out <- as.data.frame(rope_data) if (all(ci < 1)) { - out$ROPE_Equivalence <- ifelse(out$ROPE_Percentage == 0, "Rejected", - ifelse(out$ROPE_Percentage == 1, "Accepted", "Undecided") + out$ROPE_Equivalence <- datawizard::recode_into( + out$ROPE_Percentage == 0 ~ "Rejected", + out$ROPE_Percentage == 1 ~ "Accepted", + default = "Undecided" ) } else { # Related to guidelines for full rope (https://easystats.github.io/bayestestR/articles/4_Guidelines.html) - out$ROPE_Equivalence <- ifelse(out$ROPE_Percentage < 0.025, "Rejected", - ifelse(out$ROPE_Percentage > 0.975, "Accepted", "Undecided") + out$ROPE_Equivalence <- datawizard::recode_into( + out$ROPE_Percentage < 0.025 ~ "Rejected", + out$ROPE_Percentage > 0.975 ~ "Accepted", + default = "Undecided" ) } diff --git a/R/format.R b/R/format.R index 092bfef95..40515535a 100644 --- a/R/format.R +++ b/R/format.R @@ -73,6 +73,11 @@ format.bayestestR_eti <- format.describe_posterior #' @export format.bayestestR_si <- format.describe_posterior +#' @export +format.equivalence_test <- format.describe_posterior + +#' @export +format.rope <- format.describe_posterior diff --git a/R/unupdate.R b/R/unupdate.R index 0e0adacf3..10ed3df7c 100644 --- a/R/unupdate.R +++ b/R/unupdate.R @@ -102,15 +102,17 @@ unupdate.brmsfit_multiple <- function(model, insight::format_alert("Sampling priors, please wait...") } - utils::capture.output({model_prior <- - try(suppressMessages(suppressWarnings( - stats::update( - model, - sample_prior = "only", - newdata = newdata, - refresh = 0 - ) - )), silent = TRUE)}) + utils::capture.output({ + model_prior <- + try(suppressMessages(suppressWarnings( + stats::update( + model, + sample_prior = "only", + newdata = newdata, + refresh = 0 + ) + )), silent = TRUE) + }) if (methods::is(model_prior, "try-error")) { if (grepl("proper priors", model_prior, fixed = TRUE)) { @@ -143,7 +145,9 @@ unupdate.blavaan <- function(model, verbose = TRUE, ...) { cl$prisamp <- TRUE suppressMessages(suppressWarnings( - utils::capture.output({model_prior <- eval(cl)}) + utils::capture.output({ + model_prior <- eval(cl) + }) )) model_prior diff --git a/man/bayesfactor_parameters.Rd b/man/bayesfactor_parameters.Rd index ebcd19810..eceef8064 100644 --- a/man/bayesfactor_parameters.Rd +++ b/man/bayesfactor_parameters.Rd @@ -282,13 +282,13 @@ if (require("rstanarm") && require("emmeans") && require("logspline")) { bayesfactor_parameters(group_diff, prior = stan_model, verbose = FALSE) # Or - group_diff_prior <- pairs(emmeans(unupdate(stan_model), ~group)) - bayesfactor_parameters(group_diff, prior = group_diff_prior, verbose = FALSE) + # group_diff_prior <- pairs(emmeans(unupdate(stan_model), ~group)) + # bayesfactor_parameters(group_diff, prior = group_diff_prior, verbose = FALSE) } # brms models # ----------- -if (require("brms")) { +if (require("brms") && require("logspline")) { contrasts(sleep$group) <- contr.equalprior_pairs # see vingette my_custom_priors <- set_prior("student_t(3, 0, 1)", class = "b") + diff --git a/tests/testthat/test-check_prior.R b/tests/testthat/test-check_prior.R index 3ec62f578..5087aab3e 100644 --- a/tests/testthat/test-check_prior.R +++ b/tests/testthat/test-check_prior.R @@ -1,6 +1,8 @@ +skip_on_os(os = "mac") + test_that("check_prior - stanreg", { skip_on_cran() - skip_on_os(os = "windows") + skip_on_os(os = c("windows", "mac")) skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("BH") @@ -10,12 +12,12 @@ test_that("check_prior - stanreg", { set.seed(333) model1 <- insight::download_model("stanreg_lm_1") - expect_equal( + expect_identical( check_prior(model1)$Prior_Quality, c("informative", "uninformative") ) - expect_equal( + expect_identical( check_prior(model1, method = "lakeland")$Prior_Quality, c("informative", "informative") ) @@ -43,7 +45,7 @@ test_that("check_prior - brms (linux)", { ) }) - expect_warning(expect_equal( + expect_warning(expect_identical( check_prior(model2)$Prior_Quality, c( "uninformative", "informative", "informative", "uninformative", @@ -51,7 +53,7 @@ test_that("check_prior - brms (linux)", { ) )) - expect_warning(expect_equal( + expect_warning(expect_identical( check_prior(model2, method = "lakeland")$Prior_Quality, c( "informative", "informative", "informative", "informative", @@ -88,7 +90,7 @@ test_that("check_prior - brms (linux)", { # )) - expect_warning(expect_equal( + expect_warning(expect_identical( check_prior(model2, method = "lakeland")$Prior_Quality, c( "informative", "informative", "informative", "informative", @@ -99,7 +101,7 @@ test_that("check_prior - brms (linux)", { test_that("check_prior - brms (not linux or windows)", { skip_on_cran() - skip_on_os(os = c("linux", "windows")) + skip_on_os(os = c("linux", "windows", "mac")) skip_if_offline() skip_if_not_or_load_if_installed("rstanarm") skip_if_not_or_load_if_installed("BH") @@ -117,7 +119,7 @@ test_that("check_prior - brms (not linux or windows)", { ) }) - expect_warning(expect_equal( + expect_warning(expect_identical( check_prior(model2)$Prior_Quality, c( "uninformative", "uninformative", "informative", "uninformative", @@ -125,7 +127,7 @@ test_that("check_prior - brms (not linux or windows)", { ) )) - expect_warning(expect_equal( + expect_warning(expect_identical( check_prior(model2, method = "lakeland")$Prior_Quality, c( "informative", "informative", "informative", "informative", diff --git a/tests/testthat/test-format.R b/tests/testthat/test-format.R new file mode 100644 index 000000000..c8e0377a5 --- /dev/null +++ b/tests/testthat/test-format.R @@ -0,0 +1,59 @@ +test_that("p_significance", { + set.seed(333) + x <- rnorm(100) + expect_equal( + format(point_estimate(x)), + data.frame(Median = "0.05", Mean = "-0.02", MAP = "0.13", stringsAsFactors = FALSE), + ignore_attr = TRUE + ) + expect_equal( + format(ci(x)), + data.frame(`95% CI` = "[-1.93, 1.77]", stringsAsFactors = FALSE), + ignore_attr = TRUE + ) + expect_equal( + format(p_rope(x)), + data.frame(ROPE = "[-0.10, 0.10]", `p (ROPE)` = "0.100", stringsAsFactors = FALSE), + ignore_attr = TRUE + ) + expect_equal( + format(map_estimate(x)), + data.frame(x = "0.13", stringsAsFactors = FALSE), + ignore_attr = TRUE + ) + expect_equal( + format(p_direction(x)), + data.frame(x = "0.51", stringsAsFactors = FALSE), + ignore_attr = TRUE + ) + expect_equal( + format(p_map(x)), + data.frame(x = "0.97", stringsAsFactors = FALSE), + ignore_attr = TRUE + ) + expect_equal( + format(p_significance(x)), + data.frame(x = "0.46", stringsAsFactors = FALSE), + ignore_attr = TRUE + ) + expect_equal( + format(rope(x)), + data.frame(CI = "0.95", ROPE = "[-0.10, 0.10]", `% in ROPE` = "10.64%", stringsAsFactors = FALSE), + ignore_attr = TRUE + ) + expect_equal( + format(equivalence_test(x)), + data.frame( + CI = "0.95", ROPE = "[-0.10, 0.10]", `% in ROPE` = "10.64%", + `Equivalence (ROPE)` = "Undecided", HDI_low = "-1.93", HDI_high = "1.77", + stringsAsFactors = FALSE + ), + ignore_attr = TRUE + ) + skip_if_not_installed("logspline") + expect_equal( + format(bayesfactor_parameters(x, verbose = FALSE)), + data.frame(BF = "1.00", stringsAsFactors = FALSE), + ignore_attr = TRUE + ) +}) diff --git a/vignettes/bayes_factors.Rmd b/vignettes/bayes_factors.Rmd index 6f6fe5286..7d8d9b77e 100644 --- a/vignettes/bayes_factors.Rmd +++ b/vignettes/bayes_factors.Rmd @@ -920,7 +920,7 @@ posterior distribution, and estimate the HDI. In `bayestestR`, we can do this with the `weighted_posteriors()` function: -```{r} +```{r eval=FALSE} BMA_draws <- weighted_posteriors(mod, mod_carb, verbose = FALSE) BMA_hdi <- hdi(BMA_draws, ci = 0.95) @@ -928,6 +928,14 @@ BMA_hdi plot(BMA_hdi) ``` +```{r echo=FALSE} +BMA_draws <- weighted_posteriors(mod, mod_carb, verbose = FALSE) + +BMA_hdi <- hdi(BMA_draws, ci = 0.95) +BMA_hdi + +plot(BMA_hdi, data = BMA_draws) +``` We can see that across both models under consideration, the posterior of the `carb` effect is almost equally weighted between the alternative model and the