Skip to content

Commit

Permalink
get_modelmatrix() fails for brms models with monotonic effects mo (#…
Browse files Browse the repository at this point in the history
…909)

* get_modelmatrix() fails  for brms models with monotonic effects `mo`
Fixes #908

* add test

* version

* fix test
  • Loading branch information
strengejacke authored Jul 23, 2024
1 parent 1824bb8 commit 72ffa3c
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
3 changes: 2 additions & 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.4
Version: 0.20.2.5
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down Expand Up @@ -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
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion R/get_modelmatrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 10 additions & 2 deletions tests/testthat/test-brms.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 -----------------------------------------------------------

Expand Down Expand Up @@ -513,7 +513,6 @@ test_that("find_algorithm", {
)
})


test_that("get_priors", {
expect_equal(
get_priors(m7),
Expand Down Expand Up @@ -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))
})

0 comments on commit 72ffa3c

Please sign in to comment.