From 249a462dce45dc4ea2bc788545bf06111b247c4b Mon Sep 17 00:00:00 2001 From: Daniel Date: Fri, 4 Oct 2024 10:53:46 +0200 Subject: [PATCH] fix tests --- DESCRIPTION | 2 + tests/testthat/test-marginaleffects.R | 136 +++++++++++++------------- 2 files changed, 72 insertions(+), 66 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 519ca2026..b2fc01772 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -81,6 +81,7 @@ Suggests: blavaan, bridgesampling, brms, + collapse, curl, effectsize, emmeans, @@ -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 diff --git a/tests/testthat/test-marginaleffects.R b/tests/testthat/test-marginaleffects.R index 9b9260ded..a49c16f4d 100644 --- a/tests/testthat/test-marginaleffects.R +++ b/tests/testthat/test-marginaleffects.R @@ -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 + ) + }) +)