Skip to content

Commit 2e3f033

Browse files
Handle brms 0 + Intercept formulas in get_modelmatrix() (#1199)
* Initial plan * Fix brms get_modelmatrix Intercept handling Agent-Logs-Url: https://github.com/easystats/insight/sessions/599f4836-a7a8-431d-9330-d2405f4aff38 Co-authored-by: DominiqueMakowski <8875533+DominiqueMakowski@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: DominiqueMakowski <8875533+DominiqueMakowski@users.noreply.github.com>
1 parent 9b00f19 commit 2e3f033

4 files changed

Lines changed: 49 additions & 11 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Type: Package
22
Package: insight
33
Title: Easy Access to Model Information for Various Model Objects
4-
Version: 1.5.1.1
4+
Version: 1.5.1.2
55
Authors@R:
66
c(person(given = "Daniel",
77
family = "Lüdecke",

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
* Major performance improvement for `compact_list` on very large, nested list
66
objects.
77

8+
## Bug fixes
9+
10+
* `get_modelmatrix()` now correctly handles *brms* models fitted with
11+
`0 + Intercept` formulas.
12+
813
# insight 1.5.1
914

1015
## Changes

R/get_modelmatrix.R

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,33 @@ get_modelmatrix.svysurvreg <- get_modelmatrix.svyglm
178178

179179
#' @export
180180
get_modelmatrix.brmsfit <- function(x, ...) {
181-
formula_rhs <- safe_deparse(find_formula(x, verbose = FALSE)$conditional[[3]])
181+
conditional_formula <- find_formula(x, verbose = FALSE)$conditional
182+
formula_rhs <- safe_deparse(conditional_formula[[3]])
183+
model_data <- get_data(x, verbose = FALSE)
182184
# exception: for null-models, we need different handling, else `reformulate()`
183185
# will not work.
184186
if (identical(formula_rhs, "1")) {
185-
mm <- get_data(x, verbose = FALSE)
186-
mm[[1]] <- 1
187-
colnames(mm)[1] <- "(Intercept)"
188-
as.matrix(mm[1])
187+
matrix(1, nrow = nrow(model_data), dimnames = list(NULL, "(Intercept)"))
189188
} else {
190189
formula_rhs <- stats::as.formula(paste0("~", formula_rhs))
190+
intercept <- has_intercept(x, verbose = FALSE)
191+
predictors <- setdiff(all.vars(formula_rhs), "Intercept")
191192
# the formula used in model.matrix() is not allowed to have special functions,
192193
# like brms::mo() and similar. Thus, we reformulate after using "all.vars()",
193194
# which will only keep the variable names.
194-
.data_in_dots(
195-
...,
196-
object = stats::reformulate(all.vars(formula_rhs)),
197-
default_data = get_data(x, verbose = FALSE)
198-
)
195+
if (!length(predictors)) {
196+
if (intercept) {
197+
matrix(1, nrow = nrow(model_data), dimnames = list(NULL, "(Intercept)"))
198+
} else {
199+
matrix(nrow = nrow(model_data), ncol = 0)
200+
}
201+
} else {
202+
.data_in_dots(
203+
...,
204+
object = stats::reformulate(predictors, intercept = intercept),
205+
default_data = model_data
206+
)
207+
}
199208
}
200209
}
201210

tests/testthat/test-brms.R

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,30 @@ test_that("get_modelmatrix", {
10511051
expect_identical(dim(out), c(32L, 2L))
10521052
})
10531053

1054+
test_that("get_modelmatrix handles brms Intercept terms, Issue #1199", {
1055+
# apparently BH is required to fit these brms models
1056+
skip_if_not_installed("BH")
1057+
# sink() writing permission fail on some Windows CI machines
1058+
skip_on_os("windows")
1059+
1060+
void <- suppressMessages(suppressWarnings(capture.output({
1061+
mod <- brms::brm(
1062+
mpg ~ 0 + Intercept + wt,
1063+
data = mtcars,
1064+
refresh = 0,
1065+
chains = 1,
1066+
iter = 600,
1067+
warmup = 300,
1068+
cores = 1,
1069+
silent = 2
1070+
)
1071+
})))
1072+
1073+
out <- get_modelmatrix(mod)
1074+
expect_identical(colnames(out), c("(Intercept)", "wt"))
1075+
expect_identical(dim(out), c(nrow(mtcars), 2L))
1076+
})
1077+
10541078
test_that("find_variables, mo", {
10551079
m10 <- suppressWarnings(insight::download_model("brms_lf_1"))
10561080
expect_identical(

0 commit comments

Comments
 (0)