From fd780f27fa90216f9c5c9e1d9cef985e059f8a3f Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 24 Jul 2024 11:55:26 +0200 Subject: [PATCH] `get_variance_distribution()` returns `NULL` for `brms` negative binomial model (#911) * `get_variance_distribution()` returns `NULL` for `brms` negative binomial model Fixes #776 * add test * detect panelr as mixed model * add test * fix test * fix test * test --- DESCRIPTION | 2 +- NAMESPACE | 2 ++ NEWS.md | 2 ++ R/compute_variances.R | 8 ++++++++ R/is_mixed_model.R | 8 ++++++++ tests/testthat/test-get_variance.R | 10 ++++++++++ tests/testthat/test-panelr.R | 5 +++++ 7 files changed, 36 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index d7ff0ae54..187f66def 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: insight Title: Easy Access to Model Information for Various Model Objects -Version: 0.20.2.9 +Version: 0.20.2.10 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/NAMESPACE b/NAMESPACE index f0f42c97c..c328b677a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1048,6 +1048,8 @@ S3method(is_converged,merMod) S3method(is_mixed_model,afex_aov) S3method(is_mixed_model,default) S3method(is_mixed_model,marginaleffects) +S3method(is_mixed_model,wbgee) +S3method(is_mixed_model,wbm) S3method(is_nullmodel,afex_aov) S3method(is_nullmodel,default) S3method(link_function,BBmm) diff --git a/NEWS.md b/NEWS.md index ebf59ee22..d5905da03 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,6 +4,8 @@ * `get_df()` now supports more model classes. +* `get_variance()` gives an informative error if no mixed model is provided. + ## Bug fixes * Fixed issues in `find_response()` for *brms* models with `mi()` function in diff --git a/R/compute_variances.R b/R/compute_variances.R index 82f2d0bf4..c0e21f454 100644 --- a/R/compute_variances.R +++ b/R/compute_variances.R @@ -14,6 +14,14 @@ ## Revisions and adaption to more complex models and other packages ## by Daniel Lüdecke + # sanity check - only proceed for mixed models + if (!is_mixed_model(model)) { + if (verbose) { + format_warning("This function only works for mixed models, i.e. models with random effects.") + } + return(NULL) + } + # needed for singularity check check_if_installed("performance", reason = "to check for singularity") diff --git a/R/is_mixed_model.R b/R/is_mixed_model.R index e3d9cb3d6..c3df0bb0a 100644 --- a/R/is_mixed_model.R +++ b/R/is_mixed_model.R @@ -35,3 +35,11 @@ is_mixed_model.afex_aov <- function(x) { is_mixed_model.marginaleffects <- function(x) { is_mixed_model(attributes(x)$model) } + +#' @export +is_mixed_model.wbm <- function(x) { + TRUE +} + +#' @export +is_mixed_model.wbgee <- is_mixed_model.wbm diff --git a/tests/testthat/test-get_variance.R b/tests/testthat/test-get_variance.R index 984b632ac..0014f0e43 100644 --- a/tests/testthat/test-get_variance.R +++ b/tests/testthat/test-get_variance.R @@ -35,6 +35,16 @@ v4 <- suppressWarnings(get_variance(fm4)) v5 <- suppressWarnings(get_variance(fm5)) v6 <- suppressWarnings(get_variance(fm6)) +test_that("error for non-mixed", { + skip_if_not_installed("glmmTMB") + data(mtcars) + expect_warning( + get_variance(glmmTMB::glmmTMB(mpg ~ gear, data = mtcars)), + regex = "This function only works for mixed models" + ) + expect_silent(get_variance(glmmTMB::glmmTMB(mpg ~ gear, data = mtcars), verbose = FALSE)) +}) + test_that("get_variance-1", { expect_equal(v1$var.intercept, c(Subject = 612.10016), diff --git a/tests/testthat/test-panelr.R b/tests/testthat/test-panelr.R index 637b54e69..c03a9969e 100644 --- a/tests/testthat/test-panelr.R +++ b/tests/testthat/test-panelr.R @@ -10,6 +10,11 @@ test_that("model_info", { expect_true(model_info(m2)$is_linear) }) +test_that("id_mixed", { + expect_true(is_mixed_model(m1)) + expect_true(is_mixed_model(m2)) +}) + test_that("find_predictors", { expect_identical( find_predictors(m1),