Skip to content

Commit

Permalink
get_variance_distribution() returns NULL for brms negative bino…
Browse files Browse the repository at this point in the history
…mial 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
  • Loading branch information
strengejacke authored Jul 24, 2024
1 parent 52213c1 commit fd780f2
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions R/compute_variances.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
8 changes: 8 additions & 0 deletions R/is_mixed_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions tests/testthat/test-get_variance.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-panelr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit fd780f2

Please sign in to comment.