Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get_datagrid() should include a dummy-weight variable for glmmTMB models #1003

Merged
merged 3 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 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: 1.0.2
Version: 1.0.2.1
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
11 changes: 9 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

Expand Down
11 changes: 11 additions & 0 deletions R/get_datagrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@

#' @rdname get_datagrid
#' @export
get_datagrid.data.frame <- function(x,

Check warning on line 201 in R/get_datagrid.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/get_datagrid.R,line=201,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 50 to at most 40.
by = "all",
factors = "reference",
numerics = "mean",
Expand Down Expand Up @@ -580,6 +580,17 @@
}
}

# 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
}
Expand Down Expand Up @@ -749,7 +760,7 @@
# Utilities -----------------------------------------------------------------

#' @keywords internal
.get_datagrid_clean_target <- function(x, by = NULL, ...) {

Check warning on line 763 in R/get_datagrid.R

View workflow job for this annotation

GitHub Actions / lint-changed-files / lint-changed-files

file=R/get_datagrid.R,line=763,col=1,[cyclocomp_linter] Reduce the cyclomatic complexity of this function from 48 to at most 40.
by_expression <- NA
varname <- NA
original_target <- by
Expand Down
18 changes: 18 additions & 0 deletions tests/testthat/test-get_datagrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
Loading