diff --git a/DESCRIPTION b/DESCRIPTION index d4fac5a1e..a0e8fd465 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.4 +Version: 0.20.2.5 Authors@R: c(person(given = "Daniel", family = "Lüdecke", @@ -214,3 +214,4 @@ Roxygen: list(markdown = TRUE) Config/testthat/edition: 3 Config/testthat/parallel: true Config/Needs/website: easystats/easystatstemplate +Config/Needs/check: stan-dev/cmdstanr diff --git a/NEWS.md b/NEWS.md index 9513aa901..0442975d7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,6 +16,9 @@ minimum required package version based on the SUGGEST field of the _insight_ package, instead of the package that was calling the function. +* Fixed issue in `get_modelmatrix()` for models from package *brms* with + special functions in the formula (like `mo()`). + # insight 0.20.2 ## New supported models diff --git a/R/get_modelmatrix.R b/R/get_modelmatrix.R index 063a05dee..879d7890c 100644 --- a/R/get_modelmatrix.R +++ b/R/get_modelmatrix.R @@ -159,7 +159,14 @@ get_modelmatrix.svyglm <- function(x, ...) { get_modelmatrix.brmsfit <- function(x, ...) { formula_rhs <- safe_deparse(find_formula(x)$conditional[[3]]) formula_rhs <- stats::as.formula(paste0("~", formula_rhs)) - .data_in_dots(..., object = formula_rhs, default_data = get_data(x, verbose = FALSE)) + # 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 diff --git a/tests/testthat/test-brms.R b/tests/testthat/test-brms.R index fd712b057..2bd985d77 100644 --- a/tests/testthat/test-brms.R +++ b/tests/testthat/test-brms.R @@ -2,7 +2,7 @@ skip_on_cran() skip_if_offline() skip_on_os("mac") skip_if_not_installed("brms") -skip_if_not_installed("httr") +skip_if_not_installed("httr2") # Model fitting ----------------------------------------------------------- @@ -513,7 +513,6 @@ test_that("find_algorithm", { ) }) - test_that("get_priors", { expect_equal( get_priors(m7), @@ -881,3 +880,12 @@ test_that("clean_parameters", { ignore_attr = TRUE ) }) + +test_that("get_modelmatrix", { + out <- get_modelmatrix(m1) + expect_identical(dim(out), c(236L, 4L)) + m9 <- insight::download_model("brms_mo2") + skip_if(is.null(m9)) + out <- get_modelmatrix(m9) + expect_identical(dim(out), c(32L, 2L)) +})