Skip to content

Commit

Permalink
fix #688
Browse files Browse the repository at this point in the history
  • Loading branch information
mattansb authored Dec 20, 2024
2 parents c1e9392 + a6050e3 commit 51fed17
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 20 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: bayestestR
Title: Understand and Describe Bayesian Models and Posterior Distributions
Version: 0.15.0.3
Version: 0.15.0.4
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down Expand Up @@ -95,7 +95,7 @@ Suggests:
lavaan,
lme4,
logspline (>= 2.1.21),
marginaleffects (>= 0.24.0),
marginaleffects (>= 0.24.0.6),
MASS,
mclust,
mediation,
Expand Down Expand Up @@ -128,4 +128,4 @@ Config/testthat/parallel: true
Config/rcmdcheck/ignore-inconsequential-notes: true
Config/Needs/website: easystats/easystatstemplate
Config/Needs/check: stan-dev/cmdstanr
Remotes: easystats/datawizard
Remotes: easystats/datawizard, vincentarelbundock/marginaleffects
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# bayestestR 0.15.0.4

## Bug fixes

* Fix to `emmeans` / `marginaleffects` / `data.frame(<rvar>)` methods when using multiple credible levels (#688).


# bayestestR 0.15.0

## Changes
Expand Down
4 changes: 2 additions & 2 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
datagrid <- insight::get_datagrid(object)
grid_names <- colnames(datagrid)

if (long) {
if (long || nrow(datagrid) < nrow(results)) {
datagrid$Parameter <- unique(results$Parameter)
results <- datawizard::data_merge(datagrid, results, by = "Parameter")
results$Parameter <- NULL
Expand Down Expand Up @@ -270,7 +270,7 @@
grid_names <- colnames(object)[!is_rvar]
datagrid <- data.frame(object[, grid_names, drop = FALSE])

if (long) {
if (long || nrow(datagrid) < nrow(results)) {
datagrid$Parameter <- unique(results$Parameter)
results <- datawizard::data_merge(datagrid, results, by = "Parameter")
results$Parameter <- NULL
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-data.frame-with-rvar.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ test_that("data.frame w/ rvar_col descrive_posterior etc", {
res <- eti(dfx, rvar_col = "my_rvar", ci = c(0.8, 0.95))
res.ref <- eti(dfx$my_rvar, ci = c(0.8, 0.95))
expect_true(all(c("mu", "sigma") %in% colnames(res)))
expect_identical(
as.data.frame(res[c("mu", "sigma")]),
data.frame(
mu = c(0, 0, 0.5, 0.5, 1, 1),
sigma = c(1, 1, 0.5, 0.5, 0.25, 0.25)
)
)
expect_identical(nrow(format(res)), 3L)
expect_identical(ncol(format(res)), 4L)
expect_equal(res[setdiff(colnames(res), c("mu", "sigma"))],
Expand Down
31 changes: 20 additions & 11 deletions tests/testthat/test-emmGrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,16 @@ test_that("emmGrid hdi", {
expect_equal(xhdi$CI_high, all_summ$upper.HPD, tolerance = 0.1)

xhdi2 <- hdi(emc_, ci = 0.95)
expect_equal(xhdi$CI_low, xhdi2$CI_low)
expect_identical(xhdi$CI_low, xhdi2$CI_low)

xhdi3 <- hdi(all_, ci = c(0.9, 0.95))
expect_identical(
as.data.frame(xhdi3[1:2]),
data.frame(
group = c("1", "1", "2", "2", ".", "."),
contrast = c(".", ".", ".", ".", "group1 - group2", "group1 - group2"), stringsAsFactors = FALSE
)
)
})

test_that("emmGrid point_estimate", {
Expand All @@ -39,7 +48,7 @@ test_that("emmGrid point_estimate", {
expect_equal(xpest$Median, all_summ$emmean, tolerance = 0.1)

xpest2 <- point_estimate(emc_, centrality = "all", dispersion = TRUE)
expect_equal(xpest$Median, xpest2$Median)
expect_identical(xpest$Median, xpest2$Median)
})


Expand Down Expand Up @@ -114,15 +123,15 @@ test_that("emmGrid rope", {
# describe_posterior ------------------------------------------------------

test_that("emmGrid describe_posterior", {
expect_equal(
expect_identical(
describe_posterior(all_)$median,
describe_posterior(emc_)$median
)

expect_identical(colnames(describe_posterior(all_))[1:2], c("group", "contrast"))

skip_on_cran()
expect_equal(
expect_identical(
describe_posterior(all_, bf_prior = model_p, test = "bf")$log_BF,
describe_posterior(emc_, bf_prior = model_p, test = "bf")$log_BF
)
Expand Down Expand Up @@ -167,9 +176,9 @@ test_that("emmGrid bayesfactor_restricted", {
hyps <- c("`1` < `2`", "`1` < 0")

xrbf <- bayesfactor_restricted(em_, prior = model_p, hypothesis = hyps)
expect_equal(length(xrbf$log_BF), 2)
expect_equal(length(xrbf$p_prior), 2)
expect_equal(length(xrbf$p_posterior), 2)
expect_length(xrbf$log_BF, 2)
expect_length(xrbf$p_prior, 2)
expect_length(xrbf$p_posterior, 2)
expect_warning(bayesfactor_restricted(em_, hypothesis = hyps))

xrbf2 <- bayesfactor_restricted(emc_, prior = model_p, hypothesis = hyps)
Expand All @@ -182,12 +191,12 @@ test_that("emmGrid si", {

xrsi <- si(all_, prior = model_p, verbose = FALSE)
expect_identical(colnames(xrsi)[1:2], c("group", "contrast"))
expect_equal(length(xrsi$CI_low), 3)
expect_equal(length(xrsi$CI_high), 3)
expect_length(xrsi$CI_low, 3)
expect_length(xrsi$CI_high, 3)

xrsi2 <- si(emc_, prior = model_p, verbose = FALSE)
expect_equal(xrsi$CI_low, xrsi2$CI_low)
expect_equal(xrsi$CI_high, xrsi2$CI_high)
expect_identical(xrsi$CI_low, xrsi2$CI_low)
expect_identical(xrsi$CI_high, xrsi2$CI_high)
})


Expand Down
31 changes: 27 additions & 4 deletions tests/testthat/test-marginaleffects.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,34 @@ withr::with_environment(
)

expect_true(all(c("term", "contrast") %in% colnames(results)))
expect_equal(results[setdiff(colnames(results), c("term", "contrast"))],
expect_equal(results[setdiff(colnames(results), c("term", "contrast", "am"))],
results_draws[setdiff(colnames(results_draws), "Parameter")],
ignore_attr = TRUE
)

# multi ci levels
res <- hdi(mfx, ci = c(0.8, 0.9))
expect_identical(
as.data.frame(res[1:3]),
data.frame(
term = c(
"am", "am", "am", "am", "cyl", "cyl",
"cyl", "cyl", "cyl", "cyl", "cyl", "cyl",
"hp", "hp", "hp", "hp"
),
contrast = c(
"1 - 0", "1 - 0", "1 - 0", "1 - 0",
"6 - 4", "6 - 4", "8 - 4", "8 - 4",
"6 - 4", "6 - 4", "8 - 4", "8 - 4",
"dY/dX", "dY/dX", "dY/dX", "dY/dX"
),
am = c(
0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0,
1, 1
), stringsAsFactors = FALSE
)
)

# estimate_density
mfx <- marginaleffects::comparisons(mod,
variables = "cyl",
Expand Down Expand Up @@ -67,16 +90,16 @@ withr::with_environment(
outsi <- si(mfx, prior = mfxp, verbose = FALSE)
outsiref <- si(mfx_samps, prior = mfxp_samps, verbose = FALSE)

expect_true(all(c("term", "contrast") %in% colnames(outsi)))
expect_equal(outsi[setdiff(colnames(outsi), c("term", "contrast"))],
expect_true(all(c("term", "contrast", "am") %in% colnames(outsi)))
expect_equal(outsi[setdiff(colnames(outsi), c("term", "contrast", "am"))],
outsiref[setdiff(colnames(outsiref), "Parameter")],
ignore_attr = TRUE
)

# bayesfactor_parameters
bfp <- bayesfactor_parameters(mfx, prior = mfxp, verbose = FALSE)
bfpref <- bayesfactor_parameters(mfx_samps, prior = mfxp_samps, verbose = FALSE)
expect_equal(bfp[setdiff(colnames(bfp), c("term", "contrast"))],
expect_equal(bfp[setdiff(colnames(bfp), c("term", "contrast", "am"))],
bfpref[setdiff(colnames(bfpref), "Parameter")],
ignore_attr = TRUE
)
Expand Down

0 comments on commit 51fed17

Please sign in to comment.