Skip to content

Commit

Permalink
Misleading names using pretty_names in combination with cut() (#945)
Browse files Browse the repository at this point in the history
* Misleading names using `pretty_names` in combination with `cut()`
Fixes #943

* add test
  • Loading branch information
strengejacke authored Feb 7, 2024
1 parent e2a7d5f commit 1bd2884
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions R/format_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -319,11 +319,11 @@ format_parameters.parameters_model <- function(model, ...) {
if (all(grepl(pattern_cut_right, level))) {
lower_bounds <- gsub(pattern_cut_right, "\\1", level)
upper_bounds <- gsub(pattern_cut_right, "\\2", level)
level <- paste0(as.numeric(lower_bounds) + 1, "-", upper_bounds)
level <- paste0(">", as.numeric(lower_bounds), "-", upper_bounds)
} else if (all(grepl(pattern_cut_left, level))) {
lower_bounds <- gsub(pattern_cut_left, "\\1", level)
upper_bounds <- gsub(pattern_cut_left, "\\2", level)
level <- paste0(lower_bounds, "-", as.numeric(upper_bounds) - 1)
level <- paste0(lower_bounds, "-<", as.numeric(upper_bounds))
}
paste0(variable, " ", brackets[1], level, brackets[2])
}
Expand Down
15 changes: 15 additions & 0 deletions tests/testthat/test-format_parameters.R
Original file line number Diff line number Diff line change
Expand Up @@ -381,5 +381,20 @@ withr::with_options(
"e42dep [2] * c12hour"
)
})

test_that("format_parameters, cut", {
data(mtcars)
mtcars$grp <- cut(mtcars$mpg, breaks = c(0, 15, 20, 50))
out <- model_parameters(lm(wt ~ grp, data = mtcars))
expect_equal(
attributes(out)$pretty_names,
c(
`(Intercept)` = "(Intercept)", `grp(15,20]` = "grp [>15-20]",
`grp(20,50]` = "grp [>20-50]"
),
ignore_attr = TRUE
)
expect_identical(out$Parameter, c("(Intercept)", "grp(15,20]", "grp(20,50]"))
})
}
)

0 comments on commit 1bd2884

Please sign in to comment.