Skip to content

Commit

Permalink
Should we add allow.new.levels=TRUE by default? (#987)
Browse files Browse the repository at this point in the history
* Should we add `allow.new.levels=TRUE` by default?
Fixes easystats/modelbased#285

* news

* wordlist
  • Loading branch information
strengejacke authored Jan 2, 2025
1 parent f9ff9dd commit e94ec60
Show file tree
Hide file tree
Showing 5 changed files with 36 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.0.5
Version: 1.0.0.6
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 @@ -19,6 +19,9 @@

* Fixed issue with `as.numeric()` method for `get_sigma()`.

* `get_datagrid()` now only returns valid levels when `include_random = TRUE`
and group-level factor in random effects is numeric.

# insight 1.0.0

## Breaking changes
Expand Down
20 changes: 13 additions & 7 deletions R/get_datagrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -518,14 +518,20 @@ get_datagrid.default <- function(x,

# Drop random factors
random_factors <- find_random(x, flatten = TRUE)
if (isFALSE(include_random) && !is.null(random_factors)) {
keep <- c(find_predictors(x, effects = "fixed", flatten = TRUE), response)
if (!is.null(keep)) {
if (all(by != "all")) {
keep <- c(keep, by[by %in% random_factors])
random_factors <- setdiff(random_factors, by)
if (!is.null(random_factors)) {
if (isFALSE(include_random)) {
# drop random factors, if these should not be included
keep <- c(find_predictors(x, effects = "fixed", flatten = TRUE), response)
if (!is.null(keep)) {
if (all(by != "all")) {
keep <- c(keep, by[by %in% random_factors])
random_factors <- setdiff(random_factors, by)
}
data <- data[colnames(data) %in% keep]
}
data <- data[colnames(data) %in% keep]
} else {
# make sure random factors are not numeric, else, wrong "levels" will be returned
data[random_factors] <- lapply(data[random_factors], as.factor)
}
}

Expand Down
1 change: 1 addition & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Biometrics
Bundock
CMD
Coloured
DCchoice
DOI
DirichletReg
DirichletRegModel
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 @@ -394,3 +394,21 @@ test_that("get_datagrid - multiple weight variables", {
tolerance = 1e-3
)
})


test_that("get_datagrid - include_random works with numeric group factors", {
skip_if_not_installed("glmmTMB")
data(mtcars)
mtcars$vs <- as.factor(mtcars$vs)
model <- glmmTMB::glmmTMB(
mpg ~ vs + (1 | cyl),
data = mtcars
)
out <- get_datagrid(model, include_random = TRUE)
expect_identical(
out$cyl,
structure(c(1L, 1L, 2L, 2L, 3L, 3L), levels = c("4", "6", "8"), class = "factor")
)
out <- get_datagrid(model, include_random = FALSE)
expect_identical(out$cyl, c(NA, NA))
})

0 comments on commit e94ec60

Please sign in to comment.