Skip to content

Commit

Permalink
Issues with random effects: estimate_slopes and estimate_contrasts fa…
Browse files Browse the repository at this point in the history
…ilures using the marginaleffects backend (#410)

* Issues with random effects: estimate_slopes and estimate_contrasts failures using the marginaleffects backend
Fixes #409

* add tests

* news

* update tests
  • Loading branch information
strengejacke authored Feb 20, 2025
1 parent 5c55143 commit b2e5544
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 24 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: modelbased
Title: Estimation of Model-Based Predictions, Contrasts and Means
Version: 0.9.0.25
Version: 0.9.0.26
Authors@R:
c(person(given = "Dominique",
family = "Makowski",
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

* Fixed issues with contrasting slopes when `backend` was `"emmeans"`.

* Fixed issue in `estimate_slopes()` for models from package *lme4*.

# modelbased 0.9.0

## Breaking Changes
Expand Down
11 changes: 8 additions & 3 deletions R/get_marginaltrends.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ get_marginaltrends <- function(model,
if (is.null(by)) {
datagrid <- datagrid_info <- NULL
} else {
# setup arguments
dg_args <- list(model, by = by, verbose = FALSE)
dg_args <- list(
model,
by = by,
factors = "all",
include_random = TRUE,
verbose = FALSE
)
# add user-arguments from "...", but remove those arguments that are already set
dots[c("by", "verbose")] <- NULL
dots[c("by", "factors", "include_random", "verbose")] <- NULL
dg_args <- insight::compact_list(c(dg_args, dots))

# Get corresponding datagrid (and deal with particular ats)
Expand Down
2 changes: 2 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ intra
io
jmr
lifecycle
lme
lme4
loess
marginaleffects
marginalizations
Expand Down
36 changes: 16 additions & 20 deletions tests/testthat/_snaps/plot/plot-slopes-5.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions tests/testthat/test-estimate_slopes.R
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,44 @@ test_that("estimate_slopes, custom comparison", {
expect_identical(dim(out), c(1L, 7L))
expect_equal(out$Difference, -0.08782885, tolerance = 1e-4)
})

skip_on_cran()

test_that("estimate_slopes, works with lme4", {
skip_if_not_installed("lme4")
data(CO2)
model_lme4 <- suppressWarnings(lme4::lmer(
uptake ~ conc * Plant + Treatment + (1 | Type),
data = CO2
))
out <- estimate_slopes(model_lme4, trend = "conc", by = "Plant")
expect_identical(dim(out), c(12L, 7L))
expect_equal(
out$Slope,
c(
0.01847, 0.02553, 0.02322, 0.02119, 0.02458, 0.02548, 0.01366,
0.01519, 0.02286, 0.00541, 0.00632, 0.01085
),
tolerance = 1e-3
)
})


test_that("estimate_slopes, works with glmmTMB", {
skip_if_not_installed("glmmTMB")
data(CO2)
model_glmmTMB <- suppressWarnings(glmmTMB::glmmTMB(
uptake ~ conc * Plant + (1 | Type),
data = CO2
))
out <- estimate_slopes(model_glmmTMB, trend = "conc", by = "Plant")
expect_identical(dim(out), c(12L, 7L))
expect_equal(
out$Slope,
c(
0.01847, 0.02553, 0.02322, 0.02119, 0.02458, 0.02548, 0.01366,
0.01519, 0.02286, 0.00541, 0.00632, 0.01085
),
tolerance = 1e-3
)
})

0 comments on commit b2e5544

Please sign in to comment.