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

find_variables() & brms: Distributional component variables missing #913

Merged
merged 7 commits into from
Aug 7, 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.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 @@ -526,7 +526,7 @@
id <- attr(x, "id")

within_variables <- names(attr(x, "within"))
within_variables <- paste0(within_variables, collapse = "*")

Check warning on line 529 in R/find_formula.R

View workflow job for this annotation

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

file=R/find_formula.R,line=529,col=25,[paste_linter] Use paste(), not paste0(), to collapse a character vector when sep= is not used.
within_variables <- paste0("(", within_variables, ")")
e <- paste0("Error(", id, "/", within_variables, ")")

Expand Down Expand Up @@ -1578,6 +1578,33 @@
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)) {

Check warning on line 1599 in R/find_formula.R

View workflow job for this annotation

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

file=R/find_formula.R,line=1599,col=7,[if_not_else_linter] Prefer `if (A) x else y` to the less-readable `if (!A) y else x` in a simple if/else statement.
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 @@
}


compact_list(list(
compact_list(c(list(
conditional = f_cond,
random = f_random,
zero_inflated = f_zi,
Expand All @@ -1653,7 +1680,7 @@
zero_one_inflated = f_zoi,
conditional_one_inflated = f_coi,
kappa = f_kappa
))
), f_custom))
}


Expand Down
2 changes: 1 addition & 1 deletion R/find_predictors.R
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ find_predictors.default <- function(x,

f <- find_formula(x, verbose = verbose)
is_mv <- is_multivariate(f)
elements <- .get_elements(effects, component)
elements <- .get_elements(effects, component, model = x)


# filter formulas, depending on requested effects and components
Expand Down
10 changes: 9 additions & 1 deletion R/helper_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@
)
}

.get_elements <- function(effects, component) {
.get_elements <- function(effects, component, model = NULL) {
# all elements of a model
elements <- .all_elements()

Expand Down Expand Up @@ -280,6 +280,14 @@
return(auxiliary_parameters)
}

# if we have brms-models with custom formulas, we have element-names
# that are not covered by the standard elements. We then just do not
# filter elements.
if (inherits(model, "brmsfit") && component == "all") {
f <- insight::find_formula(model)
elements <- unique(c(elements, names(f)))
}

elements <- switch(effects,
all = elements,
fixed = elements[!elements %in% random_parameters],
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-brms.R
Original file line number Diff line number Diff line change
Expand Up @@ -889,3 +889,14 @@
out <- get_modelmatrix(m9)
expect_identical(dim(out), c(32L, 2L))
})

test_that("get_modelmatrix", {
m10 <- insight::download_model("brms_lf_1")
expect_identical(
find_variables(m10),
list(
response = "carb",
conditional = c("gear", "vs"),disc = c("disc", "cyl")

Check warning on line 899 in tests/testthat/test-brms.R

View workflow job for this annotation

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

file=tests/testthat/test-brms.R,line=899,col=37,[commas_linter] Put a space after a comma.
)
)
})
Loading