Skip to content

Commit

Permalink
Option to include numeric values for weights in get_datagrid()
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Feb 25, 2025
1 parent aa4e1da commit 1bc787f
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 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: insight
Title: Easy Access to Model Information for Various Model Objects
Version: 1.0.2.19
Version: 1.0.2.20
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* `get_datagrid()` gets a `digits` argument, to round numeric representative
values.

* `get_datagrid()` gets an `include_weights` argument, to include numeric values
for weights in `get_datagrid()` (instead of setting them to `NA`).

* Argument `ci_digits` defaults to `digits` in `format_table()`.

* `format_table()` gets a `select` argument, which can be used to select columns
Expand Down
25 changes: 18 additions & 7 deletions R/get_datagrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
#' usually need data with all variables in the model included.
#' @param include_response If `x` is a model object, decide whether the response
#' variable should be included in the data grid or not.
#' @param include_weights If `x` is a model object, which contains weights, the
#' weight-variable is usually set to `NA` in the returned data grid. Sometimes,
#' it is required to have an explicit valid value for the weight variable in
#' the grid. If so, set `include_weights = TRUE`, and the mean value of the
#' weight variable (instead of `NA`) is included in the returned grid.
#' @param data Optional, the data frame that was used to fit the model. Usually,
#' the data is retrieved via `get_data()`.
#' @param digits Number of digits used for rounding numeric values specified in
Expand Down Expand Up @@ -505,6 +510,7 @@ get_datagrid.default <- function(x,
include_smooth = TRUE,
include_random = FALSE,
include_response = FALSE,
include_weights = FALSE,
data = NULL,
digits = 3,
verbose = TRUE,
Expand Down Expand Up @@ -598,12 +604,16 @@ 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 (!is.null(w)) {
if (include_weights) {
vm[w] <- mean(get_weights(m, remove_na = TRUE, null_as_ones = TRUE))
} else if (!inherits(x, "brmsfit")) {
# for lme, can't be NA
if (inherits(x, c("lme", "gls"))) {
vm[w] <- 1
} else {
vm[w] <- NA_real_
}
}
}

Expand Down Expand Up @@ -634,6 +644,7 @@ get_datagrid.wbm <- function(x,
reference = x,
include_smooth = TRUE,
include_random = FALSE,
include_weights = FALSE,
data = NULL,
...) {
# Retrieve data from model
Expand All @@ -655,7 +666,7 @@ get_datagrid.wbm <- function(x,
x = x, by = by, factors = factors, numerics = numerics,
preserve_range = preserve_range, reference = reference,
include_smooth = include_smooth, include_random = include_random,
include_response = TRUE, data = data, ...
include_response = TRUE, include_weights = include_weights, data = data, ...
)
}

Expand Down
7 changes: 7 additions & 0 deletions man/get_datagrid.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/testthat/test-get_datagrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,20 @@ test_that("get_datagrid - include weights", {
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"))
expect_identical(
d$wei_factor,
c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_,
NA_real_, NA_real_, NA_real_, NA_real_
)
)
d <- insight::get_datagrid(m, "Days", include_weights = TRUE)
expect_named(d, c("Days", "Subject", "wei_factor"))
expect_equal(
d$wei_factor,
c(1.00213, 1.00213, 1.00213, 1.00213, 1.00213, 1.00213, 1.00213,
1.00213, 1.00213, 1.00213
)
)

m <- glmmTMB::glmmTMB(Reaction ~ Days + (1 | Subject), data = sleepstudy)
d <- insight::get_datagrid(m, "Days")
Expand Down

0 comments on commit 1bc787f

Please sign in to comment.