Skip to content

Commit

Permalink
fix (#992)
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke authored Jan 13, 2025
1 parent fedd7d8 commit 30bc540
Show file tree
Hide file tree
Showing 4 changed files with 27 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.1
Version: 1.0.1.1
Authors@R:
c(person(given = "Daniel",
family = "Lüdecke",
Expand Down
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# insight 1.02

## Bug fixes

* Option `"terciles"` and `"terciles2"` in `get_datagrid()` were swapped, i.e.
`"terciles"` was doing what was documented for `"terciles2"` and vice versa.
This has been fixed.

# insight 1.01

## General
Expand Down
17 changes: 10 additions & 7 deletions R/get_datagrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,6 @@ get_datagrid.visualisation_matrix <- function(x, reference = attributes(x)$refer
datagrid
}


#' @export
get_datagrid.datagrid <- get_datagrid.visualisation_matrix

Expand Down Expand Up @@ -699,6 +698,7 @@ get_datagrid.emmGrid <- function(x, ...) {
data.frame(s)[, which_cols, drop = FALSE]
}


#' @export
get_datagrid.emm_list <- function(x, ...) {
k <- length(x)
Expand All @@ -717,6 +717,7 @@ get_datagrid.emm_list <- function(x, ...) {
out[, c(clear_cols, setdiff(colnames(out), clear_cols)), drop = FALSE]
}


#' @export
get_datagrid.slopes <- function(x, ...) {
cols_newdata <- colnames(attr(x, "newdata"))
Expand Down Expand Up @@ -802,16 +803,14 @@ get_datagrid.comparisons <- get_datagrid.slopes
center <- stats::median(x, na.rm = TRUE)
spread <- stats::mad(x, na.rm = TRUE)
by_expression <- paste0("c(", center - spread, ",", center, ",", center + spread, ")")
} else if (parts == "quartiles") {
by_expression <- paste0("c(", paste(as.vector(stats::quantile(x, na.rm = TRUE)), collapse = ","), ")")
} else if (parts %in% c("fivenum", "quartiles")) {
by_expression <- paste0("c(", paste(as.vector(stats::fivenum(x, na.rm = TRUE)), collapse = ","), ")")
} else if (parts == "quartiles2") {
by_expression <- paste0("c(", paste(as.vector(stats::quantile(x, na.rm = TRUE))[2:4], collapse = ","), ")")
} else if (parts == "terciles") {
by_expression <- paste0("c(", paste(as.vector(stats::quantile(x, probs = (1:2) / 3, na.rm = TRUE)), collapse = ","), ")") # nolint
} else if (parts == "terciles2") {
by_expression <- paste0("c(", paste(as.vector(stats::quantile(x, probs = (0:3) / 3, na.rm = TRUE)), collapse = ","), ")") # nolint
} else if (parts == "fivenum") {
by_expression <- paste0("c(", paste(as.vector(stats::fivenum(x, na.rm = TRUE)), collapse = ","), ")")
} else if (parts == "terciles2") {
by_expression <- paste0("c(", paste(as.vector(stats::quantile(x, probs = (1:2) / 3, na.rm = TRUE)), collapse = ","), ")") # nolint
} else if (parts == "zeromax") {
by_expression <- paste0("c(0,", max(x, na.rm = TRUE), ")")
} else if (parts == "minmax") {
Expand Down Expand Up @@ -858,6 +857,7 @@ get_datagrid.comparisons <- get_datagrid.slopes
data.frame(varname = varname, expression = by_expression, stringsAsFactors = FALSE)
}


#' @keywords internal
.get_datagrid_summary <- function(x, numerics = "mean", factors = "reference", na.rm = TRUE, ...) {
if (na.rm) x <- stats::na.omit(x)
Expand Down Expand Up @@ -902,6 +902,7 @@ get_datagrid.comparisons <- get_datagrid.slopes
out
}


#' @keywords internal
.create_spread <- function(x, length = 10, range = "range", ci = 0.95, ...) {
range <- match.arg(tolower(range), c("range", "iqr", "ci", "hdi", "eti", "sd", "mad", "grid"))
Expand Down Expand Up @@ -965,6 +966,7 @@ get_datagrid.comparisons <- get_datagrid.slopes
seq(mini, maxi, length.out = length)
}


#' @keywords internal
.data_match <- function(x, to, ...) {
if (!is.data.frame(to)) {
Expand All @@ -979,6 +981,7 @@ get_datagrid.comparisons <- get_datagrid.slopes
.to_numeric(row.names(x)[idx])
}


#' @keywords internal
.get_model_data_for_grid <- function(x, data) {
# Retrieve data, based on variable names
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-get_datagrid.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ test_that("get_datagrid - terciles, quartiles, mean-sd", {
expect_identical(attributes(dg)$adjusted_for, c("Petal.Length", "Species"))

dg <- insight::get_datagrid(m, "Petal.Width = [terciles]")
expect_equal(dg$Petal.Width, unname(quantile(iris$Petal.Width, probs = (0:3) / 3)), tolerance = 1e-4)
expect_identical(attributes(dg)$adjusted_for, c("Petal.Length", "Species"))

dg <- insight::get_datagrid(m, "Petal.Width = [terciles2]")
expect_equal(dg$Petal.Width, unname(quantile(iris$Petal.Width, probs = (1:2) / 3)), tolerance = 1e-4)
expect_identical(attributes(dg)$adjusted_for, c("Petal.Length", "Species"))

dg <- insight::get_datagrid(m, "Petal.Width = [fivenum]")
expect_equal(dg$Petal.Width, unname(quantile(iris$Petal.Width)), tolerance = 1e-4)
expect_identical(attributes(dg)$adjusted_for, c("Petal.Length", "Species"))
})

# bracket tokens
Expand Down

0 comments on commit 30bc540

Please sign in to comment.