From ce30c74e1ccf3b187b90fb35491345f414b93af4 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 6 Feb 2025 22:39:15 +0100 Subject: [PATCH 1/3] `get_datagrid()` should include a dummy-weight variable for glmmTMB models Fixes #1002 --- DESCRIPTION | 2 +- R/get_datagrid.R | 11 +++++++++++ tests/testthat/test-get_datagrid.R | 18 ++++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/DESCRIPTION b/DESCRIPTION index b2b6da9e6..834e5fa5b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: insight Title: Easy Access to Model Information for Various Model Objects -Version: 1.0.2 +Version: 1.0.2.1 Authors@R: c(person(given = "Daniel", family = "Lüdecke", diff --git a/R/get_datagrid.R b/R/get_datagrid.R index 3f1a6740d..8b628d88e 100644 --- a/R/get_datagrid.R +++ b/R/get_datagrid.R @@ -580,6 +580,17 @@ get_datagrid.default <- function(x, } } + # if model has weights, we need to add a dummy for certain classes, e.g. glmmTMB + w <- insight::find_weights(x) + if (!inherits(x, "brmsfit") && !is.null(w)) { + # for lme, can't be NA + if (inherits(x, c("lme", "gls"))) { + vm[[w]] <- 1 + } else { + vm[[w]] <- NA_real_ + } + } + if (isFALSE(include_smooth)) { vm[colnames(vm) %in% clean_names(find_smooth(x, flatten = TRUE))] <- NULL } diff --git a/tests/testthat/test-get_datagrid.R b/tests/testthat/test-get_datagrid.R index 5ec8618e5..4ac7e1468 100644 --- a/tests/testthat/test-get_datagrid.R +++ b/tests/testthat/test-get_datagrid.R @@ -487,3 +487,21 @@ test_that("get_datagrid - informative error when by not found", { regex = "was not found" ) }) + + +test_that("get_datagrid - include weights", { + skip_if_not_installed("glmmTMB") + skip_if_not_installed("lme4") + + data(sleepstudy, package = "lme4") + set.seed(123) + sleepstudy$wei_factor <- abs(rnorm(nrow(sleepstudy), 1, 0.4)) + + m <- glmmTMB::glmmTMB(Reaction ~ Days + (1 | Subject), data = sleepstudy, weights = wei_factor) + d <- insight::get_datagrid(m, "Days") + expect_named(d, c("Days", "Subject", "wei_factor")) + + m <- glmmTMB::glmmTMB(Reaction ~ Days + (1 | Subject), data = sleepstudy) + d <- insight::get_datagrid(m, "Days") + expect_named(d, c("Days", "Subject")) +}) From 2eb47d5dffa01ddc0c5664dac8dd6b9f7105402b Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 6 Feb 2025 22:41:04 +0100 Subject: [PATCH 2/3] news --- NEWS.md | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/NEWS.md b/NEWS.md index 8f34f40b7..bb8ae40f9 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,11 @@ -# insight 1.02 +# insight (devel) + +## Changes + +* `get_datagrid()` now includes a dummy column for model weights (values `NA`), + to work with models that used weights. + +# insight 1.0.2 ## Changes @@ -20,7 +27,7 @@ * Fixed issue in `get_varcov()` for models of class `brmsfit` that included monotonic effects. -# insight 1.01 +# insight 1.0.1 ## General From 267f5dca72915670825099afe1dd3abe44a70b5e Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 6 Feb 2025 22:55:07 +0100 Subject: [PATCH 3/3] fix --- R/get_datagrid.R | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/R/get_datagrid.R b/R/get_datagrid.R index 8b628d88e..fe7593c2e 100644 --- a/R/get_datagrid.R +++ b/R/get_datagrid.R @@ -585,9 +585,9 @@ get_datagrid.default <- function(x, if (!inherits(x, "brmsfit") && !is.null(w)) { # for lme, can't be NA if (inherits(x, c("lme", "gls"))) { - vm[[w]] <- 1 + vm[w] <- 1 } else { - vm[[w]] <- NA_real_ + vm[w] <- NA_real_ } }