Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 4, 2024
1 parent 9d914d8 commit 249a462
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 66 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Suggests:
blavaan,
bridgesampling,
brms,
collapse,
curl,
effectsize,
emmeans,
Expand Down Expand Up @@ -127,3 +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
136 changes: 70 additions & 66 deletions tests/testthat/test-marginaleffects.R
Original file line number Diff line number Diff line change
@@ -1,80 +1,84 @@
test_that("marginaleffects descrive_posterior", {
# skip_on_ci()
skip_on_cran()
skip_on_cran()
skip_if_not_installed("withr")
skip_if_not_installed("rstanarm")
skip_if_not_installed("marginaleffects")
skip_if_not_installed("collapse")

skip_if_not_installed("rstanarm")
skip_if_not_installed("marginaleffects")
withr::with_environment(
new.env(),
test_that("marginaleffects descrive_posterior", {
# skip_on_ci()

data("mtcars")
mtcars$cyl <- factor(mtcars$cyl)
mod <- rstanarm::stan_glm(mpg ~ cyl + hp * am, data = mtcars, refresh = 0)
data("mtcars")
mtcars$cyl <- factor(mtcars$cyl)
mod <- rstanarm::stan_glm(mpg ~ cyl + hp * am, data = mtcars, refresh = 0)

mfx <- marginaleffects::avg_slopes(mod, by = "am")
mfx_samps <- as.data.frame(t(attr(mfx, "posterior_draws")))
mfx <- marginaleffects::avg_slopes(mod, by = "am")
mfx_samps <- as.data.frame(t(attr(mfx, "posterior_draws")))

results <- describe_posterior(mfx,
centrality = "MAP", ci_method = "hdi",
test = c("pd", "rope", "p_map", "equivalence_test")
)
results_draws <- describe_posterior(mfx_samps,
centrality = "MAP", ci_method = "hdi",
test = c("pd", "rope", "p_map", "equivalence_test")
)
results <- describe_posterior(mfx,
centrality = "MAP", ci_method = "hdi",
test = c("pd", "rope", "p_map", "equivalence_test")
)
results_draws <- describe_posterior(mfx_samps,
centrality = "MAP", ci_method = "hdi",
test = c("pd", "rope", "p_map", "equivalence_test")
)

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

# estimate_density
mfx <- marginaleffects::comparisons(mod,
variables = "cyl",
newdata = marginaleffects::datagrid(hp = 100, am = 0)
)
samps <- insight::get_parameters(mod)[c("cyl6", "cyl8")]
# estimate_density
mfx <- marginaleffects::comparisons(mod,
variables = "cyl",
newdata = marginaleffects::datagrid(hp = 100, am = 0)
)
samps <- insight::get_parameters(mod)[c("cyl6", "cyl8")]

res <- estimate_density(mfx)
resref <- estimate_density(samps)
expect_equal(res[intersect(colnames(res), colnames(resref))],
resref[intersect(colnames(res), colnames(resref))],
ignore_attr = TRUE
)
})
res <- estimate_density(mfx)
resref <- estimate_density(samps)
expect_equal(res[intersect(colnames(res), colnames(resref))],
resref[intersect(colnames(res), colnames(resref))],
ignore_attr = TRUE
)
})
)

test_that("marginaleffects bayesfactors", {
# skip_on_ci()
skip_on_cran()
withr::with_environment(
new.env(),
test_that("marginaleffects bayesfactors", {
# skip_on_ci()

skip_if_not_installed("rstanarm")
skip_if_not_installed("marginaleffects")
data("mtcars")
mtcars$cyl <- factor(mtcars$cyl)
mod <- rstanarm::stan_glm(mpg ~ cyl + hp * am, data = mtcars, refresh = 0)
modp <- unupdate(mod, verbose = FALSE)

data("mtcars")
mtcars$cyl <- factor(mtcars$cyl)
mod <- rstanarm::stan_glm(mpg ~ cyl + hp * am, data = mtcars, refresh = 0)
modp <- unupdate(mod, verbose = FALSE)
mfx <- marginaleffects::avg_slopes(mod, by = "am")
mfxp <- marginaleffects::avg_slopes(modp, by = "am")

mfx <- marginaleffects::avg_slopes(mod, by = "am")
mfxp <- marginaleffects::avg_slopes(modp, by = "am")
mfx_samps <- as.data.frame(t(attr(mfx, "posterior_draws")))
mfxp_samps <- as.data.frame(t(attr(mfxp, "posterior_draws")))

mfx_samps <- as.data.frame(t(attr(mfx, "posterior_draws")))
mfxp_samps <- as.data.frame(t(attr(mfxp, "posterior_draws")))
# SI
outsi <- si(mfx, prior = mfxp, verbose = FALSE)
outsiref <- si(mfx_samps, prior = mfxp_samps, verbose = FALSE)

# SI
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"))],
outsiref[setdiff(colnames(outsiref), "Parameter")],
ignore_attr = TRUE
)

expect_true(all(c("term", "contrast") %in% colnames(outsi)))
expect_equal(outsi[setdiff(colnames(outsi), c("term", "contrast"))],
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"))],
bfpref[setdiff(colnames(bfpref), "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"))],
bfpref[setdiff(colnames(bfpref), "Parameter")],
ignore_attr = TRUE
)
})
)

0 comments on commit 249a462

Please sign in to comment.