Skip to content

Commit

Permalink
get_variance() Triggers Model Recompilation and Returns NULL for … (
Browse files Browse the repository at this point in the history
#916)

* `get_variance()` Triggers Model Recompilation and Returns `NULL` for `brms` Mixed Models in v0.20.3
Fixes #915

* fix

* update test
  • Loading branch information
strengejacke authored Aug 25, 2024
1 parent 2cfd950 commit 96fd0c5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 18 deletions.
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.3
Version: 0.20.3.1
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# insight 0.20.4

## Bug fixes

* Fixed regression from latest fix related to `get_variance()` for *brms* models.

# insight 0.20.3

## Changes
Expand Down
21 changes: 13 additions & 8 deletions R/compute_variances.R
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,20 @@

# we also need necessary model information, like fixed and random effects,
# variance-covariance matrix etc. for the null model
if (is.null(model_null)) {
model_null <- .safe(null_model(model, verbose = FALSE))
if (faminfo$is_linear && !faminfo$is_tweedie) {
# we don't need these for linear models
me_info_null <- NULL
} else {
if (is.null(model_null)) {
model_null <- .safe(null_model(model, verbose = FALSE))
}
me_info_null <- .get_variance_information(
model_null,
faminfo = faminfo,
name_fun = name_fun,
verbose = verbose
)
}
me_info_null <- .get_variance_information(
model_null,
faminfo = faminfo,
name_fun = name_fun,
verbose = verbose
)

# Test for non-zero random effects ((near) singularity)
no_random_variance <- FALSE
Expand Down
27 changes: 18 additions & 9 deletions R/get_modelmatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,24 @@ get_modelmatrix.svyglm <- function(x, ...) {
#' @export
get_modelmatrix.brmsfit <- function(x, ...) {
formula_rhs <- safe_deparse(find_formula(x)$conditional[[3]])
formula_rhs <- stats::as.formula(paste0("~", formula_rhs))
# the formula used in model.matrix() is not allowed to have special functions,
# like brms::mo() and similar. Thus, we reformulate after using "all.vars()",
# which will only keep the variable names.
.data_in_dots(
...,
object = stats::reformulate(all.vars(formula_rhs)),
default_data = get_data(x, verbose = FALSE)
)
# exception: for null-models, we need different handling, else `reformulate()`
# will not work.
if (identical(formula_rhs, "1")) {
mm <- get_data(x, verbose = FALSE)
mm[[1]] <- 1
colnames(mm)[1] <- "(Intercept)"
mm[1]
} else {
formula_rhs <- stats::as.formula(paste0("~", formula_rhs))
# the formula used in model.matrix() is not allowed to have special functions,
# like brms::mo() and similar. Thus, we reformulate after using "all.vars()",
# which will only keep the variable names.
.data_in_dots(
...,
object = stats::reformulate(all.vars(formula_rhs)),
default_data = get_data(x, verbose = FALSE)
)
}
}

#' @export
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-brms.R
Original file line number Diff line number Diff line change
Expand Up @@ -900,3 +900,22 @@ test_that("get_modelmatrix", {
)
)
})

# get variance
test_that("get_variance works", {
mdl <- insight::download_model("brms_mixed_9")
out <- get_variance(mdl)
expect_equal(
out,
list(
var.fixed = 4.91103174480995,
var.random = 22.4069708072874,
var.residual = 11.3506326649,
var.distribution = 11.3506326649,
var.dispersion = 0,
var.intercept = c(cyl = 22.4069708072874)
),
tolerance = 1e-3,
ignore_attr = TRUE
)
})

0 comments on commit 96fd0c5

Please sign in to comment.