Skip to content

Commit

Permalink
find_variables() & brms: Distributional component variables missing
Browse files Browse the repository at this point in the history
Fixes #896
  • Loading branch information
strengejacke committed Aug 6, 2024
1 parent 4c5ff45 commit 66b53aa
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 7 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.2.11
Version: 0.20.2.12
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

## Bug fixes

* Fixed issue in `find_formula()`, `find_predictors()` and `find_variables()`
for models from package *brms* with custom formulas.

* Fixed issues in `find_response()` for *brms* models with `mi()` function in
the response variable.

Expand Down
31 changes: 29 additions & 2 deletions R/find_formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,33 @@ find_formula.model_fit <- function(x, verbose = TRUE, ...) {
f_bias <- f$pforms$bias
f_bs <- f$pforms$bs

# brms formulas can also have custom names, based on variable names, e.g.:
# brm(
# bf(carb ~ gear * vs) + lf(disc ~ 0 + mo(cyl)),
# data = mtcars,
# family = cumulative("probit"),
# )
# the lf() part is in "f$pforms" with name "disc".
#
# we therefore need to check whether we have additional names not yet covered
# by the above exceptions.

# auxiliary names
auxiliary_names <- c(
"sigma", "mu", "nu", "shape", "beta", "phi", "hu", "ndt", "zoi", "coi",
"kappa", "bias", "bs", "zi"
)

# check if any further pforms exist
if (!all(names(f$pforms) %in% auxiliary_names)) {
custom_names <- setdiff(names(f$pforms), auxiliary_names)
if (length(custom_names)) {
f_custom <- f$pforms[custom_names]
}
} else {
f_custom <- NULL
}

f_sigmarandom <- NULL
f_betarandom <- NULL

Expand Down Expand Up @@ -1633,7 +1660,7 @@ find_formula.model_fit <- function(x, verbose = TRUE, ...) {
}


compact_list(list(
compact_list(c(list(
conditional = f_cond,
random = f_random,
zero_inflated = f_zi,
Expand All @@ -1653,7 +1680,7 @@ find_formula.model_fit <- function(x, verbose = TRUE, ...) {
zero_one_inflated = f_zoi,
conditional_one_inflated = f_coi,
kappa = f_kappa
))
), f_custom))
}


Expand Down
10 changes: 6 additions & 4 deletions R/find_predictors.R
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ find_predictors.default <- function(x,

# filter formulas, depending on requested effects and components
if (is_mv) {
f <- lapply(f, function(.x) .prepare_predictors(x, .x, elements))
f <- lapply(f, function(.x) .prepare_predictors(x, .x, elements, component))
} else {
f <- .prepare_predictors(x, f, elements)
f <- .prepare_predictors(x, f, elements, component)
}

# random effects are returned as list, so we need to unlist here
Expand Down Expand Up @@ -292,8 +292,10 @@ find_predictors.afex_aov <- function(x,
}


.prepare_predictors <- function(x, f, elements) {
f <- f[names(f) %in% elements]
.prepare_predictors <- function(x, f, elements, component = "all") {
if (!inherits(x, "brmsfit") && component != "all") {
f <- f[names(f) %in% elements]
}

# from conditional model, remove response
if (object_has_names(f, "conditional")) {
Expand Down

0 comments on commit 66b53aa

Please sign in to comment.