Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_variance_distribution() returns NULL for brms negative binomial model #911

Merged
merged 8 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -1,4 +1,4 @@
.compute_variances <- function(model,

Check warning on line 1 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/compute_variances.R,line=1,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 54 to at most 40.

Check warning on line 1 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/compute_variances.R,line=1,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 54 to at most 40.
component,
name_fun = NULL,
name_full = NULL,
Expand All @@ -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 Expand Up @@ -497,7 +505,7 @@
# fixed effects variance ------------------------------------------------------
#
# This is in line with Nakagawa et al. 2017, Suppl. 2
# see https://royalsocietypublishing.org/action/downloadSupplement?doi=10.1098%2Frsif.2017.0213&file=rsif20170213supp2.pdf

Check warning on line 508 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/compute_variances.R,line=508,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.

Check warning on line 508 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/compute_variances.R,line=508,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.
#
# However, package MuMIn differs and uses "fitted()" instead, leading to minor
# deviations
Expand Down Expand Up @@ -525,7 +533,7 @@
# variance associated with a random-effects term (Johnson 2014) --------------
#
# This is in line with Nakagawa et al. 2017, Suppl. 2, and package MuMIm
# see https://royalsocietypublishing.org/action/downloadSupplement?doi=10.1098%2Frsif.2017.0213&file=rsif20170213supp2.pdf

Check warning on line 536 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/compute_variances.R,line=536,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.

Check warning on line 536 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/compute_variances.R,line=536,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.
# ----------------------------------------------------------------------------
.compute_variance_random <- function(model, terms, mixed_effects_info) {
if (is.null(terms)) {
Expand Down Expand Up @@ -566,7 +574,7 @@
# (also call obersvation level variance).
#
# This is in line with Nakagawa et al. 2017, Suppl. 2, and package MuMIm
# see https://royalsocietypublishing.org/action/downloadSupplement?doi=10.1098%2Frsif.2017.0213&file=rsif20170213supp2.pdf

Check warning on line 577 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/compute_variances.R,line=577,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.

Check warning on line 577 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/compute_variances.R,line=577,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.
#
# We need:
# - the overdispersion parameter / sigma
Expand All @@ -581,7 +589,7 @@
faminfo,
model_null = NULL,
revar_null = NULL,
name,

Check warning on line 592 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint / lint

file=R/compute_variances.R,line=592,col=44,[function_argument_linter] Arguments without defaults should come before arguments with defaults.

Check warning on line 592 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/compute_variances.R,line=592,col=44,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
approx_method = "lognormal",
model_component = NULL,
verbose = TRUE) {
Expand Down Expand Up @@ -762,7 +770,7 @@
# different methods, which are now also implemented here.
#
# This is in line with Nakagawa et al. 2017, Suppl. 2, and package MuMIm
# see https://royalsocietypublishing.org/action/downloadSupplement?doi=10.1098%2Frsif.2017.0213&file=rsif20170213supp2.pdf

Check warning on line 773 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/compute_variances.R,line=773,col=121,[line_length_linter] Lines should not be more than 120 characters. This line is 122 characters.
#
# There may be small deviations to Nakagawa et al. for the null-model, which
# despite being correctly re-formulated in "null_model()", returns slightly
Expand All @@ -778,7 +786,7 @@
sig,
model_null = NULL,
revar_null = NULL,
name,

Check warning on line 789 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/compute_variances.R,line=789,col=38,[function_argument_linter] Arguments without defaults should come before arguments with defaults.
approx_method = "lognormal",
model_component = NULL,
verbose = TRUE) {
Expand Down Expand Up @@ -1204,7 +1212,7 @@
if (length(rs) > length(fe)) rs <- rs[seq_along(fe)]
if (length(fe) > length(rs)) fe <- fe[seq_along(rs)]

all(mapply(function(r, f) all(r %in% f), rs, fe, SIMPLIFY = TRUE))

Check warning on line 1215 in R/compute_variances.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/compute_variances.R,line=1215,col=7,[undesirable_function_linter] Avoid undesirable function "mapply".
}


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
Loading