From d58410a9f760c26147dda9111d413c72295b913e Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 24 Nov 2025 14:13:22 +0100 Subject: [PATCH 1/5] Prepare CRAN release --- DESCRIPTION | 3 +-- NEWS.md | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 935e1eed1..0372b1002 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: parameters Title: Processing of Model Parameters -Version: 0.28.2.10 +Version: 0.28.3 Authors@R: c(person(given = "Daniel", family = "Lüdecke", @@ -232,4 +232,3 @@ Config/testthat/parallel: true Config/Needs/website: easystats/easystatstemplate Config/Needs/check: stan-dev/cmdstanr Config/rcmdcheck/ignore-inconsequential-notes: true -Remotes: easystats/insight, easystats/modelbased diff --git a/NEWS.md b/NEWS.md index f8b7b5fc8..ddb2b8b38 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ -# parameters (devel) +# parameters 0.28.3 -* fixed bug in `standardize_info()` that was preventing +* fixed bug in `standardize_info()` that was preventing `standardise_parameters()` from working for `fixest` models. * `equivalence_test()` gets methods for objects from the *modelbased* package. From c0ef635919ca841aa3aa301efd1bac7649655a20 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 24 Nov 2025 15:57:54 +0100 Subject: [PATCH 2/5] fix --- tests/testthat/test-model_parameters_df_method.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testthat/test-model_parameters_df_method.R b/tests/testthat/test-model_parameters_df_method.R index a3eb2d36a..f89bdd3b6 100644 --- a/tests/testthat/test-model_parameters_df_method.R +++ b/tests/testthat/test-model_parameters_df_method.R @@ -1,7 +1,7 @@ skip_if_not_installed("lmerTest") skip_if_not_installed("pbkrtest") skip_if_not_installed("lme4") -skip_if_not_installed("glmmTMB") +skip_if_not_installed("glmmTMB", minimum_version = "1.1.13") mtcars$cyl <- as.factor(mtcars$cyl) model <- suppressMessages(lme4::lmer( From a403e5b02a0526ecd207fc41631cb230d69f2c63 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 24 Nov 2025 15:59:05 +0100 Subject: [PATCH 3/5] fix test --- tests/testthat/test-model_parameters_df_method.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test-model_parameters_df_method.R b/tests/testthat/test-model_parameters_df_method.R index f89bdd3b6..459fff46c 100644 --- a/tests/testthat/test-model_parameters_df_method.R +++ b/tests/testthat/test-model_parameters_df_method.R @@ -2,6 +2,7 @@ skip_if_not_installed("lmerTest") skip_if_not_installed("pbkrtest") skip_if_not_installed("lme4") skip_if_not_installed("glmmTMB", minimum_version = "1.1.13") +skip_if_not_installed("insight", minimum_version = "1.4.3") mtcars$cyl <- as.factor(mtcars$cyl) model <- suppressMessages(lme4::lmer( From 2c61da15f9cf43b23681f6f395fe89f4df275c38 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 25 Nov 2025 08:04:06 +0100 Subject: [PATCH 4/5] skip_on_cran first --- tests/testthat/test-glmmTMB.R | 1897 ++++++++++------- tests/testthat/test-model_parameters.bracl.R | 39 +- .../test-model_parameters.fixest_multi.R | 8 +- .../testthat/test-model_parameters.mclogit.R | 61 +- .../testthat/test-model_parameters.mediate.R | 91 +- .../test-model_parameters_std_mixed.R | 89 +- tests/testthat/test-polr.R | 9 +- tests/testthat/test-posterior.R | 39 +- tests/testthat/test-printing-stan.R | 114 +- .../testthat/test-random_effects_ci-glmmTMB.R | 320 ++- tests/testthat/test-random_effects_ci.R | 568 ++++- tests/testthat/test-robust.R | 34 +- tests/testthat/test-svyolr.R | 3 +- 13 files changed, 2086 insertions(+), 1186 deletions(-) diff --git a/tests/testthat/test-glmmTMB.R b/tests/testthat/test-glmmTMB.R index 9906feebd..1c7711879 100644 --- a/tests/testthat/test-glmmTMB.R +++ b/tests/testthat/test-glmmTMB.R @@ -1,3 +1,5 @@ +skip_on_cran() + skip_if_not_installed("withr") skip_if_not_installed("glmmTMB") skip_if_not(getRversion() >= "4.0.0") @@ -5,777 +7,1128 @@ skip_if_not(getRversion() >= "4.0.0") data("fish", package = "parameters") data("Salamanders", package = "glmmTMB") -skip_on_cran() - -withr::with_options( - list(parameters_exponentiate = FALSE), - { - m1 <- suppressWarnings(glmmTMB::glmmTMB( - count ~ child + camper + (1 | persons), - ziformula = ~ child + camper + (1 | persons), - data = fish, - family = glmmTMB::truncated_poisson() - )) - - m2 <- suppressWarnings(glmmTMB::glmmTMB( - count ~ child + camper + (1 | persons), - data = fish, - family = poisson() - )) - - m3 <- suppressWarnings(glmmTMB::glmmTMB( - count ~ spp + mined + (1 | site), - ziformula = ~ spp + mined, - family = glmmTMB::nbinom2, - data = Salamanders - )) - - test_that("ci", { - expect_equal( - ci(m1)$CI_low, - c(0.33067, -1.32402, 0.55037, -1.66786, 1.44667, -1.64177), - tolerance = 1e-3 - ) - expect_equal( - ci(m1, component = "cond")$CI_low, - c(0.33067, -1.32402, 0.55037), - tolerance = 1e-3 - ) - expect_equal( - ci(m1, component = "zi")$CI_low, - c(-1.66786, 1.44667, -1.64177), - tolerance = 1e-3 - ) - - expect_equal( - ci(m2)$CI_low, - c(-0.47982, -1.85096, 0.76044), - tolerance = 1e-3 - ) - expect_equal( - ci(m2, component = "cond")$CI_low, - c(-0.47982, -1.85096, 0.76044), - tolerance = 1e-3 - ) - - expect_message( - expect_null(ci(m2, component = "zi")), - "no zero-inflation component" - ) - }) - - - test_that("se", { - expect_equal( - standard_error(m1)$SE, - c(0.47559, 0.09305, 0.09346, 0.65229, 0.3099, 0.32324), - tolerance = 1e-3 - ) - expect_equal( - standard_error(m1, effects = "random")$persons$`(Intercept)`, - c(0.69856, 0.68935, 0.68749, 0.68596), - tolerance = 1e-3 - ) - expect_equal( - standard_error(m1, component = "cond")$SE, - c(0.47559, 0.09305, 0.09346), - tolerance = 1e-3 - ) - expect_equal( - standard_error(m1, component = "zi")$SE, - c(0.65229, 0.3099, 0.32324), - tolerance = 1e-3 - ) - - expect_equal( - standard_error(m2)$SE, - c(0.62127, 0.08128, 0.08915), - tolerance = 1e-3 - ) - expect_equal( - standard_error(m2, component = "cond")$SE, - c(0.62127, 0.08128, 0.08915), - tolerance = 1e-3 - ) - - expect_message( - expect_null(standard_error(m2, component = "zi")), - "no zero-inflation component" - ) - }) - - - test_that("p_value", { - expect_equal( - p_value(m1)$p, - c(0.00792, 0, 0, 0.55054, 0, 0.00181), - tolerance = 1e-3 - ) - expect_equal( - p_value(m1, component = "cond")$p, - c(0.00792, 0, 0), - tolerance = 1e-3 - ) - expect_equal( - p_value(m1, component = "zi")$p, - c(0.55054, 0, 0.00181), - tolerance = 1e-3 - ) - - expect_equal( - p_value(m2)$p, - c(0.23497, 0, 0), - tolerance = 1e-3 - ) - expect_equal( - p_value(m2, component = "cond")$p, - c(0.23497, 0, 0), - tolerance = 1e-3 - ) - - expect_message( - expect_null(p_value(m2, component = "zi")), - "no zero-inflation component" - ) - }) - - - test_that("model_parameters", { - expect_equal( - model_parameters(m1, effects = "fixed")$Coefficient, - c(1.2628, -1.14165, 0.73354, -0.38939, 2.05407, -1.00823), - tolerance = 1e-3 - ) - expect_equal( - model_parameters(m1, effects = "all")$Coefficient, - c(1.2628, -1.14165, 0.73354, -0.38939, 2.05407, -1.00823, 0.9312, 1.17399), - tolerance = 1e-3 - ) - expect_equal( - model_parameters(m2, effects = "fixed")$Coefficient, - c(0.73785, -1.69166, 0.93516), - tolerance = 1e-3 - ) - expect_equal( - model_parameters(m3, effects = "fixed")$Coefficient, - c( - -0.61038, -0.9637, 0.17068, -0.38706, 0.48795, 0.58949, -0.11327, - 1.42935, 0.91004, 1.16141, -0.93932, 1.04243, -0.56231, -0.893, - -2.53981, -2.56303, 1.51165 - ), - tolerance = 1e-2 - ) - expect_identical( - model_parameters(m1)$Component, - c( - "conditional", "conditional", "conditional", "zero_inflated", - "zero_inflated", "zero_inflated", "conditional", "zero_inflated" - ) - ) - expect_null(model_parameters(m2, effects = "fixed")$Component) - expect_identical( - model_parameters(m2)$Component, - c("conditional", "conditional", "conditional", "conditional") - ) - expect_identical( - model_parameters(m3, effects = "fixed")$Component, - c( - "conditional", "conditional", "conditional", "conditional", - "conditional", "conditional", "conditional", "conditional", "zero_inflated", - "zero_inflated", "zero_inflated", "zero_inflated", "zero_inflated", - "zero_inflated", "zero_inflated", "zero_inflated", "dispersion" - ) - ) - expect_equal( - model_parameters(m3, effects = "fixed")$SE, - c( - 0.4052, 0.6436, 0.2353, 0.3424, 0.2383, 0.2278, 0.2439, 0.3666, - 0.6279, 1.3346, 0.8005, 0.714, 0.7263, 0.7535, 2.1817, 0.6045, - NA - ), - tolerance = 1e-2 - ) - }) - - test_that("model_parameters.mixed-random", { - params <- model_parameters(m1, effects = "random", group_level = TRUE) - expect_identical(c(nrow(params), ncol(params)), c(8L, 10L)) - expect_named( - params, - c( - "Parameter", "Level", "Coefficient", "SE", "CI", "CI_low", - "CI_high", "Component", "Effects", "Group" - ) - ) - expect_identical( - as.vector(params$Parameter), - c( - "(Intercept)", "(Intercept)", "(Intercept)", "(Intercept)", - "(Intercept)", "(Intercept)", "(Intercept)", "(Intercept)" - ) - ) - expect_identical( - params$Component, - c( - "conditional", "conditional", "conditional", "conditional", - "zero_inflated", "zero_inflated", "zero_inflated", "zero_inflated" - ) - ) - expect_equal( - params$Coefficient, - c(-1.24, -0.3456, 0.3617, 1.2553, 1.5719, 0.3013, -0.3176, -1.5665), - tolerance = 1e-2 - ) - }) - - test_that("model_parameters.mixed-random grouplevel", { - params <- model_parameters(m1, effects = "grouplevel") - expect_identical(c(nrow(params), ncol(params)), c(8L, 10L)) - expect_named( - params, - c( - "Parameter", "Level", "Coefficient", "SE", "CI", "CI_low", - "CI_high", "Component", "Effects", "Group" - ) - ) - expect_identical( - as.vector(params$Parameter), - c( - "(Intercept)", "(Intercept)", "(Intercept)", "(Intercept)", - "(Intercept)", "(Intercept)", "(Intercept)", "(Intercept)" - ) - ) - expect_identical( - params$Component, - c( - "conditional", "conditional", "conditional", "conditional", - "zero_inflated", "zero_inflated", "zero_inflated", "zero_inflated" - ) - ) - expect_equal( - params$Coefficient, - c(-1.24, -0.3456, 0.3617, 1.2553, 1.5719, 0.3013, -0.3176, -1.5665), - tolerance = 1e-2 - ) - }) - - test_that("model_parameters.mixed-random total", { - params <- model_parameters(m1, effects = "total") - expect_identical(c(nrow(params), ncol(params)), c(8L, 6L)) - expect_named( - params, - c( - "Group", "Level", "Parameter", "Coefficient", "Component", - "Effects" - ) - ) - expect_identical( - as.vector(params$Parameter), - c( - "(Intercept)", "(Intercept)", "(Intercept)", "(Intercept)", - "(Intercept)", "(Intercept)", "(Intercept)", "(Intercept)" - ) - ) - expect_identical( - params$Component, - c( - "conditional", "conditional", "conditional", "conditional", - "zero_inflated", "zero_inflated", "zero_inflated", "zero_inflated" - ) - ) - expect_equal( - params$Coefficient, - c(0.02278, 0.91717, 1.62455, 2.51811, 1.18248, -0.08808, -0.70703, -1.95584), - tolerance = 1e-2 - ) - }) - - test_that("model_parameters.mixed-ran_pars", { - params <- model_parameters(m1, effects = "random") - expect_identical(c(nrow(params), ncol(params)), c(2L, 9L)) - expect_named( - params, - c("Parameter", "Coefficient", "SE", "CI", "CI_low", "CI_high", "Effects", "Group", "Component") - ) - expect_identical( - params$Parameter, - c("SD (Intercept)", "SD (Intercept)") - ) - expect_identical( - params$Component, - c("conditional", "zero_inflated") - ) - expect_equal( - params$Coefficient, - c(0.9312, 1.17399), - tolerance = 1e-2 - ) - }) - - test_that("model_parameters.mixed-all_pars", { - params <- model_parameters(m1, effects = "all") - expect_identical(c(nrow(params), ncol(params)), c(8L, 12L)) - expect_named( - params, - c( - "Parameter", "Coefficient", "SE", "CI", "CI_low", - "CI_high", "z", "df_error", "p", "Effects", "Group", "Component" - ) - ) - expect_identical( - params$Parameter, - c( - "(Intercept)", "child", "camper1", "(Intercept)", "child", - "camper1", "SD (Intercept)", "SD (Intercept)" - ) - ) - expect_identical( - params$Component, - c( - "conditional", "conditional", "conditional", "zero_inflated", - "zero_inflated", "zero_inflated", "conditional", "zero_inflated" - ) - ) - expect_equal( - params$Coefficient, - c(1.2628, -1.14165, 0.73354, -0.38939, 2.05407, -1.00823, 0.9312, 1.17399), - tolerance = 1e-2 - ) - }) - - test_that("model_parameters.mixed-all", { - params <- model_parameters(m1, effects = "all", group_level = TRUE) - expect_identical(c(nrow(params), ncol(params)), c(14L, 13L)) - expect_named( - params, - c( - "Parameter", "Level", "Coefficient", "SE", "CI", "CI_low", - "CI_high", "z", "df_error", "p", "Component", "Effects", - "Group" - ) - ) - expect_identical( - params$Parameter, - c( - "(Intercept)", "child", "camper1", "(Intercept)", "child", - "camper1", "(Intercept)", "(Intercept)", "(Intercept)", "(Intercept)", - "(Intercept)", "(Intercept)", "(Intercept)", "(Intercept)" - ) - ) - expect_identical( - params$Component, - c( - "conditional", "conditional", "conditional", "zero_inflated", - "zero_inflated", "zero_inflated", "conditional", "conditional", - "conditional", "conditional", "zero_inflated", "zero_inflated", - "zero_inflated", "zero_inflated" +withr::with_options(list(parameters_exponentiate = FALSE), { + m1 <- suppressWarnings(glmmTMB::glmmTMB( + count ~ child + camper + (1 | persons), + ziformula = ~ child + camper + (1 | persons), + data = fish, + family = glmmTMB::truncated_poisson() + )) + + m2 <- suppressWarnings(glmmTMB::glmmTMB( + count ~ child + camper + (1 | persons), + data = fish, + family = poisson() + )) + + m3 <- suppressWarnings(glmmTMB::glmmTMB( + count ~ spp + mined + (1 | site), + ziformula = ~ spp + mined, + family = glmmTMB::nbinom2, + data = Salamanders + )) + + test_that("ci", { + expect_equal( + ci(m1)$CI_low, + c(0.33067, -1.32402, 0.55037, -1.66786, 1.44667, -1.64177), + tolerance = 1e-3 + ) + expect_equal( + ci(m1, component = "cond")$CI_low, + c(0.33067, -1.32402, 0.55037), + tolerance = 1e-3 + ) + expect_equal( + ci(m1, component = "zi")$CI_low, + c(-1.66786, 1.44667, -1.64177), + tolerance = 1e-3 + ) + + expect_equal(ci(m2)$CI_low, c(-0.47982, -1.85096, 0.76044), tolerance = 1e-3) + expect_equal( + ci(m2, component = "cond")$CI_low, + c(-0.47982, -1.85096, 0.76044), + tolerance = 1e-3 + ) + + expect_message(expect_null(ci(m2, component = "zi")), "no zero-inflation component") + }) + + test_that("se", { + expect_equal( + standard_error(m1)$SE, + c(0.47559, 0.09305, 0.09346, 0.65229, 0.3099, 0.32324), + tolerance = 1e-3 + ) + expect_equal( + standard_error(m1, effects = "random")$persons$`(Intercept)`, + c(0.69856, 0.68935, 0.68749, 0.68596), + tolerance = 1e-3 + ) + expect_equal( + standard_error(m1, component = "cond")$SE, + c(0.47559, 0.09305, 0.09346), + tolerance = 1e-3 + ) + expect_equal( + standard_error(m1, component = "zi")$SE, + c(0.65229, 0.3099, 0.32324), + tolerance = 1e-3 + ) + + expect_equal(standard_error(m2)$SE, c(0.62127, 0.08128, 0.08915), tolerance = 1e-3) + expect_equal( + standard_error(m2, component = "cond")$SE, + c(0.62127, 0.08128, 0.08915), + tolerance = 1e-3 + ) + + expect_message( + expect_null(standard_error(m2, component = "zi")), + "no zero-inflation component" + ) + }) + + test_that("p_value", { + expect_equal(p_value(m1)$p, c(0.00792, 0, 0, 0.55054, 0, 0.00181), tolerance = 1e-3) + expect_equal(p_value(m1, component = "cond")$p, c(0.00792, 0, 0), tolerance = 1e-3) + expect_equal( + p_value(m1, component = "zi")$p, + c(0.55054, 0, 0.00181), + tolerance = 1e-3 + ) + + expect_equal(p_value(m2)$p, c(0.23497, 0, 0), tolerance = 1e-3) + expect_equal(p_value(m2, component = "cond")$p, c(0.23497, 0, 0), tolerance = 1e-3) + + expect_message( + expect_null(p_value(m2, component = "zi")), + "no zero-inflation component" + ) + }) + + test_that("model_parameters", { + expect_equal( + model_parameters(m1, effects = "fixed")$Coefficient, + c(1.2628, -1.14165, 0.73354, -0.38939, 2.05407, -1.00823), + tolerance = 1e-3 + ) + expect_equal( + model_parameters(m1, effects = "all")$Coefficient, + c(1.2628, -1.14165, 0.73354, -0.38939, 2.05407, -1.00823, 0.9312, 1.17399), + tolerance = 1e-3 + ) + expect_equal( + model_parameters(m2, effects = "fixed")$Coefficient, + c(0.73785, -1.69166, 0.93516), + tolerance = 1e-3 + ) + expect_equal( + model_parameters(m3, effects = "fixed")$Coefficient, + c( + -0.61038, + -0.9637, + 0.17068, + -0.38706, + 0.48795, + 0.58949, + -0.11327, + 1.42935, + 0.91004, + 1.16141, + -0.93932, + 1.04243, + -0.56231, + -0.893, + -2.53981, + -2.56303, + 1.51165 + ), + tolerance = 1e-2 + ) + expect_identical( + model_parameters(m1)$Component, + c( + "conditional", + "conditional", + "conditional", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "conditional", + "zero_inflated" + ) + ) + expect_null(model_parameters(m2, effects = "fixed")$Component) + expect_identical( + model_parameters(m2)$Component, + c("conditional", "conditional", "conditional", "conditional") + ) + expect_identical( + model_parameters(m3, effects = "fixed")$Component, + c( + "conditional", + "conditional", + "conditional", + "conditional", + "conditional", + "conditional", + "conditional", + "conditional", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "dispersion" + ) + ) + expect_equal( + model_parameters(m3, effects = "fixed")$SE, + c( + 0.4052, + 0.6436, + 0.2353, + 0.3424, + 0.2383, + 0.2278, + 0.2439, + 0.3666, + 0.6279, + 1.3346, + 0.8005, + 0.714, + 0.7263, + 0.7535, + 2.1817, + 0.6045, + NA + ), + tolerance = 1e-2 + ) + }) + + test_that("model_parameters.mixed-random", { + params <- model_parameters(m1, effects = "random", group_level = TRUE) + expect_identical(c(nrow(params), ncol(params)), c(8L, 10L)) + expect_named( + params, + c( + "Parameter", + "Level", + "Coefficient", + "SE", + "CI", + "CI_low", + "CI_high", + "Component", + "Effects", + "Group" + ) + ) + expect_identical( + as.vector(params$Parameter), + c( + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)" + ) + ) + expect_identical( + params$Component, + c( + "conditional", + "conditional", + "conditional", + "conditional", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "zero_inflated" + ) + ) + expect_equal( + params$Coefficient, + c(-1.24, -0.3456, 0.3617, 1.2553, 1.5719, 0.3013, -0.3176, -1.5665), + tolerance = 1e-2 + ) + }) + + test_that("model_parameters.mixed-random grouplevel", { + params <- model_parameters(m1, effects = "grouplevel") + expect_identical(c(nrow(params), ncol(params)), c(8L, 10L)) + expect_named( + params, + c( + "Parameter", + "Level", + "Coefficient", + "SE", + "CI", + "CI_low", + "CI_high", + "Component", + "Effects", + "Group" + ) + ) + expect_identical( + as.vector(params$Parameter), + c( + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)" + ) + ) + expect_identical( + params$Component, + c( + "conditional", + "conditional", + "conditional", + "conditional", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "zero_inflated" + ) + ) + expect_equal( + params$Coefficient, + c(-1.24, -0.3456, 0.3617, 1.2553, 1.5719, 0.3013, -0.3176, -1.5665), + tolerance = 1e-2 + ) + }) + + test_that("model_parameters.mixed-random total", { + params <- model_parameters(m1, effects = "total") + expect_identical(c(nrow(params), ncol(params)), c(8L, 6L)) + expect_named( + params, + c("Group", "Level", "Parameter", "Coefficient", "Component", "Effects") + ) + expect_identical( + as.vector(params$Parameter), + c( + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)" + ) + ) + expect_identical( + params$Component, + c( + "conditional", + "conditional", + "conditional", + "conditional", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "zero_inflated" + ) + ) + expect_equal( + params$Coefficient, + c(0.02278, 0.91717, 1.62455, 2.51811, 1.18248, -0.08808, -0.70703, -1.95584), + tolerance = 1e-2 + ) + }) + + test_that("model_parameters.mixed-ran_pars", { + params <- model_parameters(m1, effects = "random") + expect_identical(c(nrow(params), ncol(params)), c(2L, 9L)) + expect_named( + params, + c( + "Parameter", + "Coefficient", + "SE", + "CI", + "CI_low", + "CI_high", + "Effects", + "Group", + "Component" + ) + ) + expect_identical(params$Parameter, c("SD (Intercept)", "SD (Intercept)")) + expect_identical(params$Component, c("conditional", "zero_inflated")) + expect_equal(params$Coefficient, c(0.9312, 1.17399), tolerance = 1e-2) + }) + + test_that("model_parameters.mixed-all_pars", { + params <- model_parameters(m1, effects = "all") + expect_identical(c(nrow(params), ncol(params)), c(8L, 12L)) + expect_named( + params, + c( + "Parameter", + "Coefficient", + "SE", + "CI", + "CI_low", + "CI_high", + "z", + "df_error", + "p", + "Effects", + "Group", + "Component" + ) + ) + expect_identical( + params$Parameter, + c( + "(Intercept)", + "child", + "camper1", + "(Intercept)", + "child", + "camper1", + "SD (Intercept)", + "SD (Intercept)" + ) + ) + expect_identical( + params$Component, + c( + "conditional", + "conditional", + "conditional", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "conditional", + "zero_inflated" + ) + ) + expect_equal( + params$Coefficient, + c(1.2628, -1.14165, 0.73354, -0.38939, 2.05407, -1.00823, 0.9312, 1.17399), + tolerance = 1e-2 + ) + }) + + test_that("model_parameters.mixed-all", { + params <- model_parameters(m1, effects = "all", group_level = TRUE) + expect_identical(c(nrow(params), ncol(params)), c(14L, 13L)) + expect_named( + params, + c( + "Parameter", + "Level", + "Coefficient", + "SE", + "CI", + "CI_low", + "CI_high", + "z", + "df_error", + "p", + "Component", + "Effects", + "Group" + ) + ) + expect_identical( + params$Parameter, + c( + "(Intercept)", + "child", + "camper1", + "(Intercept)", + "child", + "camper1", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)", + "(Intercept)" + ) + ) + expect_identical( + params$Component, + c( + "conditional", + "conditional", + "conditional", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "conditional", + "conditional", + "conditional", + "conditional", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "zero_inflated" + ) + ) + expect_equal( + params$Coefficient, + c( + 1.2628, + -1.1417, + 0.7335, + -0.3894, + 2.0541, + -1.0082, + -1.24, + -0.3456, + 0.3617, + 1.2553, + 1.5719, + 0.3013, + -0.3176, + -1.5665 + ), + tolerance = 1e-2 + ) + }) + + data(mtcars) + mdisp <- glmmTMB::glmmTMB(hp ~ 0 + wt / mpg, mtcars) + test_that("model_parameters, dispersion", { + mp <- model_parameters(mdisp) + expect_equal(mp$Coefficient, c(59.50992, -0.80396, 48.97731), tolerance = 1e-2) + expect_identical(mp$Parameter, c("wt", "wt:mpg", "(Intercept)")) + expect_identical(mp$Component, c("conditional", "conditional", "dispersion")) + }) + + mdisp <- glmmTMB::glmmTMB(hp ~ 0 + wt / mpg + (1 | gear), mtcars) + test_that("model_parameters, dispersion", { + mp <- model_parameters(mdisp) + expect_equal( + mp$Coefficient, + c(58.25869, -0.87868, 47.01676, 36.99492), + tolerance = 1e-2 + ) + expect_identical( + mp$Parameter, + c("wt", "wt:mpg", "SD (Intercept)", "SD (Observations)") + ) + expect_identical( + mp$Component, + c("conditional", "conditional", "conditional", "conditional") + ) + }) + + m4 <- suppressWarnings(glmmTMB::glmmTMB( + count ~ child + camper + (1 + xb | persons), + ziformula = ~ child + camper + (1 + zg | persons), + data = fish, + family = glmmTMB::truncated_poisson() + )) + + test_that("model_parameters.mixed-ran_pars", { + expect_message( + { + params <- model_parameters(m4, effects = "random") + }, + regex = "Your model may" + ) + expect_identical(c(nrow(params), ncol(params)), c(6L, 9L)) + expect_named( + params, + c( + "Parameter", + "Coefficient", + "SE", + "CI", + "CI_low", + "CI_high", + "Effects", + "Group", + "Component" + ) + ) + expect_identical( + params$Parameter, + c( + "SD (Intercept)", + "SD (xb)", + "Cor (Intercept~xb)", + "SD (Intercept)", + "SD (zg)", + "Cor (Intercept~zg)" + ) + ) + expect_identical( + params$Component, + c( + "conditional", + "conditional", + "conditional", + "zero_inflated", + "zero_inflated", + "zero_inflated" + ) + ) + expect_equal( + params$Coefficient, + c(3.40563, 1.21316, -1, 2.73583, 1.56833, 1), + tolerance = 1e-2 + ) + }) + + # exponentiate for dispersion = sigma parameters ----------------------- + + set.seed(101) + ## rbeta() function parameterized by mean and shape + my_rbeta <- function(n, mu, shape0) { + rbeta(n, shape1 = mu * shape0, shape2 = (1 - mu) * shape0) + } + n <- 100 + ng <- 10 + dd <- data.frame(x = rnorm(n), f = factor(rep(1:(n / ng), ng))) + dd <- transform(dd, y = my_rbeta(n, mu = plogis(-1 + 2 * x + rnorm(ng)[f]), shape0 = 5)) + + m_exp <- glmmTMB::glmmTMB(y ~ x + (1 | f), family = glmmTMB::beta_family(), dd) + + test_that("model_parameters, exp, glmmTMB", { + mp1 <- model_parameters(m_exp, exponentiate = TRUE) + mp2 <- model_parameters(m_exp, exponentiate = FALSE) + expect_equal(mp1$Coefficient, c(0.49271, 6.75824, 5.56294, 1.14541), tolerance = 1e-3) + expect_equal(mp1$Coefficient[3:4], mp2$Coefficient[3:4], tolerance = 1e-3) + }) + + test_that("model_parameters, no dispersion, glmmTMB", { + mp1 <- model_parameters( + m_exp, + effects = "fixed", + component = "conditional", + exponentiate = TRUE + ) + mp2 <- model_parameters( + m_exp, + effects = "fixed", + component = "conditional", + exponentiate = FALSE + ) + expect_equal( + mp1$Coefficient, + unname(exp(unlist(glmmTMB::fixef(m_exp)$cond))), + tolerance = 1e-3 + ) + expect_equal( + mp2$Coefficient, + unname(unlist(glmmTMB::fixef(m_exp)$cond)), + tolerance = 1e-3 + ) + }) + + # proper printing --------------------- + + test_that("print-model_parameters glmmTMB", { + skip_on_os(c("mac", "linux", "solaris")) + skip_if_not(getRversion() >= "4.3.3") + + mp <- model_parameters(m4, effects = "fixed", component = "conditional") + out <- utils::capture.output(print(mp)) + expect_snapshot(out[-5]) + + mp <- model_parameters( + m4, + ci_random = TRUE, + effects = "random", + component = "conditional", + verbose = FALSE + ) + out <- utils::capture.output(print(mp)) + expect_identical( + attributes(mp)$pretty_labels, + c( + `SD (Intercept)` = "SD (Intercept)", + `SD (xb)` = "SD (xb)", + `Cor (Intercept~xb)` = "Cor (Intercept~xb)" + ) + ) + expect_identical( + substr(out, 1, 30), + c( + "# Random Effects", + "", + "Parameter | ", + "------------------------------", + "SD (Intercept: persons) | ", + "SD (xb: persons) | ", + "Cor (Intercept~xb: persons) | " + ) + ) + expect_equal(mp$Coefficient, c(3.40563, 1.21316, -1), tolerance = 1e-3) + expect_equal(mp$CI_low, c(1.64567, 0.5919, -1), tolerance = 1e-3) + + mp <- model_parameters( + m4, + ci_random = TRUE, + effects = "fixed", + component = "zero_inflated" + ) + out <- utils::capture.output(print(mp)) + expect_identical( + attributes(mp)$pretty_labels, + c(`(Intercept)` = "(Intercept)", child = "child", camper1 = "camper [1]") + ) + expect_identical( + substr(out, 1, 12), + c( + "# Fixed Effe", + "", + "Parameter ", + "------------", + "(Intercept) ", + "child ", + "camper [1] " + ) + ) + expect_equal(mp$Coefficient, c(1.88964, 0.15712, -0.17007), tolerance = 1e-3) + expect_equal(mp$CI_low, c(0.5878, -0.78781, -0.92836), tolerance = 1e-3) + + mp <- model_parameters( + m4, + ci_random = TRUE, + effects = "random", + component = "zero_inflated", + verbose = FALSE + ) + out <- utils::capture.output(print(mp)) + expect_identical( + attributes(mp)$pretty_labels, + c( + `SD (Intercept)` = "SD (Intercept)", + `SD (zg)` = "SD (zg)", + `Cor (Intercept~zg)` = "Cor (Intercept~zg)" + ) + ) + expect_identical( + substr(out, 1, 30), + c( + "# Random Effects (Zero-Inflati", + "", + "Parameter | ", + "------------------------------", + "SD (Intercept: persons) | ", + "SD (zg: persons) | ", + "Cor (Intercept~zg: persons) | " + ) + ) + expect_equal(mp$Coefficient, c(2.73583, 1.56833, 1), tolerance = 1e-3) + expect_equal(mp$CI_low, c(1.16329, 0.64246, -1), tolerance = 1e-3) + + mp <- model_parameters( + m4, + ci_random = TRUE, + effects = "all", + component = "conditional", + verbose = FALSE + ) + out <- utils::capture.output(print(mp)) + expect_identical( + attributes(mp)$pretty_labels, + c( + `(Intercept)` = "(Intercept)", + child = "child", + camper1 = "camper [1]", + `SD (Intercept)` = "SD (Intercept)", + `SD (xb)` = "SD (xb)", + `Cor (Intercept~xb)` = "Cor (Intercept~xb)" + ) + ) + expect_identical( + substr(out, 1, 30), + c( + "# Fixed Effects", + "", + "Parameter | Log-Mean | SE ", + "------------------------------", + "(Intercept) | 2.55 | 0.25 ", + "child | -1.09 | 0.10 ", + "camper [1] | 0.27 | 0.10 ", + "", + "# Random Effects", + "", + "Parameter | ", + "------------------------------", + "SD (Intercept: persons) | ", + "SD (xb: persons) | ", + "Cor (Intercept~xb: persons) | " + ) + ) + expect_equal( + mp$Coefficient, + c(2.54713, -1.08747, 0.2723, 3.40563, 1.21316, -1), + tolerance = 1e-3 + ) + expect_equal( + mp$CI_low, + c(2.06032, -1.27967, 0.07461, 1.64567, 0.5919, -1), + tolerance = 1e-3 + ) + + mp <- model_parameters( + m4, + effects = "all", + ci_random = TRUE, + component = "zero_inflated", + verbose = FALSE + ) + out <- utils::capture.output(print(mp)) + expect_identical( + attributes(mp)$pretty_labels, + c( + `(Intercept)` = "(Intercept)", + child = "child", + camper1 = "camper [1]", + `SD (Intercept)` = "SD (Intercept)", + `SD (zg)` = "SD (zg)", + `Cor (Intercept~zg)` = "Cor (Intercept~zg)" + ) + ) + expect_identical( + substr(out, 1, 30), + c( + "# Fixed Effects (Zero-Inflatio", + "", + "Parameter | Log-Mean | SE ", + "------------------------------", + "(Intercept) | 1.89 | 0.66 ", + "child | 0.16 | 0.48 ", + "camper [1] | -0.17 | 0.39 ", + "", + "# Random Effects (Zero-Inflati", + "", + "Parameter | ", + "------------------------------", + "SD (Intercept: persons) | ", + "SD (zg: persons) | ", + "Cor (Intercept~zg: persons) | " + ) + ) + expect_equal( + mp$Coefficient, + c(1.88964, 0.15712, -0.17007, 2.73583, 1.56833, 1), + tolerance = 1e-3 + ) + expect_equal( + mp$CI_low, + c(0.5878, -0.78781, -0.92836, 1.16329, 0.64246, -1), + tolerance = 1e-3 + ) + + mp <- model_parameters( + m4, + effects = "all", + component = "all", + ci_random = TRUE, + verbose = FALSE + ) + out <- utils::capture.output(print(mp)) + expect_identical( + attributes(mp)$pretty_labels, + c( + `(Intercept)` = "(Intercept)", + child = "child", + camper1 = "camper [1]", + `(Intercept)` = "(Intercept)", + child = "child", + camper1 = "camper1", # nolint + `SD (Intercept)` = "SD (Intercept)", + `SD (xb)` = "SD (xb)", + `Cor (Intercept~xb)` = "Cor (Intercept~xb)", + `SD (Intercept)` = "SD (Intercept)", + `SD (zg)` = "SD (zg)", # nolint + `Cor (Intercept~zg)` = "Cor (Intercept~zg)" + ) + ) + expect_identical( + substr(out, 1, 30), + c( + "# Fixed Effects (Count Model)", + "", + "Parameter | Log-Mean | SE ", + "------------------------------", + "(Intercept) | 2.55 | 0.25 ", + "child | -1.09 | 0.10 ", + "camper [1] | 0.27 | 0.10 ", + "", + "# Fixed Effects (Zero-Inflatio", + "", + "Parameter | Log-Odds | SE ", + "------------------------------", + "(Intercept) | 1.89 | 0.66 ", + "child | 0.16 | 0.48 ", + "camper [1] | -0.17 | 0.39 ", + "", + "# Random Effects Variances", + "", + "Parameter | ", + "------------------------------", + "SD (Intercept: persons) | ", + "SD (xb: persons) | ", + "Cor (Intercept~xb: persons) | ", + "", + "# Random Effects (Zero-Inflati", + "", + "Parameter | ", + "------------------------------", + "SD (Intercept: persons) | ", + "SD (zg: persons) | ", + "Cor (Intercept~zg: persons) | " + ) + ) + expect_equal( + mp$Coefficient, + c( + 2.54713, + -1.08747, + 0.2723, + 1.88964, + 0.15712, + -0.17007, + 3.40563, + 1.21316, + -1, + 2.73583, + 1.56833, + 1 + ), + tolerance = 1e-3 + ) + expect_equal( + mp$CI_low, + c( + 2.06032, + -1.27967, + 0.07461, + 0.5878, + -0.78781, + -0.92836, + 1.64567, + 0.5919, + -1, + 1.16329, + 0.64246, + -1 + ), + tolerance = 1e-3 + ) + }) + + # proper printing of digits --------------------- + + test_that("print-model_parameters glmmTMB digits", { + skip_on_os(c("mac", "linux", "solaris")) + skip_if_not(getRversion() >= "4.3.3") + + mp <- model_parameters(m4, ci_random = TRUE, effects = "all", component = "all") + out <- utils::capture.output(print(mp, digits = 4, ci_digits = 5)) + expect_snapshot(out[-c(5, 14)]) + + mp <- model_parameters( + m4, + effects = "all", + component = "all", + ci_random = TRUE, + digits = 4, + ci_digits = 5 + ) + out <- utils::capture.output(print(mp)) + expect_snapshot(out[-c(5, 14)]) + }) + + # proper alignment of CIs --------------------- + test_that("print-model_parameters glmmTMB CI alignment", { + skip_if_not_installed("curl") + skip_if_offline() + skip_on_os(c("mac", "linux", "solaris")) + skip_if_not(getRversion() >= "4.3.3") + + model_pr <- tryCatch( + { + load(url( + "https://github.com/d-morrison/parameters/raw/glmmTMB/data/pressure_durations.RData" + )) + glmmTMB::glmmTMB( + formula = n_samples ~ Surface + Side + Jaw + (1 | Participant / Session), + ziformula = ~ Surface + Side + Jaw + (1 | Participant / Session), + dispformula = ~1, + family = glmmTMB::nbinom2(), + data = pressure_durations ) - ) - expect_equal( - params$Coefficient, - c( - 1.2628, -1.1417, 0.7335, -0.3894, 2.0541, -1.0082, -1.24, -0.3456, - 0.3617, 1.2553, 1.5719, 0.3013, -0.3176, -1.5665 - ), - tolerance = 1e-2 - ) - }) - - - data(mtcars) - mdisp <- glmmTMB::glmmTMB(hp ~ 0 + wt / mpg, mtcars) - test_that("model_parameters, dispersion", { - mp <- model_parameters(mdisp) - expect_equal(mp$Coefficient, c(59.50992, -0.80396, 48.97731), tolerance = 1e-2) - expect_identical(mp$Parameter, c("wt", "wt:mpg", "(Intercept)")) - expect_identical(mp$Component, c("conditional", "conditional", "dispersion")) - }) - - mdisp <- glmmTMB::glmmTMB(hp ~ 0 + wt / mpg + (1 | gear), mtcars) - test_that("model_parameters, dispersion", { - mp <- model_parameters(mdisp) - expect_equal(mp$Coefficient, c(58.25869, -0.87868, 47.01676, 36.99492), tolerance = 1e-2) - expect_identical(mp$Parameter, c("wt", "wt:mpg", "SD (Intercept)", "SD (Observations)")) - expect_identical(mp$Component, c("conditional", "conditional", "conditional", "conditional")) - }) - - - m4 <- suppressWarnings(glmmTMB::glmmTMB( - count ~ child + camper + (1 + xb | persons), - ziformula = ~ child + camper + (1 + zg | persons), - data = fish, - family = glmmTMB::truncated_poisson() + }, + error = function(e) { + NULL + } + ) + + mp <- model_parameters( + model_pr, + effects = "random", + component = "all", + ci_random = TRUE + ) + expect_snapshot(print(mp)) + + mp <- model_parameters(model_pr, effects = "fixed", component = "all") + expect_snapshot(print(mp)) + }) + + test_that("model_parameters.mixed-all", { + skip_on_os(c("mac", "linux", "solaris")) + skip_if_not(getRversion() >= "4.3.3") + + params <- model_parameters(m4, effects = "all") + expect_identical(c(nrow(params), ncol(params)), c(12L, 12L)) + expect_identical( + colnames(params), + c( + "Parameter", + "Coefficient", + "SE", + "CI", + "CI_low", + "CI_high", + "z", + "df_error", + "p", + "Effects", + "Group", + "Component" + ) + ) + expect_identical( + params$Parameter, + c( + "(Intercept)", + "child", + "camper1", + "(Intercept)", + "child", + "camper1", + "SD (Intercept)", + "SD (xb)", + "Cor (Intercept~xb)", + "SD (Intercept)", + "SD (zg)", + "Cor (Intercept~zg)" + ) + ) + expect_identical( + params$Component, + c( + "conditional", + "conditional", + "conditional", + "zero_inflated", + "zero_inflated", + "zero_inflated", + "conditional", + "conditional", + "conditional", + "zero_inflated", + "zero_inflated", + "zero_inflated" + ) + ) + expect_equal( + params$Coefficient, + c( + 2.54713, + -1.08747, + 0.2723, + 1.88964, + 0.15712, + -0.17007, + 3.40563, + 1.21316, + -1, + 2.73583, + 1.56833, + 1 + ), + tolerance = 1e-2 + ) + }) + + test_that("print-model_parameters", { + skip_on_os(c("mac", "linux", "solaris")) + skip_if_not(getRversion() >= "4.3.3") + + mp <- model_parameters(m1, effects = "fixed", verbose = FALSE) + expect_snapshot(mp) + + mp <- model_parameters(m1, effects = "fixed", exponentiate = TRUE, verbose = FALSE) + expect_snapshot(mp) + + mp <- model_parameters(m1, effects = "all", verbose = FALSE) + expect_snapshot(mp) + }) + + test_that("print-model_parameters, random dispersion", { + data(Salamanders, package = "glmmTMB") + m <- glmmTMB::glmmTMB( + count ~ spp + cover + mined + (1 | site), + ziformula = ~ spp + mined, + dispformula = ~ DOY + (1 | site), + data = Salamanders, + family = glmmTMB::nbinom1() + ) + out <- as.data.frame(model_parameters(m, effects = "fixed", component = "all")) + expect_identical(nrow(out), 19L) + out <- as.data.frame(model_parameters(m, effects = "random", component = "all")) + expect_identical(nrow(out), 1L) + out <- as.data.frame(model_parameters( + m, + effects = "random", + component = "all", + group_level = TRUE )) - - test_that("model_parameters.mixed-ran_pars", { - expect_message( - { - params <- model_parameters(m4, effects = "random") - }, - regex = "Your model may" - ) - expect_identical(c(nrow(params), ncol(params)), c(6L, 9L)) - expect_named( - params, - c("Parameter", "Coefficient", "SE", "CI", "CI_low", "CI_high", "Effects", "Group", "Component") - ) - expect_identical( - params$Parameter, - c( - "SD (Intercept)", "SD (xb)", "Cor (Intercept~xb)", - "SD (Intercept)", "SD (zg)", "Cor (Intercept~zg)" - ) - ) - expect_identical( - params$Component, - c( - "conditional", "conditional", "conditional", - "zero_inflated", "zero_inflated", "zero_inflated" - ) - ) - expect_equal( - params$Coefficient, - c(3.40563, 1.21316, -1, 2.73583, 1.56833, 1), - tolerance = 1e-2 - ) - }) - - - # exponentiate for dispersion = sigma parameters ----------------------- - - set.seed(101) - ## rbeta() function parameterized by mean and shape - my_rbeta <- function(n, mu, shape0) { - rbeta(n, shape1 = mu * shape0, shape2 = (1 - mu) * shape0) - } - n <- 100 - ng <- 10 - dd <- data.frame(x = rnorm(n), f = factor(rep(1:(n / ng), ng))) - dd <- transform(dd, y = my_rbeta(n, mu = plogis(-1 + 2 * x + rnorm(ng)[f]), shape0 = 5)) - - m_exp <- glmmTMB::glmmTMB(y ~ x + (1 | f), family = glmmTMB::beta_family(), dd) - - test_that("model_parameters, exp, glmmTMB", { - mp1 <- model_parameters(m_exp, exponentiate = TRUE) - mp2 <- model_parameters(m_exp, exponentiate = FALSE) - expect_equal(mp1$Coefficient, c(0.49271, 6.75824, 5.56294, 1.14541), tolerance = 1e-3) - expect_equal(mp1$Coefficient[3:4], mp2$Coefficient[3:4], tolerance = 1e-3) - }) - - test_that("model_parameters, no dispersion, glmmTMB", { - mp1 <- model_parameters(m_exp, effects = "fixed", component = "conditional", exponentiate = TRUE) - mp2 <- model_parameters(m_exp, effects = "fixed", component = "conditional", exponentiate = FALSE) - expect_equal(mp1$Coefficient, unname(exp(unlist(glmmTMB::fixef(m_exp)$cond))), tolerance = 1e-3) - expect_equal(mp2$Coefficient, unname(unlist(glmmTMB::fixef(m_exp)$cond)), tolerance = 1e-3) - }) - - - # proper printing --------------------- - - test_that("print-model_parameters glmmTMB", { - skip_on_os(c("mac", "linux", "solaris")) - skip_if_not(getRversion() >= "4.3.3") - - mp <- model_parameters(m4, effects = "fixed", component = "conditional") - out <- utils::capture.output(print(mp)) - expect_snapshot(out[-5]) - - - mp <- model_parameters(m4, ci_random = TRUE, effects = "random", component = "conditional", verbose = FALSE) - out <- utils::capture.output(print(mp)) - expect_identical( - attributes(mp)$pretty_labels, - c( - `SD (Intercept)` = "SD (Intercept)", `SD (xb)` = "SD (xb)", - `Cor (Intercept~xb)` = "Cor (Intercept~xb)" - ) - ) - expect_identical( - substr(out, 1, 30), - c( - "# Random Effects", - "", - "Parameter | ", - "------------------------------", - "SD (Intercept: persons) | ", - "SD (xb: persons) | ", - "Cor (Intercept~xb: persons) | " - ) - ) - expect_equal(mp$Coefficient, c(3.40563, 1.21316, -1), tolerance = 1e-3) - expect_equal(mp$CI_low, c(1.64567, 0.5919, -1), tolerance = 1e-3) - - - mp <- model_parameters(m4, ci_random = TRUE, effects = "fixed", component = "zero_inflated") - out <- utils::capture.output(print(mp)) - expect_identical( - attributes(mp)$pretty_labels, - c(`(Intercept)` = "(Intercept)", child = "child", camper1 = "camper [1]") - ) - expect_identical( - substr(out, 1, 12), - c( - "# Fixed Effe", "", "Parameter ", "------------", "(Intercept) ", - "child ", "camper [1] " - ) - ) - expect_equal(mp$Coefficient, c(1.88964, 0.15712, -0.17007), tolerance = 1e-3) - expect_equal(mp$CI_low, c(0.5878, -0.78781, -0.92836), tolerance = 1e-3) - - - mp <- model_parameters(m4, ci_random = TRUE, effects = "random", component = "zero_inflated", verbose = FALSE) - out <- utils::capture.output(print(mp)) - expect_identical( - attributes(mp)$pretty_labels, - c( - `SD (Intercept)` = "SD (Intercept)", `SD (zg)` = "SD (zg)", - `Cor (Intercept~zg)` = "Cor (Intercept~zg)" - ) - ) - expect_identical( - substr(out, 1, 30), - c( - "# Random Effects (Zero-Inflati", "", "Parameter | ", - "------------------------------", "SD (Intercept: persons) | ", - "SD (zg: persons) | ", "Cor (Intercept~zg: persons) | " - ) - ) - expect_equal(mp$Coefficient, c(2.73583, 1.56833, 1), tolerance = 1e-3) - expect_equal(mp$CI_low, c(1.16329, 0.64246, -1), tolerance = 1e-3) - - - mp <- model_parameters(m4, ci_random = TRUE, effects = "all", component = "conditional", verbose = FALSE) - out <- utils::capture.output(print(mp)) - expect_identical( - attributes(mp)$pretty_labels, - c( - `(Intercept)` = "(Intercept)", child = "child", camper1 = "camper [1]", - `SD (Intercept)` = "SD (Intercept)", `SD (xb)` = "SD (xb)", - `Cor (Intercept~xb)` = "Cor (Intercept~xb)" - ) - ) - expect_identical( - substr(out, 1, 30), - c( - "# Fixed Effects", "", "Parameter | Log-Mean | SE ", - "------------------------------", - "(Intercept) | 2.55 | 0.25 ", "child | -1.09 | 0.10 ", - "camper [1] | 0.27 | 0.10 ", "", "# Random Effects", "", - "Parameter | ", "------------------------------", - "SD (Intercept: persons) | ", "SD (xb: persons) | ", - "Cor (Intercept~xb: persons) | " - ) - ) - expect_equal(mp$Coefficient, c(2.54713, -1.08747, 0.2723, 3.40563, 1.21316, -1), tolerance = 1e-3) - expect_equal(mp$CI_low, c(2.06032, -1.27967, 0.07461, 1.64567, 0.5919, -1), tolerance = 1e-3) - - - mp <- model_parameters(m4, effects = "all", ci_random = TRUE, component = "zero_inflated", verbose = FALSE) - out <- utils::capture.output(print(mp)) - expect_identical( - attributes(mp)$pretty_labels, - c( - `(Intercept)` = "(Intercept)", child = "child", camper1 = "camper [1]", - `SD (Intercept)` = "SD (Intercept)", `SD (zg)` = "SD (zg)", - `Cor (Intercept~zg)` = "Cor (Intercept~zg)" - ) - ) - expect_identical( - substr(out, 1, 30), - c( - "# Fixed Effects (Zero-Inflatio", "", "Parameter | Log-Mean | SE ", - "------------------------------", "(Intercept) | 1.89 | 0.66 ", - "child | 0.16 | 0.48 ", "camper [1] | -0.17 | 0.39 ", - "", "# Random Effects (Zero-Inflati", "", "Parameter | ", - "------------------------------", "SD (Intercept: persons) | ", - "SD (zg: persons) | ", "Cor (Intercept~zg: persons) | " - ) - ) - expect_equal(mp$Coefficient, c(1.88964, 0.15712, -0.17007, 2.73583, 1.56833, 1), tolerance = 1e-3) - expect_equal(mp$CI_low, c(0.5878, -0.78781, -0.92836, 1.16329, 0.64246, -1), tolerance = 1e-3) - - - mp <- model_parameters(m4, effects = "all", component = "all", ci_random = TRUE, verbose = FALSE) - out <- utils::capture.output(print(mp)) - expect_identical( - attributes(mp)$pretty_labels, - c( - `(Intercept)` = "(Intercept)", child = "child", camper1 = "camper [1]", - `(Intercept)` = "(Intercept)", child = "child", camper1 = "camper1", # nolint - `SD (Intercept)` = "SD (Intercept)", `SD (xb)` = "SD (xb)", - `Cor (Intercept~xb)` = "Cor (Intercept~xb)", - `SD (Intercept)` = "SD (Intercept)", `SD (zg)` = "SD (zg)", # nolint - `Cor (Intercept~zg)` = "Cor (Intercept~zg)" - ) - ) - expect_identical( - substr(out, 1, 30), - c( - "# Fixed Effects (Count Model)", "", "Parameter | Log-Mean | SE ", - "------------------------------", "(Intercept) | 2.55 | 0.25 ", - "child | -1.09 | 0.10 ", "camper [1] | 0.27 | 0.10 ", - "", "# Fixed Effects (Zero-Inflatio", "", "Parameter | Log-Odds | SE ", - "------------------------------", "(Intercept) | 1.89 | 0.66 ", - "child | 0.16 | 0.48 ", "camper [1] | -0.17 | 0.39 ", - "", "# Random Effects Variances", "", "Parameter | ", - "------------------------------", "SD (Intercept: persons) | ", - "SD (xb: persons) | ", "Cor (Intercept~xb: persons) | ", - "", "# Random Effects (Zero-Inflati", "", "Parameter | ", - "------------------------------", "SD (Intercept: persons) | ", - "SD (zg: persons) | ", "Cor (Intercept~zg: persons) | " - ) - ) - expect_equal( - mp$Coefficient, - c( - 2.54713, -1.08747, 0.2723, 1.88964, 0.15712, -0.17007, 3.40563, - 1.21316, -1, 2.73583, 1.56833, 1 - ), - tolerance = 1e-3 - ) - expect_equal( - mp$CI_low, - c( - 2.06032, -1.27967, 0.07461, 0.5878, -0.78781, -0.92836, 1.64567, - 0.5919, -1, 1.16329, 0.64246, -1 - ), - tolerance = 1e-3 - ) - }) - - - # proper printing of digits --------------------- - - test_that("print-model_parameters glmmTMB digits", { - skip_on_os(c("mac", "linux", "solaris")) - skip_if_not(getRversion() >= "4.3.3") - - mp <- model_parameters(m4, ci_random = TRUE, effects = "all", component = "all") - out <- utils::capture.output(print(mp, digits = 4, ci_digits = 5)) - expect_snapshot(out[-c(5, 14)]) - - mp <- model_parameters(m4, effects = "all", component = "all", ci_random = TRUE, digits = 4, ci_digits = 5) - out <- utils::capture.output(print(mp)) - expect_snapshot(out[-c(5, 14)]) - }) - - - # proper alignment of CIs --------------------- - test_that("print-model_parameters glmmTMB CI alignment", { - skip_if_not_installed("curl") - skip_if_offline() - skip_on_os(c("mac", "linux", "solaris")) - skip_if_not(getRversion() >= "4.3.3") - - model_pr <- tryCatch( - { - load(url("https://github.com/d-morrison/parameters/raw/glmmTMB/data/pressure_durations.RData")) - glmmTMB::glmmTMB( - formula = n_samples ~ Surface + Side + Jaw + (1 | Participant / Session), - ziformula = ~ Surface + Side + Jaw + (1 | Participant / Session), - dispformula = ~1, - family = glmmTMB::nbinom2(), - data = pressure_durations - ) - }, - error = function(e) { - NULL - } - ) - - mp <- model_parameters(model_pr, effects = "random", component = "all", ci_random = TRUE) - expect_snapshot(print(mp)) - - mp <- model_parameters(model_pr, effects = "fixed", component = "all") - expect_snapshot(print(mp)) - }) - - - test_that("model_parameters.mixed-all", { - skip_on_os(c("mac", "linux", "solaris")) - skip_if_not(getRversion() >= "4.3.3") - - params <- model_parameters(m4, effects = "all") - expect_identical(c(nrow(params), ncol(params)), c(12L, 12L)) - expect_identical( - colnames(params), - c( - "Parameter", "Coefficient", "SE", "CI", "CI_low", "CI_high", - "z", "df_error", "p", "Effects", "Group", "Component" - ) - ) - expect_identical( - params$Parameter, - c( - "(Intercept)", "child", "camper1", "(Intercept)", "child", - "camper1", "SD (Intercept)", "SD (xb)", "Cor (Intercept~xb)", - "SD (Intercept)", "SD (zg)", "Cor (Intercept~zg)" - ) - ) - expect_identical( - params$Component, - c( - "conditional", "conditional", "conditional", "zero_inflated", - "zero_inflated", "zero_inflated", "conditional", "conditional", - "conditional", "zero_inflated", "zero_inflated", "zero_inflated" - ) - ) - expect_equal( - params$Coefficient, - c( - 2.54713, -1.08747, 0.2723, 1.88964, 0.15712, -0.17007, 3.40563, - 1.21316, -1, 2.73583, 1.56833, 1 - ), - tolerance = 1e-2 - ) - }) - - test_that("print-model_parameters", { - skip_on_os(c("mac", "linux", "solaris")) - skip_if_not(getRversion() >= "4.3.3") - - mp <- model_parameters(m1, effects = "fixed", verbose = FALSE) - expect_snapshot(mp) - - mp <- model_parameters(m1, effects = "fixed", exponentiate = TRUE, verbose = FALSE) - expect_snapshot(mp) - - mp <- model_parameters(m1, effects = "all", verbose = FALSE) - expect_snapshot(mp) - }) - - test_that("print-model_parameters, random dispersion", { - data(Salamanders, package = "glmmTMB") - m <- glmmTMB::glmmTMB( - count ~ spp + cover + mined + (1 | site), - ziformula = ~ spp + mined, - dispformula = ~ DOY + (1 | site), - data = Salamanders, - family = glmmTMB::nbinom1() - ) - out <- as.data.frame(model_parameters(m, effects = "fixed", component = "all")) - expect_identical(nrow(out), 19L) - out <- as.data.frame(model_parameters(m, effects = "random", component = "all")) - expect_identical(nrow(out), 1L) - out <- as.data.frame(model_parameters(m, effects = "random", component = "all", group_level = TRUE)) - expect_identical(nrow(out), 46L) - expect_equal(out$Coefficient, unlist(glmmTMB::ranef(m)), ignore_attr = TRUE, tolerance = 1e-4) - }) - - test_that("robust SE/VCOV", { - skip_if_not_installed("sandwich") - skip_if(packageVersion("insight") <= "1.4.0") - - out1 <- standard_error(m1) - out2 <- sqrt(diag(insight::get_varcov(m1, component = "all"))) - expect_equal(out1$SE, out2[1:6], ignore_attr = TRUE, tolerance = 1e-4) - - out1 <- standard_error(m1, vcov = "HC0") - out2 <- sqrt(diag(insight::get_varcov(m1, vcov = "HC0", component = "all"))) - expect_equal(out1$SE, out2[1:6], ignore_attr = TRUE, tolerance = 1e-4) - - out <- model_parameters(m1, vcov = "HC0") - expect_snapshot(print(out, table_width = Inf)) - }) - } -) + expect_identical(nrow(out), 46L) + expect_equal( + out$Coefficient, + unlist(glmmTMB::ranef(m)), + ignore_attr = TRUE, + tolerance = 1e-4 + ) + }) + + test_that("robust SE/VCOV", { + skip_if_not_installed("sandwich") + skip_if(packageVersion("insight") <= "1.4.0") + + out1 <- standard_error(m1) + out2 <- sqrt(diag(insight::get_varcov(m1, component = "all"))) + expect_equal(out1$SE, out2[1:6], ignore_attr = TRUE, tolerance = 1e-4) + + out1 <- standard_error(m1, vcov = "HC0") + out2 <- sqrt(diag(insight::get_varcov(m1, vcov = "HC0", component = "all"))) + expect_equal(out1$SE, out2[1:6], ignore_attr = TRUE, tolerance = 1e-4) + + out <- model_parameters(m1, vcov = "HC0") + expect_snapshot(print(out, table_width = Inf)) + }) +}) diff --git a/tests/testthat/test-model_parameters.bracl.R b/tests/testthat/test-model_parameters.bracl.R index b2eb3e25f..8b3ae7dde 100644 --- a/tests/testthat/test-model_parameters.bracl.R +++ b/tests/testthat/test-model_parameters.bracl.R @@ -1,14 +1,18 @@ +skip_on_cran() + skip_if_not_installed("brglm2") skip_if_not_installed("faraway") skip_if_not(packageVersion("insight") > "0.19.1") -skip_on_cran() - test_that("model_parameters.bracl", { data("cns", package = "faraway") - cns2 <- reshape(cns, - direction = "long", timevar = "Type", - times = names(cns)[3:5], varying = 3:5, v.names = "Freq" + cns2 <- reshape( + cns, + direction = "long", + timevar = "Type", + times = names(cns)[3:5], + varying = 3:5, + v.names = "Freq" )[, 3:6] cns2$Type <- factor(cns2$Type, levels = unique(cns2$Type)) @@ -18,21 +22,30 @@ test_that("model_parameters.bracl", { expect_named( mpbracl, c( - "Parameter", "Coefficient", "SE", "CI", "CI_low", "CI_high", - "z", "df_error", "p", "Response" + "Parameter", + "Coefficient", + "SE", + "CI", + "CI_low", + "CI_high", + "z", + "df_error", + "p", + "Response" ) ) expect_identical( mpbracl$Parameter, c( - "An:(Intercept)", "Sp:(Intercept)", "An:Water", "Sp:Water", - "An:WorkNonManual", "Sp:WorkNonManual" + "An:(Intercept)", + "Sp:(Intercept)", + "An:Water", + "Sp:Water", + "An:WorkNonManual", + "Sp:WorkNonManual" ) ) - expect_identical( - mpbracl$Response, - c("An", "Sp", "An", "Sp", "An", "Sp") - ) + expect_identical(mpbracl$Response, c("An", "Sp", "An", "Sp", "An", "Sp")) expect_equal( mpbracl$Coefficient, c(-0.37392, 1.49063, 0.00129, -0.00349, -0.11292, 0.36384), diff --git a/tests/testthat/test-model_parameters.fixest_multi.R b/tests/testthat/test-model_parameters.fixest_multi.R index 70d054b64..e6e7f5f36 100644 --- a/tests/testthat/test-model_parameters.fixest_multi.R +++ b/tests/testthat/test-model_parameters.fixest_multi.R @@ -1,13 +1,15 @@ -skip_if_not_installed("fixest") - skip_on_cran() +skip_if_not_installed("fixest") set.seed(123) iris$x <- rnorm(150) test_that("model_parameters.fixest_multi", { - mod <- fixest::feols(c(Petal.Width, Sepal.Width) ~ x + csw0(Petal.Length, Sepal.Length) | Species, iris) + mod <- fixest::feols( + c(Petal.Width, Sepal.Width) ~ x + csw0(Petal.Length, Sepal.Length) | Species, + iris + ) expect_snapshot(print(model_parameters(mod))) expect_snapshot(print(ci(mod))) }) diff --git a/tests/testthat/test-model_parameters.mclogit.R b/tests/testthat/test-model_parameters.mclogit.R index 924baf4cc..e1a0c4617 100644 --- a/tests/testthat/test-model_parameters.mclogit.R +++ b/tests/testthat/test-model_parameters.mclogit.R @@ -1,42 +1,33 @@ +skip_on_cran() + skip_if_not_installed("mclogit") skip_if_not_installed("withr") skip_if_not(packageVersion("insight") > "0.19.1") -skip_on_cran() - -withr::with_options( - list(parameters_exponentiate = FALSE), - { - data(Transport, package = "mclogit") - invisible(capture.output({ - m1 <- mclogit::mclogit( - cbind(resp, suburb) ~ distance + cost, - data = Transport - ) - })) +withr::with_options(list(parameters_exponentiate = FALSE), { + data(Transport, package = "mclogit") + invisible(capture.output({ + m1 <- mclogit::mclogit(cbind(resp, suburb) ~ distance + cost, data = Transport) + })) - data(housing, package = "MASS") - invisible(capture.output({ - m2 <- mclogit::mblogit(Sat ~ Infl + Type + Cont, - weights = Freq, - data = housing - ) - })) + data(housing, package = "MASS") + invisible(capture.output({ + m2 <- mclogit::mblogit(Sat ~ Infl + Type + Cont, weights = Freq, data = housing) + })) - test_that("model_parameters.mclogit", { - params <- model_parameters(m1) - expect_snapshot(params) - }) - test_that("model_parameters.mblogit", { - params <- model_parameters(m2) - expect_snapshot(params) - }) + test_that("model_parameters.mclogit", { + params <- model_parameters(m1) + expect_snapshot(params) + }) + test_that("model_parameters.mblogit", { + params <- model_parameters(m2) + expect_snapshot(params) + }) - skip_on_os(c("mac", "linux")) - test_that("simulate_parameters.mblogit", { - set.seed(1234) - params <- simulate_parameters(m2) - expect_snapshot(params) - }) - } -) + skip_on_os(c("mac", "linux")) + test_that("simulate_parameters.mblogit", { + set.seed(1234) + params <- simulate_parameters(m2) + expect_snapshot(params) + }) +}) diff --git a/tests/testthat/test-model_parameters.mediate.R b/tests/testthat/test-model_parameters.mediate.R index 1d100fd45..57f28aedb 100644 --- a/tests/testthat/test-model_parameters.mediate.R +++ b/tests/testthat/test-model_parameters.mediate.R @@ -1,3 +1,4 @@ +skip_on_cran() skip_if_not_installed("mediation") skip_if_not_installed("MASS") @@ -10,20 +11,33 @@ m1 <- mediation::mediate(b, c, sims = 50, treat = "treat", mediator = "job_seek" b2 <- lm(job_seek ~ educ + sex, data = jobs) c2 <- lm(depress2 ~ educ + job_seek + sex, data = jobs) set.seed(1234) -m2 <- mediation::mediate(b2, c2, - treat = "educ", mediator = "job_seek", sims = 50, - control.value = "gradwk", treat.value = "somcol" +m2 <- mediation::mediate( + b2, + c2, + treat = "educ", + mediator = "job_seek", + sims = 50, + control.value = "gradwk", + treat.value = "somcol" ) test_that("model_parameters.mediate-1", { params <- model_parameters(m1) - expect_equal(params$Estimate, c(-0.01488, -0.04753, -0.06242, 0.16635), tolerance = 1e-2) + expect_equal( + params$Estimate, + c(-0.01488, -0.04753, -0.06242, 0.16635), + tolerance = 1e-2 + ) expect_equal(params$Parameter, c("ACME", "ADE", "Total Effect", "Prop. Mediated")) }) test_that("model_parameters.mediate-2", { params <- model_parameters(m2) - expect_equal(params$Estimate, c(0.02484, -0.05793, -0.03309, -0.27914), tolerance = 1e-2) + expect_equal( + params$Estimate, + c(0.02484, -0.05793, -0.03309, -0.27914), + tolerance = 1e-2 + ) expect_equal(params$Parameter, c("ACME", "ADE", "Total Effect", "Prop. Mediated")) }) @@ -48,21 +62,60 @@ test_that("model_parameters.mediate-3", { family = binomial(link = "probit") ) set.seed(1234) - m3 <- mediation::mediate(b.ord, d.bin, sims = 50, treat = "treat", mediator = "job_disc") + m3 <- mediation::mediate( + b.ord, + d.bin, + sims = 50, + treat = "treat", + mediator = "job_disc" + ) params <- model_parameters(m3) - expect_equal(params$Estimate, c( - 0.00216, 0.00231, 0.0486, 0.04875, 0.05091, 0.03981, 0.04829, - 0.00223, 0.04868, 0.04405 - ), tolerance = 1e-2) - expect_equal(params$Parameter, c( - "ACME (control)", "ACME (treated)", "ADE (control)", "ADE (treated)", - "Total Effect", "Prop. Mediated (control)", "Prop. Mediated (treated)", - "ACME (average)", "ADE (average)", "Prop. Mediated (average)" - )) - expect_equal(params$Component, c( - "control", "treated", "control", "treated", "Total Effect", - "control", "treated", "average", "average", "average" - )) + expect_equal( + params$Estimate, + c( + 0.00216, + 0.00231, + 0.0486, + 0.04875, + 0.05091, + 0.03981, + 0.04829, + 0.00223, + 0.04868, + 0.04405 + ), + tolerance = 1e-2 + ) + expect_equal( + params$Parameter, + c( + "ACME (control)", + "ACME (treated)", + "ADE (control)", + "ADE (treated)", + "Total Effect", + "Prop. Mediated (control)", + "Prop. Mediated (treated)", + "ACME (average)", + "ADE (average)", + "Prop. Mediated (average)" + ) + ) + expect_equal( + params$Component, + c( + "control", + "treated", + "control", + "treated", + "Total Effect", + "control", + "treated", + "average", + "average", + "average" + ) + ) }) diff --git a/tests/testthat/test-model_parameters_std_mixed.R b/tests/testthat/test-model_parameters_std_mixed.R index 1875516f7..adb694cf7 100644 --- a/tests/testthat/test-model_parameters_std_mixed.R +++ b/tests/testthat/test-model_parameters_std_mixed.R @@ -1,7 +1,7 @@ -skip_if_not_installed("effectsize") -skip_if_not_installed("lme4") skip_on_cran() +skip_if_not_installed("effectsize") +skip_if_not_installed("lme4") data(iris) set.seed(1234) @@ -24,15 +24,7 @@ test_that("model_parameters, standardize-refit, wald-normal", { expect_equal(c(nrow(params), ncol(params)), c(7, 10)) expect_equal( params$Coefficient, - c( - 0.96949, - -1.28631, - -1.81461, - 0.34791, - 1.74252, - -0.25421, - -0.18834 - ), + c(0.96949, -1.28631, -1.81461, 0.34791, 1.74252, -0.25421, -0.18834), tolerance = 1e-3 ) expect_equal( @@ -42,15 +34,7 @@ test_that("model_parameters, standardize-refit, wald-normal", { ) expect_equal( params$CI_high, - c( - 1.37031, - -0.77301, - -1.14754, - 0.46488, - 2.01523, - -0.06287, - -0.00312 - ), + c(1.37031, -0.77301, -1.14754, 0.46488, 2.01523, -0.06287, -0.00312), tolerance = 1e-3 ) }) @@ -65,15 +49,7 @@ test_that("model_parameters, standardize-refit, wald-t", { ) expect_equal( params$CI_high, - c( - 1.37378, - -0.76856, - -1.14177, - 0.4659, - 2.01759, - -0.06121, - -0.00151 - ), + c(1.37378, -0.76856, -1.14177, 0.4659, 2.01759, -0.06121, -0.00151), tolerance = 1e-3 ) }) @@ -88,32 +64,17 @@ test_that("model_parameters, standardize-refit", { expect_equal(c(nrow(params), ncol(params)), c(7, 10)) expect_equal( params$Coefficient, - c( - 0.96949, - -1.28631, - -1.81461, - 0.34791, - 1.74252, - -0.25421, - -0.18834 - ), + c(0.96949, -1.28631, -1.81461, 0.34791, 1.74252, -0.25421, -0.18834), tolerance = 1e-3 ) - expect_equal(params$SE, + expect_equal( + params$SE, c(0.2045, 0.2619, 0.34035, 0.05968, 0.13914, 0.09762, 0.0945), tolerance = 1e-3 ) expect_equal( params$CI_high, - c( - 1.37378, - -0.76856, - -1.14177, - 0.4659, - 2.01759, - -0.06121, - -0.00151 - ), + c(1.37378, -0.76856, -1.14177, 0.4659, 2.01759, -0.06121, -0.00151), tolerance = 1e-3 ) @@ -132,7 +93,8 @@ test_that("model_parameters, standardize-refit", { }) test_that("model_parameters, standardize-posthoc", { - params <- model_parameters(model, + params <- model_parameters( + model, standardize = "posthoc", verbose = FALSE, effects = "fixed" @@ -143,7 +105,8 @@ test_that("model_parameters, standardize-posthoc", { c(0, 0.49679, -0.49355, 0.34791, 1.74252, -0.25421, -0.18834), tolerance = 1e-3 ) - expect_equal(params$SE, + expect_equal( + params$SE, c(0, 0.66228, 0.70202, 0.05968, 0.13914, 0.09762, 0.0945), tolerance = 1e-3 ) @@ -209,7 +172,8 @@ test_that("model_parameters, standardize-basic", { c(0, 0.23497, -0.23344, 0.34791, 1.74252, -0.77129, -0.61304), tolerance = 1e-3 ) - expect_equal(params$SE, + expect_equal( + params$SE, c(0, 0.31325, 0.33204, 0.05968, 0.13914, 0.2962, 0.30761), tolerance = 1e-3 ) @@ -247,7 +211,8 @@ test_that("model_parameters, standardize-basic", { }) test_that("model_parameters, standardize-basic", { - params <- model_parameters(model, + params <- model_parameters( + model, standardize = "basic", verbose = FALSE, effects = "fixed" @@ -272,15 +237,7 @@ test_that("model_parameters, standardize-refit robust", { expect_equal(c(nrow(params), ncol(params)), c(7, 10)) expect_equal( params$Coefficient, - c( - 0.96949, - -1.28631, - -1.81461, - 0.34791, - 1.74252, - -0.25421, - -0.18834 - ), + c(0.96949, -1.28631, -1.81461, 0.34791, 1.74252, -0.25421, -0.18834), tolerance = 1e-3 ) expect_equal( @@ -290,15 +247,7 @@ test_that("model_parameters, standardize-refit robust", { ) expect_equal( params$CI_high, - c( - 1.12224, - -0.6259, - -1.36691, - 0.45151, - 1.94204, - 0.11227, - -0.07858 - ), + c(1.12224, -0.6259, -1.36691, 0.45151, 1.94204, 0.11227, -0.07858), tolerance = 1e-3 ) }) diff --git a/tests/testthat/test-polr.R b/tests/testthat/test-polr.R index d82ed9d13..6c7055aa6 100644 --- a/tests/testthat/test-polr.R +++ b/tests/testthat/test-polr.R @@ -1,12 +1,17 @@ -skip_if_not_installed("MASS") skip_on_cran() +skip_if_not_installed("MASS") test_that("robust-se polr", { data(housing, package = "MASS") m <- MASS::polr(Sat ~ Infl + Type + Cont, weights = Freq, data = housing) out <- model_parameters(m) expect_identical(attributes(out)$coefficient_name, "Log-Odds") - m <- MASS::polr(Sat ~ Infl + Type + Cont, weights = Freq, data = housing, method = "probit") + m <- MASS::polr( + Sat ~ Infl + Type + Cont, + weights = Freq, + data = housing, + method = "probit" + ) out <- model_parameters(m) expect_identical(attributes(out)$coefficient_name, "Z-Score") }) diff --git a/tests/testthat/test-posterior.R b/tests/testthat/test-posterior.R index cb2c41313..9375816cc 100644 --- a/tests/testthat/test-posterior.R +++ b/tests/testthat/test-posterior.R @@ -1,18 +1,23 @@ +skip_on_cran() + skip_if_not_installed("curl") skip_if_offline() skip_if_not_installed("posterior") skip_if_not_installed("brms") skip_if_not_installed("httr2") -skip_on_cran() - model <- insight::download_model("brms_1") skip_if(is.null(model)) test_that("mp-posterior-draws", { x <- posterior::as_draws(model) mp <- model_parameters(x) - expect_equal(mp$Median, c(39.68234, -3.19505, -1.4936, 2.62881, -79.73344), tolerance = 1e-2, ignore_attr = TRUE) + expect_equal( + mp$Median, + c(39.68234, -3.19505, -1.4936, 2.62881, -79.73344), + tolerance = 1e-2, + ignore_attr = TRUE + ) expect_identical(mp$Parameter, c("b_Intercept", "b_wt", "b_cyl", "sigma", "lp__")) expect_identical(colnames(mp), c("Parameter", "Median", "CI_low", "CI_high", "pd")) }) @@ -20,28 +25,48 @@ test_that("mp-posterior-draws", { test_that("mp-posterior-draws_list", { x <- posterior::as_draws_list(model) mp <- model_parameters(x) - expect_equal(mp$Median, c(39.68234, -3.19505, -1.4936, 2.62881, -79.73344), tolerance = 1e-2, ignore_attr = TRUE) + expect_equal( + mp$Median, + c(39.68234, -3.19505, -1.4936, 2.62881, -79.73344), + tolerance = 1e-2, + ignore_attr = TRUE + ) expect_identical(mp$Parameter, c("b_Intercept", "b_wt", "b_cyl", "sigma", "lp__")) }) test_that("mp-posterior-draws_df", { x <- posterior::as_draws_df(model) mp <- model_parameters(x) - expect_equal(mp$Median, c(39.68234, -3.19505, -1.4936, 2.62881, -79.73344), tolerance = 1e-2, ignore_attr = TRUE) + expect_equal( + mp$Median, + c(39.68234, -3.19505, -1.4936, 2.62881, -79.73344), + tolerance = 1e-2, + ignore_attr = TRUE + ) expect_identical(mp$Parameter, c("b_Intercept", "b_wt", "b_cyl", "sigma", "lp__")) }) test_that("mp-posterior-draws_matrix", { x <- posterior::as_draws_matrix(model) mp <- model_parameters(x) - expect_equal(mp$Median, c(39.68234, -3.19505, -1.4936, 2.62881, -79.73344), tolerance = 1e-2, ignore_attr = TRUE) + expect_equal( + mp$Median, + c(39.68234, -3.19505, -1.4936, 2.62881, -79.73344), + tolerance = 1e-2, + ignore_attr = TRUE + ) expect_identical(mp$Parameter, c("b_Intercept", "b_wt", "b_cyl", "sigma", "lp__")) }) test_that("mp-posterior-draws_array", { x <- posterior::as_draws_array(model) mp <- model_parameters(x) - expect_equal(mp$Median, c(39.68234, -3.19505, -1.4936, 2.62881, -79.73344), tolerance = 1e-2, ignore_attr = TRUE) + expect_equal( + mp$Median, + c(39.68234, -3.19505, -1.4936, 2.62881, -79.73344), + tolerance = 1e-2, + ignore_attr = TRUE + ) expect_identical(mp$Parameter, c("b_Intercept", "b_wt", "b_cyl", "sigma", "lp__")) }) diff --git a/tests/testthat/test-printing-stan.R b/tests/testthat/test-printing-stan.R index f62482733..2b41b16bc 100644 --- a/tests/testthat/test-printing-stan.R +++ b/tests/testthat/test-printing-stan.R @@ -1,72 +1,76 @@ +skip_on_cran() + skip_if_not_installed("curl") skip_if_offline() -skip_on_cran() skip_if_not_installed("brms") skip_if_not_installed("insight") skip_if_not_installed("withr") skip_if_not_installed("rstanarm") skip_if_not_installed("httr2") -withr::with_options( - list(parameters_exponentiate = FALSE), - { - test_that("print brms", { - m1 <- insight::download_model("brms_1") - skip_if(is.null(m1)) - mp1 <- model_parameters(m1, effects = "all", component = "all", centrality = "mean") - expect_snapshot(mp1) +withr::with_options(list(parameters_exponentiate = FALSE), { + test_that("print brms", { + m1 <- insight::download_model("brms_1") + skip_if(is.null(m1)) + mp1 <- model_parameters(m1, effects = "all", component = "all", centrality = "mean") + expect_snapshot(mp1) - m2 <- insight::download_model("brms_mixed_1") - skip_if(is.null(m2)) - mp2 <- model_parameters(m2, effects = "all", component = "all", centrality = "mean") - expect_snapshot(mp2) + m2 <- insight::download_model("brms_mixed_1") + skip_if(is.null(m2)) + mp2 <- model_parameters(m2, effects = "all", component = "all", centrality = "mean") + expect_snapshot(mp2) - m3 <- insight::download_model("brms_mixed_2") - skip_if(is.null(m3)) - mp3 <- model_parameters(m3, effects = "all", component = "all", centrality = "mean") - expect_snapshot(mp3) + m3 <- insight::download_model("brms_mixed_2") + skip_if(is.null(m3)) + mp3 <- model_parameters(m3, effects = "all", component = "all", centrality = "mean") + expect_snapshot(mp3) - m4 <- insight::download_model("brms_mixed_3") - skip_if(is.null(m4)) - mp4 <- model_parameters(m4, effects = "all", component = "all", centrality = "mean") - expect_snapshot(mp4) + m4 <- insight::download_model("brms_mixed_3") + skip_if(is.null(m4)) + mp4 <- model_parameters(m4, effects = "all", component = "all", centrality = "mean") + expect_snapshot(mp4) - m5 <- insight::download_model("brms_mixed_4") - skip_if(is.null(m5)) - mp5 <- model_parameters(m5, effects = "all", component = "all", centrality = "mean") - expect_snapshot(mp5) + m5 <- insight::download_model("brms_mixed_4") + skip_if(is.null(m5)) + mp5 <- model_parameters(m5, effects = "all", component = "all", centrality = "mean") + expect_snapshot(mp5) - m6 <- insight::download_model("brms_mixed_7") - skip_if(is.null(m6)) - mp6 <- model_parameters(m6, effects = "all", component = "all", centrality = "mean") - expect_snapshot(mp6) + m6 <- insight::download_model("brms_mixed_7") + skip_if(is.null(m6)) + mp6 <- model_parameters(m6, effects = "all", component = "all", centrality = "mean") + expect_snapshot(mp6) - m7 <- insight::download_model("brms_zi_1") - skip_if(is.null(m7)) - mp7 <- model_parameters(m7, effects = "all", component = "all", centrality = "mean", verbose = FALSE) - expect_snapshot(mp7) + m7 <- insight::download_model("brms_zi_1") + skip_if(is.null(m7)) + mp7 <- model_parameters( + m7, + effects = "all", + component = "all", + centrality = "mean", + verbose = FALSE + ) + expect_snapshot(mp7) - ## TODO: check why local results differ from snapshot - # m8 <- insight::download_model("brms_zi_3") - # mp8 <- model_parameters(m8, effects = "all", component = "all", centrality = "mean", verbose = FALSE) - # expect_snapshot(mp8) + ## TODO: check why local results differ from snapshot + # m8 <- insight::download_model("brms_zi_3") + # mp8 <- model_parameters(m8, effects = "all", component = "all", centrality = "mean", verbose = FALSE) + # expect_snapshot(mp8) - m9 <- insight::download_model("brms_ordinal_1") - skip_if(is.null(m9)) - mp9 <- model_parameters(m9, effects = "all", component = "all", centrality = "mean") - expect_snapshot(mp9) - }) + m9 <- insight::download_model("brms_ordinal_1") + skip_if(is.null(m9)) + mp9 <- model_parameters(m9, effects = "all", component = "all", centrality = "mean") + expect_snapshot(mp9) + }) - test_that("print-information", { - m <- insight::download_model("brms_1") - skip_if(is.null(m)) - out <- model_parameters(m) - expect_snapshot(out) - out <- model_parameters(m, ci_method = "HDI") - expect_snapshot(out) - m <- insight::download_model("stanreg_glm_1") - skip_if(is.null(m)) - out <- model_parameters(m) - }) - } -) + test_that("print-information", { + m <- insight::download_model("brms_1") + skip_if(is.null(m)) + out <- model_parameters(m) + expect_snapshot(out) + out <- model_parameters(m, ci_method = "HDI") + expect_snapshot(out) + m <- insight::download_model("stanreg_glm_1") + skip_if(is.null(m)) + out <- model_parameters(m) + }) +}) diff --git a/tests/testthat/test-random_effects_ci-glmmTMB.R b/tests/testthat/test-random_effects_ci-glmmTMB.R index 13988150d..0e72aab4e 100644 --- a/tests/testthat/test-random_effects_ci-glmmTMB.R +++ b/tests/testthat/test-random_effects_ci-glmmTMB.R @@ -1,9 +1,10 @@ # Test Setup -------------------------------- +skip_on_cran() + skip_on_os(c("mac", "linux", "solaris")) skip_if_not_installed("glmmTMB", minimum_version = "1.1.12") skip_if_not_installed("lme4") -skip_on_cran() # tests -------------------------------- @@ -21,9 +22,18 @@ m1 <- suppressWarnings(glmmTMB::glmmTMB( data = cake )) m2 <- glmmTMB::glmmTMB(Reaction ~ Days + (Days | Subject), data = sleepstudy) -m3 <- suppressWarnings(glmmTMB::glmmTMB(angle ~ temperature + (temperature | recipe), data = cake)) -m4 <- suppressWarnings(glmmTMB::glmmTMB(angle ~ temperature + (temperature | replicate), data = cake)) -m5 <- suppressWarnings(glmmTMB::glmmTMB(Reaction ~ Days + (Days + Months | Subject), data = sleepstudy)) +m3 <- suppressWarnings(glmmTMB::glmmTMB( + angle ~ temperature + (temperature | recipe), + data = cake +)) +m4 <- suppressWarnings(glmmTMB::glmmTMB( + angle ~ temperature + (temperature | replicate), + data = cake +)) +m5 <- suppressWarnings(glmmTMB::glmmTMB( + Reaction ~ Days + (Days + Months | Subject), + data = sleepstudy +)) set.seed(123) expect_message( @@ -65,26 +75,54 @@ test_that("random effects CIs, two slopes, categorical", { expect_identical( mp1$Parameter, c( - "(Intercept)", "temperature.L", "temperature.Q", "temperature.C", - "temperature^4", "temperature^5", "SD (Intercept)", "SD (Intercept)", - "SD (temperature.L)", "SD (temperature.Q)", "SD (temperature.C)", - "SD (temperature^4)", "SD (temperature^5)", "SD (temperature.L)", - "SD (temperature.Q)", "SD (temperature.C)", "SD (temperature^4)", - "SD (temperature^5)", "Cor (Intercept~temperature.L)", "Cor (Intercept~temperature.Q)", - "Cor (Intercept~temperature.C)", "Cor (Intercept~temperature^4)", - "Cor (Intercept~temperature^5)", "Cor (Intercept~temperature.L)", - "Cor (Intercept~temperature.Q)", "Cor (Intercept~temperature.C)", - "Cor (Intercept~temperature^4)", "Cor (Intercept~temperature^5)", - "Cor (temperature.L~temperature.Q)", "Cor (temperature.L~temperature.C)", - "Cor (temperature.L~temperature^4)", "Cor (temperature.L~temperature^5)", - "Cor (temperature.Q~temperature.C)", "Cor (temperature.Q~temperature^4)", - "Cor (temperature.Q~temperature^5)", "Cor (temperature.C~temperature^4)", - "Cor (temperature.C~temperature^5)", "Cor (temperature^4~temperature^5)", - "Cor (temperature.L~temperature.Q)", "Cor (temperature.L~temperature.C)", - "Cor (temperature.L~temperature^4)", "Cor (temperature.L~temperature^5)", - "Cor (temperature.Q~temperature.C)", "Cor (temperature.Q~temperature^4)", - "Cor (temperature.Q~temperature^5)", "Cor (temperature.C~temperature^4)", - "Cor (temperature.C~temperature^5)", "Cor (temperature^4~temperature^5)", + "(Intercept)", + "temperature.L", + "temperature.Q", + "temperature.C", + "temperature^4", + "temperature^5", + "SD (Intercept)", + "SD (Intercept)", + "SD (temperature.L)", + "SD (temperature.Q)", + "SD (temperature.C)", + "SD (temperature^4)", + "SD (temperature^5)", + "SD (temperature.L)", + "SD (temperature.Q)", + "SD (temperature.C)", + "SD (temperature^4)", + "SD (temperature^5)", + "Cor (Intercept~temperature.L)", + "Cor (Intercept~temperature.Q)", + "Cor (Intercept~temperature.C)", + "Cor (Intercept~temperature^4)", + "Cor (Intercept~temperature^5)", + "Cor (Intercept~temperature.L)", + "Cor (Intercept~temperature.Q)", + "Cor (Intercept~temperature.C)", + "Cor (Intercept~temperature^4)", + "Cor (Intercept~temperature^5)", + "Cor (temperature.L~temperature.Q)", + "Cor (temperature.L~temperature.C)", + "Cor (temperature.L~temperature^4)", + "Cor (temperature.L~temperature^5)", + "Cor (temperature.Q~temperature.C)", + "Cor (temperature.Q~temperature^4)", + "Cor (temperature.Q~temperature^5)", + "Cor (temperature.C~temperature^4)", + "Cor (temperature.C~temperature^5)", + "Cor (temperature^4~temperature^5)", + "Cor (temperature.L~temperature.Q)", + "Cor (temperature.L~temperature.C)", + "Cor (temperature.L~temperature^4)", + "Cor (temperature.L~temperature^5)", + "Cor (temperature.Q~temperature.C)", + "Cor (temperature.Q~temperature^4)", + "Cor (temperature.Q~temperature^5)", + "Cor (temperature.C~temperature^4)", + "Cor (temperature.C~temperature^5)", + "Cor (temperature^4~temperature^5)", "SD (Observations)" ) ) @@ -92,14 +130,55 @@ test_that("random effects CIs, two slopes, categorical", { expect_identical( mp1$Group, c( - "", "", "", "", "", "", "recipe", "replicate", "recipe", "recipe", - "recipe", "recipe", "recipe", "replicate", "replicate", "replicate", - "replicate", "replicate", "recipe", "recipe", "recipe", "recipe", - "recipe", "replicate", "replicate", "replicate", "replicate", - "replicate", "recipe", "recipe", "recipe", "recipe", "recipe", - "recipe", "recipe", "recipe", "recipe", "recipe", "replicate", - "replicate", "replicate", "replicate", "replicate", "replicate", - "replicate", "replicate", "replicate", "replicate", "Residual" + "", + "", + "", + "", + "", + "", + "recipe", + "replicate", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "Residual" ) ) }) @@ -116,7 +195,11 @@ test_that("random effects CIs, simple slope", { expect_identical( mp2$Parameter, c( - "(Intercept)", "Days", "SD (Intercept)", "SD (Days)", "Cor (Intercept~Days)", + "(Intercept)", + "Days", + "SD (Intercept)", + "SD (Days)", + "Cor (Intercept~Days)", "SD (Observations)" ) ) @@ -139,27 +222,68 @@ test_that("random effects CIs, categorical slope-1", { expect_identical( mp3$Parameter, c( - "(Intercept)", "temperature.L", "temperature.Q", "temperature.C", - "temperature^4", "temperature^5", "SD (Intercept)", "SD (temperature.L)", - "SD (temperature.Q)", "SD (temperature.C)", "SD (temperature^4)", - "SD (temperature^5)", "Cor (Intercept~temperature.L)", "Cor (Intercept~temperature.Q)", - "Cor (Intercept~temperature.C)", "Cor (Intercept~temperature^4)", - "Cor (Intercept~temperature^5)", "Cor (temperature.L~temperature.Q)", - "Cor (temperature.L~temperature.C)", "Cor (temperature.L~temperature^4)", - "Cor (temperature.L~temperature^5)", "Cor (temperature.Q~temperature.C)", - "Cor (temperature.Q~temperature^4)", "Cor (temperature.Q~temperature^5)", - "Cor (temperature.C~temperature^4)", "Cor (temperature.C~temperature^5)", - "Cor (temperature^4~temperature^5)", "SD (Observations)" + "(Intercept)", + "temperature.L", + "temperature.Q", + "temperature.C", + "temperature^4", + "temperature^5", + "SD (Intercept)", + "SD (temperature.L)", + "SD (temperature.Q)", + "SD (temperature.C)", + "SD (temperature^4)", + "SD (temperature^5)", + "Cor (Intercept~temperature.L)", + "Cor (Intercept~temperature.Q)", + "Cor (Intercept~temperature.C)", + "Cor (Intercept~temperature^4)", + "Cor (Intercept~temperature^5)", + "Cor (temperature.L~temperature.Q)", + "Cor (temperature.L~temperature.C)", + "Cor (temperature.L~temperature^4)", + "Cor (temperature.L~temperature^5)", + "Cor (temperature.Q~temperature.C)", + "Cor (temperature.Q~temperature^4)", + "Cor (temperature.Q~temperature^5)", + "Cor (temperature.C~temperature^4)", + "Cor (temperature.C~temperature^5)", + "Cor (temperature^4~temperature^5)", + "SD (Observations)" ) ) expect_identical( mp3$Group, c( - "", "", "", "", "", "", "recipe", "recipe", "recipe", "recipe", - "recipe", "recipe", "recipe", "recipe", "recipe", "recipe", "recipe", - "recipe", "recipe", "recipe", "recipe", "recipe", "recipe", "recipe", - "recipe", "recipe", "recipe", "Residual" + "", + "", + "", + "", + "", + "", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "Residual" ) ) }) @@ -183,28 +307,68 @@ test_that("random effects CIs, categorical slope-2", { expect_identical( mp4$Parameter, c( - "(Intercept)", "temperature.L", "temperature.Q", "temperature.C", - "temperature^4", "temperature^5", "SD (Intercept)", "SD (temperature.L)", - "SD (temperature.Q)", "SD (temperature.C)", "SD (temperature^4)", - "SD (temperature^5)", "Cor (Intercept~temperature.L)", "Cor (Intercept~temperature.Q)", - "Cor (Intercept~temperature.C)", "Cor (Intercept~temperature^4)", - "Cor (Intercept~temperature^5)", "Cor (temperature.L~temperature.Q)", - "Cor (temperature.L~temperature.C)", "Cor (temperature.L~temperature^4)", - "Cor (temperature.L~temperature^5)", "Cor (temperature.Q~temperature.C)", - "Cor (temperature.Q~temperature^4)", "Cor (temperature.Q~temperature^5)", - "Cor (temperature.C~temperature^4)", "Cor (temperature.C~temperature^5)", - "Cor (temperature^4~temperature^5)", "SD (Observations)" + "(Intercept)", + "temperature.L", + "temperature.Q", + "temperature.C", + "temperature^4", + "temperature^5", + "SD (Intercept)", + "SD (temperature.L)", + "SD (temperature.Q)", + "SD (temperature.C)", + "SD (temperature^4)", + "SD (temperature^5)", + "Cor (Intercept~temperature.L)", + "Cor (Intercept~temperature.Q)", + "Cor (Intercept~temperature.C)", + "Cor (Intercept~temperature^4)", + "Cor (Intercept~temperature^5)", + "Cor (temperature.L~temperature.Q)", + "Cor (temperature.L~temperature.C)", + "Cor (temperature.L~temperature^4)", + "Cor (temperature.L~temperature^5)", + "Cor (temperature.Q~temperature.C)", + "Cor (temperature.Q~temperature^4)", + "Cor (temperature.Q~temperature^5)", + "Cor (temperature.C~temperature^4)", + "Cor (temperature.C~temperature^5)", + "Cor (temperature^4~temperature^5)", + "SD (Observations)" ) ) expect_identical( mp4$Group, c( - "", "", "", "", "", "", "replicate", "replicate", "replicate", - "replicate", "replicate", "replicate", "replicate", "replicate", - "replicate", "replicate", "replicate", "replicate", "replicate", - "replicate", "replicate", "replicate", "replicate", "replicate", - "replicate", "replicate", "replicate", "Residual" + "", + "", + "", + "", + "", + "", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "Residual" ) ) }) @@ -221,9 +385,15 @@ test_that("random effects CIs, double slope", { expect_identical( mp5$Parameter, c( - "(Intercept)", "Days", "SD (Intercept)", "SD (Days)", "SD (Months)", - "Cor (Intercept~Days)", "Cor (Intercept~Months)", - "Cor (Days~Months)", "SD (Observations)" + "(Intercept)", + "Days", + "SD (Intercept)", + "SD (Days)", + "SD (Months)", + "Cor (Intercept~Days)", + "Cor (Intercept~Months)", + "Cor (Days~Months)", + "SD (Observations)" ) ) }) @@ -236,7 +406,10 @@ test_that("random effects CIs, simple slope", { set.seed(123) m2 <- glmmTMB::glmmTMB(Reaction ~ Days + (0 + Days | Subject), data = sleepstudy) - m5 <- suppressWarnings(glmmTMB::glmmTMB(Reaction ~ Days + (0 + Days + Months | Subject), data = sleepstudy)) + m5 <- suppressWarnings(glmmTMB::glmmTMB( + Reaction ~ Days + (0 + Days + Months | Subject), + data = sleepstudy + )) set.seed(123) mp2 <- model_parameters(m2, ci_random = TRUE) @@ -269,7 +442,11 @@ test_that("random effects CIs, simple slope", { expect_identical( mp5$Parameter, c( - "(Intercept)", "Days", "SD (Days)", "SD (Months)", "Cor (Days~Months)", + "(Intercept)", + "Days", + "SD (Days)", + "SD (Months)", + "Cor (Days~Months)", "SD (Observations)" ) ) @@ -279,5 +456,8 @@ test_that("random effects CIs, simple slope", { # messages for profiled CI test_that("profiled CI messages", { mp2 <- model_parameters(m2, ci_method = "profile") - expect_message(utils::capture.output(print(mp2)), regexp = "(.*)profile-likelihood(.*)z-distribution(.*)") + expect_message( + utils::capture.output(print(mp2)), + regexp = "(.*)profile-likelihood(.*)z-distribution(.*)" + ) }) diff --git a/tests/testthat/test-random_effects_ci.R b/tests/testthat/test-random_effects_ci.R index 5b5a78492..d0dc559c4 100644 --- a/tests/testthat/test-random_effects_ci.R +++ b/tests/testthat/test-random_effects_ci.R @@ -1,8 +1,7 @@ -skip_on_os("mac") -skip_if_not_installed("lme4") - skip_on_cran() +skip_on_os("mac") +skip_if_not_installed("lme4") data(sleepstudy, package = "lme4") data(cake, package = "lme4") @@ -14,9 +13,18 @@ m1 <- suppressMessages(lme4::lmer( data = cake )) m2 <- suppressMessages(lme4::lmer(Reaction ~ Days + (Days | Subject), data = sleepstudy)) -m3 <- suppressMessages(lme4::lmer(angle ~ temperature + (temperature | recipe), data = cake)) -m4 <- suppressMessages(lme4::lmer(angle ~ temperature + (temperature | replicate), data = cake)) -m5 <- suppressMessages(lme4::lmer(Reaction ~ Days + (Days + Months | Subject), data = sleepstudy)) +m3 <- suppressMessages(lme4::lmer( + angle ~ temperature + (temperature | recipe), + data = cake +)) +m4 <- suppressMessages(lme4::lmer( + angle ~ temperature + (temperature | replicate), + data = cake +)) +m5 <- suppressMessages(lme4::lmer( + Reaction ~ Days + (Days + Months | Subject), + data = sleepstudy +)) ## TODO also check messages for profiled CI @@ -53,12 +61,55 @@ test_that("random effects CIs, two slopes, categorical", { expect_equal( mp1$CI_low, c( - 28.75568, 4.97893, -1.95002, -2.69995, -3.62201, -2.69102, - 4.28558, 0.21474, 0.40062, 0.10169, 0.04953, 1e-05, 0.55398, - 0, 2e-05, 0.6333, 1.09851, 0.00944, -0.65406, -0.69103, -1, -0.95271, - -0.90617, -1, -1, -1, -1, -1, -1, -0.99802, -1, -0.75274, -0.99836, - -1, -0.96895, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, 4.07985 + 28.75568, + 4.97893, + -1.95002, + -2.69995, + -3.62201, + -2.69102, + 4.28558, + 0.21474, + 0.40062, + 0.10169, + 0.04953, + 1e-05, + 0.55398, + 0, + 2e-05, + 0.6333, + 1.09851, + 0.00944, + -0.65406, + -0.69103, + -1, + -0.95271, + -0.90617, + -1, + -1, + -1, + -1, + -1, + -1, + -0.99802, + -1, + -0.75274, + -0.99836, + -1, + -0.96895, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + 4.07985 ), tolerance = 1e-3, ignore_attr = TRUE @@ -67,26 +118,54 @@ test_that("random effects CIs, two slopes, categorical", { expect_identical( mp1$Parameter, c( - "(Intercept)", "temperature.L", "temperature.Q", "temperature.C", - "temperature^4", "temperature^5", "SD (Intercept)", "SD (Intercept)", - "SD (temperature.L)", "SD (temperature.Q)", "SD (temperature.C)", - "SD (temperature^4)", "SD (temperature^5)", "SD (temperature.L)", - "SD (temperature.Q)", "SD (temperature.C)", "SD (temperature^4)", - "SD (temperature^5)", "Cor (Intercept~temperature.L)", "Cor (Intercept~temperature.Q)", - "Cor (Intercept~temperature.C)", "Cor (Intercept~temperature^4)", - "Cor (Intercept~temperature^5)", "Cor (Intercept~temperature.L)", - "Cor (Intercept~temperature.Q)", "Cor (Intercept~temperature.C)", - "Cor (Intercept~temperature^4)", "Cor (Intercept~temperature^5)", - "Cor (temperature.L~temperature.Q)", "Cor (temperature.L~temperature.C)", - "Cor (temperature.L~temperature^4)", "Cor (temperature.L~temperature^5)", - "Cor (temperature.Q~temperature.C)", "Cor (temperature.Q~temperature^4)", - "Cor (temperature.Q~temperature^5)", "Cor (temperature.C~temperature^4)", - "Cor (temperature.C~temperature^5)", "Cor (temperature^4~temperature^5)", - "Cor (temperature.L~temperature.Q)", "Cor (temperature.L~temperature.C)", - "Cor (temperature.L~temperature^4)", "Cor (temperature.L~temperature^5)", - "Cor (temperature.Q~temperature.C)", "Cor (temperature.Q~temperature^4)", - "Cor (temperature.Q~temperature^5)", "Cor (temperature.C~temperature^4)", - "Cor (temperature.C~temperature^5)", "Cor (temperature^4~temperature^5)", + "(Intercept)", + "temperature.L", + "temperature.Q", + "temperature.C", + "temperature^4", + "temperature^5", + "SD (Intercept)", + "SD (Intercept)", + "SD (temperature.L)", + "SD (temperature.Q)", + "SD (temperature.C)", + "SD (temperature^4)", + "SD (temperature^5)", + "SD (temperature.L)", + "SD (temperature.Q)", + "SD (temperature.C)", + "SD (temperature^4)", + "SD (temperature^5)", + "Cor (Intercept~temperature.L)", + "Cor (Intercept~temperature.Q)", + "Cor (Intercept~temperature.C)", + "Cor (Intercept~temperature^4)", + "Cor (Intercept~temperature^5)", + "Cor (Intercept~temperature.L)", + "Cor (Intercept~temperature.Q)", + "Cor (Intercept~temperature.C)", + "Cor (Intercept~temperature^4)", + "Cor (Intercept~temperature^5)", + "Cor (temperature.L~temperature.Q)", + "Cor (temperature.L~temperature.C)", + "Cor (temperature.L~temperature^4)", + "Cor (temperature.L~temperature^5)", + "Cor (temperature.Q~temperature.C)", + "Cor (temperature.Q~temperature^4)", + "Cor (temperature.Q~temperature^5)", + "Cor (temperature.C~temperature^4)", + "Cor (temperature.C~temperature^5)", + "Cor (temperature^4~temperature^5)", + "Cor (temperature.L~temperature.Q)", + "Cor (temperature.L~temperature.C)", + "Cor (temperature.L~temperature^4)", + "Cor (temperature.L~temperature^5)", + "Cor (temperature.Q~temperature.C)", + "Cor (temperature.Q~temperature^4)", + "Cor (temperature.Q~temperature^5)", + "Cor (temperature.C~temperature^4)", + "Cor (temperature.C~temperature^5)", + "Cor (temperature^4~temperature^5)", "SD (Observations)" ) ) @@ -94,14 +173,55 @@ test_that("random effects CIs, two slopes, categorical", { expect_identical( mp1$Group, c( - "", "", "", "", "", "", "replicate", "recipe", "replicate", - "replicate", "replicate", "replicate", "replicate", "recipe", - "recipe", "recipe", "recipe", "recipe", "replicate", "replicate", - "replicate", "replicate", "replicate", "recipe", "recipe", "recipe", - "recipe", "recipe", "replicate", "replicate", "replicate", "replicate", - "replicate", "replicate", "replicate", "replicate", "replicate", - "replicate", "recipe", "recipe", "recipe", "recipe", "recipe", - "recipe", "recipe", "recipe", "recipe", "recipe", "Residual" + "", + "", + "", + "", + "", + "", + "replicate", + "recipe", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "Residual" ) ) }) @@ -120,15 +240,16 @@ test_that("random effects CIs, simple slope", { expect_identical( mp2$Parameter, c( - "(Intercept)", "Days", "SD (Intercept)", "SD (Days)", "Cor (Intercept~Days)", + "(Intercept)", + "Days", + "SD (Intercept)", + "SD (Days)", + "Cor (Intercept~Days)", "SD (Observations)" ) ) - expect_identical( - mp2$Group, - c("", "", "Subject", "Subject", "Subject", "Residual") - ) + expect_identical(mp2$Group, c("", "", "Subject", "Subject", "Subject", "Residual")) }) @@ -145,8 +266,18 @@ test_that("random effects CIs, categorical slope-1", { expect_equal( mp3$CI_low[1:12], c( - 30.91139, 4.33247, -2.6798, -3.20703, -4.07681, -3.27237, 0.06301, - 0, 0, 0.1192, 0.32213, 0 + 30.91139, + 4.33247, + -2.6798, + -3.20703, + -4.07681, + -3.27237, + 0.06301, + 0, + 0, + 0.1192, + 0.32213, + 0 ), tolerance = 1e-2, ignore_attr = TRUE @@ -154,27 +285,68 @@ test_that("random effects CIs, categorical slope-1", { expect_identical( mp3$Parameter, c( - "(Intercept)", "temperature.L", "temperature.Q", "temperature.C", - "temperature^4", "temperature^5", "SD (Intercept)", "SD (temperature.L)", - "SD (temperature.Q)", "SD (temperature.C)", "SD (temperature^4)", - "SD (temperature^5)", "Cor (Intercept~temperature.L)", "Cor (Intercept~temperature.Q)", - "Cor (Intercept~temperature.C)", "Cor (Intercept~temperature^4)", - "Cor (Intercept~temperature^5)", "Cor (temperature.L~temperature.Q)", - "Cor (temperature.L~temperature.C)", "Cor (temperature.L~temperature^4)", - "Cor (temperature.L~temperature^5)", "Cor (temperature.Q~temperature.C)", - "Cor (temperature.Q~temperature^4)", "Cor (temperature.Q~temperature^5)", - "Cor (temperature.C~temperature^4)", "Cor (temperature.C~temperature^5)", - "Cor (temperature^4~temperature^5)", "SD (Observations)" + "(Intercept)", + "temperature.L", + "temperature.Q", + "temperature.C", + "temperature^4", + "temperature^5", + "SD (Intercept)", + "SD (temperature.L)", + "SD (temperature.Q)", + "SD (temperature.C)", + "SD (temperature^4)", + "SD (temperature^5)", + "Cor (Intercept~temperature.L)", + "Cor (Intercept~temperature.Q)", + "Cor (Intercept~temperature.C)", + "Cor (Intercept~temperature^4)", + "Cor (Intercept~temperature^5)", + "Cor (temperature.L~temperature.Q)", + "Cor (temperature.L~temperature.C)", + "Cor (temperature.L~temperature^4)", + "Cor (temperature.L~temperature^5)", + "Cor (temperature.Q~temperature.C)", + "Cor (temperature.Q~temperature^4)", + "Cor (temperature.Q~temperature^5)", + "Cor (temperature.C~temperature^4)", + "Cor (temperature.C~temperature^5)", + "Cor (temperature^4~temperature^5)", + "SD (Observations)" ) ) expect_identical( mp3$Group, c( - "", "", "", "", "", "", "recipe", "recipe", "recipe", "recipe", - "recipe", "recipe", "recipe", "recipe", "recipe", "recipe", "recipe", - "recipe", "recipe", "recipe", "recipe", "recipe", "recipe", "recipe", - "recipe", "recipe", "recipe", "Residual" + "", + "", + "", + "", + "", + "", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "Residual" ) ) }) @@ -186,10 +358,34 @@ test_that("random effects CIs, categorical slope-2", { expect_equal( mp4$CI_low, c( - 28.88523, 4.96796, -1.93239, -1.98597, -2.68858, -2.5524, 4.27899, - 0.35378, 0.08109, 0.03419, 0, 0.49982, -0.68893, -0.71984, -1, - -0.96725, -0.92158, -1, -0.99894, -1, -0.80378, -0.99924, -1, - -0.9778, -1, -1, -1, 4.21143 + 28.88523, + 4.96796, + -1.93239, + -1.98597, + -2.68858, + -2.5524, + 4.27899, + 0.35378, + 0.08109, + 0.03419, + 0, + 0.49982, + -0.68893, + -0.71984, + -1, + -0.96725, + -0.92158, + -1, + -0.99894, + -1, + -0.80378, + -0.99924, + -1, + -0.9778, + -1, + -1, + -1, + 4.21143 ), tolerance = 1e-3, ignore_attr = TRUE @@ -198,28 +394,68 @@ test_that("random effects CIs, categorical slope-2", { expect_identical( mp4$Parameter, c( - "(Intercept)", "temperature.L", "temperature.Q", "temperature.C", - "temperature^4", "temperature^5", "SD (Intercept)", "SD (temperature.L)", - "SD (temperature.Q)", "SD (temperature.C)", "SD (temperature^4)", - "SD (temperature^5)", "Cor (Intercept~temperature.L)", "Cor (Intercept~temperature.Q)", - "Cor (Intercept~temperature.C)", "Cor (Intercept~temperature^4)", - "Cor (Intercept~temperature^5)", "Cor (temperature.L~temperature.Q)", - "Cor (temperature.L~temperature.C)", "Cor (temperature.L~temperature^4)", - "Cor (temperature.L~temperature^5)", "Cor (temperature.Q~temperature.C)", - "Cor (temperature.Q~temperature^4)", "Cor (temperature.Q~temperature^5)", - "Cor (temperature.C~temperature^4)", "Cor (temperature.C~temperature^5)", - "Cor (temperature^4~temperature^5)", "SD (Observations)" + "(Intercept)", + "temperature.L", + "temperature.Q", + "temperature.C", + "temperature^4", + "temperature^5", + "SD (Intercept)", + "SD (temperature.L)", + "SD (temperature.Q)", + "SD (temperature.C)", + "SD (temperature^4)", + "SD (temperature^5)", + "Cor (Intercept~temperature.L)", + "Cor (Intercept~temperature.Q)", + "Cor (Intercept~temperature.C)", + "Cor (Intercept~temperature^4)", + "Cor (Intercept~temperature^5)", + "Cor (temperature.L~temperature.Q)", + "Cor (temperature.L~temperature.C)", + "Cor (temperature.L~temperature^4)", + "Cor (temperature.L~temperature^5)", + "Cor (temperature.Q~temperature.C)", + "Cor (temperature.Q~temperature^4)", + "Cor (temperature.Q~temperature^5)", + "Cor (temperature.C~temperature^4)", + "Cor (temperature.C~temperature^5)", + "Cor (temperature^4~temperature^5)", + "SD (Observations)" ) ) expect_identical( mp4$Group, c( - "", "", "", "", "", "", "replicate", "replicate", "replicate", - "replicate", "replicate", "replicate", "replicate", "replicate", - "replicate", "replicate", "replicate", "replicate", "replicate", - "replicate", "replicate", "replicate", "replicate", "replicate", - "replicate", "replicate", "replicate", "Residual" + "", + "", + "", + "", + "", + "", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "replicate", + "Residual" ) ) }) @@ -238,17 +474,30 @@ test_that("random effects CIs, double slope", { expect_identical( mp5$Parameter, c( - "(Intercept)", "Days", "SD (Intercept)", "SD (Days)", "SD (Months)", - "Cor (Intercept~Days)", "Cor (Intercept~Months)", - "Cor (Days~Months)", "SD (Observations)" + "(Intercept)", + "Days", + "SD (Intercept)", + "SD (Days)", + "SD (Months)", + "Cor (Intercept~Days)", + "Cor (Intercept~Months)", + "Cor (Days~Months)", + "SD (Observations)" ) ) expect_identical( mp5$Group, c( - "", "", "Subject", "Subject", "Subject", "Subject", "Subject", - "Subject", "Residual" + "", + "", + "Subject", + "Subject", + "Subject", + "Subject", + "Subject", + "Subject", + "Residual" ) ) }) @@ -287,7 +536,11 @@ test_that("random effects CIs, simple slope", { expect_identical( mp5$Parameter, c( - "(Intercept)", "Days", "SD (Days)", "SD (Months)", "Cor (Days~Months)", + "(Intercept)", + "Days", + "SD (Days)", + "SD (Months)", + "Cor (Days~Months)", "SD (Observations)" ) ) @@ -298,15 +551,27 @@ test_that("random effects CIs, simple slope", { test_that("random effects CIs, poly slope", { data(cake, package = "lme4") suppressMessages({ - m <- lme4::lmer(angle ~ poly(temp, 2) + (poly(temp, 2) | replicate) + (1 | recipe), data = cake) + m <- lme4::lmer( + angle ~ poly(temp, 2) + (poly(temp, 2) | replicate) + (1 | recipe), + data = cake + ) }) mp <- model_parameters(m, ci_random = TRUE) expect_equal( mp$CI_low, c( - 28.7884, 33.56318, -12.84259, 4.27435, 0.16222, 7.78988, 0.87668, - -0.8172, -1, -1, 4.32855 + 28.7884, + 33.56318, + -12.84259, + 4.27435, + 0.16222, + 7.78988, + 0.87668, + -0.8172, + -1, + -1, + 4.32855 ), tolerance = 1e-3, ignore_attr = TRUE @@ -315,10 +580,17 @@ test_that("random effects CIs, poly slope", { expect_identical( mp$Parameter, c( - "(Intercept)", "poly(temp, 2)1", "poly(temp, 2)2", "SD (Intercept)", - "SD (Intercept)", "SD (poly(temp, 2)1)", "SD (poly(temp, 2)2)", - "Cor (Intercept~poly(temp, 2)1)", "Cor (Intercept~poly(temp, 2)2)", - "Cor (poly(temp, 2)1~poly(temp, 2)2)", "SD (Observations)" + "(Intercept)", + "poly(temp, 2)1", + "poly(temp, 2)2", + "SD (Intercept)", + "SD (Intercept)", + "SD (poly(temp, 2)1)", + "SD (poly(temp, 2)2)", + "Cor (Intercept~poly(temp, 2)1)", + "Cor (Intercept~poly(temp, 2)2)", + "Cor (poly(temp, 2)1~poly(temp, 2)2)", + "SD (Observations)" ) ) }) @@ -330,7 +602,8 @@ test_that("random effects CIs, poly categorical slope", { ## NOTE check back every now and then and see if tests still work skip("works interactively") - m <- lme4::lmer(angle ~ poly(temp, 2) + (poly(temp, 2) | replicate) + (temperature | recipe), + m <- lme4::lmer( + angle ~ poly(temp, 2) + (poly(temp, 2) | replicate) + (temperature | recipe), data = cake ) mp <- model_parameters(m, effects = "random") @@ -338,9 +611,34 @@ test_that("random effects CIs, poly categorical slope", { expect_equal( mp$CI_low, c( - 4.27846, 0.22005, 8.22659, 1.17579, 0, 5e-05, 0.37736, 1.24258, - 0, -0.77207, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, 4.22056 + 4.27846, + 0.22005, + 8.22659, + 1.17579, + 0, + 5e-05, + 0.37736, + 1.24258, + 0, + -0.77207, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + -1, + 4.22056 ), tolerance = 1e-3, ignore_attr = TRUE @@ -349,18 +647,33 @@ test_that("random effects CIs, poly categorical slope", { expect_identical( mp$Parameter, c( - "SD (Intercept)", "SD (Intercept)", "SD (poly(temp, 2)1)", - "SD (poly(temp, 2)2)", "SD (temperature.L)", "SD (temperature.Q)", - "SD (temperature.C)", "SD (temperature^4)", "SD (temperature^5)", - "Cor (Intercept~poly(temp, 2)1)", "Cor (Intercept~poly(temp, 2)2)", - "Cor (Intercept~temperature.L)", "Cor (Intercept~temperature.Q)", - "Cor (Intercept~temperature.C)", "Cor (Intercept~temperature^4)", - "Cor (Intercept~temperature^5)", "Cor (poly(temp, 2)1~poly(temp, 2)2)", - "Cor (temperature.L~temperature.Q)", "Cor (temperature.L~temperature.C)", - "Cor (temperature.L~temperature^4)", "Cor (temperature.L~temperature^5)", - "Cor (temperature.Q~temperature.C)", "Cor (temperature.Q~temperature^4)", - "Cor (temperature.Q~temperature^5)", "Cor (temperature.C~temperature^4)", - "Cor (temperature.C~temperature^5)", "Cor (temperature^4~temperature^5)", + "SD (Intercept)", + "SD (Intercept)", + "SD (poly(temp, 2)1)", + "SD (poly(temp, 2)2)", + "SD (temperature.L)", + "SD (temperature.Q)", + "SD (temperature.C)", + "SD (temperature^4)", + "SD (temperature^5)", + "Cor (Intercept~poly(temp, 2)1)", + "Cor (Intercept~poly(temp, 2)2)", + "Cor (Intercept~temperature.L)", + "Cor (Intercept~temperature.Q)", + "Cor (Intercept~temperature.C)", + "Cor (Intercept~temperature^4)", + "Cor (Intercept~temperature^5)", + "Cor (poly(temp, 2)1~poly(temp, 2)2)", + "Cor (temperature.L~temperature.Q)", + "Cor (temperature.L~temperature.C)", + "Cor (temperature.L~temperature^4)", + "Cor (temperature.L~temperature^5)", + "Cor (temperature.Q~temperature.C)", + "Cor (temperature.Q~temperature^4)", + "Cor (temperature.Q~temperature^5)", + "Cor (temperature.C~temperature^4)", + "Cor (temperature.C~temperature^5)", + "Cor (temperature^4~temperature^5)", "SD (Observations)" ) ) @@ -368,11 +681,34 @@ test_that("random effects CIs, poly categorical slope", { expect_identical( mp$Group, c( - "replicate", "recipe", "replicate", "replicate", "recipe", - "recipe", "recipe", "recipe", "recipe", "replicate", "replicate", - "recipe", "recipe", "recipe", "recipe", "recipe", "replicate", - "recipe", "recipe", "recipe", "recipe", "recipe", "recipe", "recipe", - "recipe", "recipe", "recipe", "Residual" + "replicate", + "recipe", + "replicate", + "replicate", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "replicate", + "replicate", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "replicate", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "recipe", + "Residual" ) ) }) diff --git a/tests/testthat/test-robust.R b/tests/testthat/test-robust.R index 971c67beb..8d718d92f 100644 --- a/tests/testthat/test-robust.R +++ b/tests/testthat/test-robust.R @@ -1,13 +1,10 @@ -skip_if_not_installed("sandwich") skip_on_cran() +skip_if_not_installed("sandwich") # standard errors ------------------------------------- test_that("robust-se glm warn with profile-CI", { mglm <- glm(mpg ~ wt, data = mtcars) - expect_message( - ci(mglm, vcov = "HC3"), - regex = "available" - ) + expect_message(ci(mglm, vcov = "HC3"), regex = "available") expect_message( model_parameters(mglm, vcov = "HC3", ci_method = "profile"), regex = "modifies" @@ -200,7 +197,6 @@ test_that("robust-p survival", { # CI ------------------------------------- - test_that("robust-ci lm", { data(iris) m <- lm(Petal.Length ~ Sepal.Length * Species, data = iris) @@ -266,10 +262,7 @@ test_that("robust-ci ivreg", { se <- sqrt(diag(sandwich::vcovCL(m))) dof <- degrees_of_freedom(m, method = "wald", verbose = FALSE) fac <- suppressWarnings(stats::qt(0.975, df = dof)) - ci2 <- as.data.frame(cbind( - CI_low = coef(m) - se * fac, - CI_high = coef(m) + se * fac - )) + ci2 <- as.data.frame(cbind(CI_low = coef(m) - se * fac, CI_high = coef(m) + se * fac)) expect_equal(ci1$CI_low, ci2$CI_low, tolerance = 1e-4, ignore_attr = TRUE) expect_equal(ci1$CI_high, ci2$CI_high, tolerance = 1e-4, ignore_attr = TRUE) @@ -278,10 +271,7 @@ test_that("robust-ci ivreg", { se <- sqrt(diag(sandwich::vcovOPG(m))) dof <- degrees_of_freedom(m, method = "wald", verbose = FALSE) fac <- suppressWarnings(stats::qt(0.975, df = dof)) - ci2 <- as.data.frame(cbind( - CI_low = coef(m) - se * fac, - CI_high = coef(m) + se * fac - )) + ci2 <- as.data.frame(cbind(CI_low = coef(m) - se * fac, CI_high = coef(m) + se * fac)) expect_equal(ci1$CI_low, ci2$CI_low, tolerance = 1e-4, ignore_attr = TRUE) expect_equal(ci1$CI_high, ci2$CI_high, tolerance = 1e-4, ignore_attr = TRUE) }) @@ -296,10 +286,7 @@ test_that("robust-ci zeroinfl", { se <- sqrt(diag(sandwich::vcovCL(m))) dof <- degrees_of_freedom(m, method = "wald", verbose = FALSE) fac <- suppressWarnings(stats::qt(0.975, df = dof)) - ci2 <- as.data.frame(cbind( - CI_low = coef(m) - se * fac, - CI_high = coef(m) + se * fac - )) + ci2 <- as.data.frame(cbind(CI_low = coef(m) - se * fac, CI_high = coef(m) + se * fac)) expect_equal(ci1$CI_low, ci2$CI_low, tolerance = 1e-4, ignore_attr = TRUE) expect_equal(ci1$CI_high, ci2$CI_high, tolerance = 1e-4, ignore_attr = TRUE) @@ -308,10 +295,7 @@ test_that("robust-ci zeroinfl", { se <- sqrt(diag(sandwich::vcovOPG(m))) dof <- degrees_of_freedom(m, method = "wald", verbose = FALSE) fac <- suppressWarnings(stats::qt(0.975, df = dof)) - ci2 <- as.data.frame(cbind( - CI_low = coef(m) - se * fac, - CI_high = coef(m) + se * fac - )) + ci2 <- as.data.frame(cbind(CI_low = coef(m) - se * fac, CI_high = coef(m) + se * fac)) expect_equal(ci1$CI_low, ci2$CI_low, tolerance = 1e-4, ignore_attr = TRUE) expect_equal(ci1$CI_high, ci2$CI_high, tolerance = 1e-4, ignore_attr = TRUE) }) @@ -354,7 +338,11 @@ test_that("robust-se lmer", { Sepal.Length ~ Species * Sepal.Width + Petal.Length + (1 | grp), data = iris ) - se1 <- standard_error(m, vcov = "vcovCR", vcov_args = list(type = "CR1", cluster = iris$grp)) + se1 <- standard_error( + m, + vcov = "vcovCR", + vcov_args = list(type = "CR1", cluster = iris$grp) + ) se2 <- sqrt(diag(clubSandwich::vcovCR(m, type = "CR1", cluster = iris$grp))) expect_equal(se1$SE, se2, tolerance = 1e-4, ignore_attr = TRUE) }) diff --git a/tests/testthat/test-svyolr.R b/tests/testthat/test-svyolr.R index 5de420a8c..00ab79b5c 100644 --- a/tests/testthat/test-svyolr.R +++ b/tests/testthat/test-svyolr.R @@ -1,6 +1,7 @@ +skip_on_cran() + skip_if_not_installed("MASS") skip_if_not_installed("survey") -skip_on_cran() test_that("robust-se polr", { data(api, package = "survey") From a05cc0b7cb45ae1171ab4bc061531eaf92cbb968 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 25 Nov 2025 08:04:43 +0100 Subject: [PATCH 5/5] skip test on CRAN --- tests/testthat/test-Hmisc.R | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/testthat/test-Hmisc.R b/tests/testthat/test-Hmisc.R index 447efbd57..6c74d380f 100644 --- a/tests/testthat/test-Hmisc.R +++ b/tests/testthat/test-Hmisc.R @@ -1,3 +1,5 @@ +skip_on_cran() + test_that("issue 697", { skip_if_not_installed("Hmisc") skip_if_not_installed("rms")