diff --git a/DESCRIPTION b/DESCRIPTION index 245bb3ce0..917dabb53 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", diff --git a/NEWS.md b/NEWS.md index 75f79b149..b9c014c75 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/R/find_formula.R b/R/find_formula.R index 7f0ba00c8..ff3ebf837 100644 --- a/R/find_formula.R +++ b/R/find_formula.R @@ -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 @@ -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, @@ -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)) } diff --git a/R/find_predictors.R b/R/find_predictors.R index 57fecf219..7aebe41c7 100644 --- a/R/find_predictors.R +++ b/R/find_predictors.R @@ -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 @@ -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")) {