Skip to content

Commit

Permalink
find_transformation() returning multiple values
Browse files Browse the repository at this point in the history
Fixes #971
  • Loading branch information
strengejacke committed Nov 20, 2024
1 parent d04c989 commit d771f2c
Show file tree
Hide file tree
Showing 4 changed files with 31 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: 0.99.0.15
Version: 0.99.0.16
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 @@ -62,6 +62,9 @@

* Fixed issue in `compact_character()` and `compact_list()` for date-variables.

* Fixed edge case in `find_transformation()` for simple log-transformation of
the response variable.

# insight 0.20.5

## General
Expand Down
18 changes: 11 additions & 7 deletions R/find_transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,19 @@ find_transformation.character <- function(x, ...) {
if (grepl("log\\(log\\((.*)\\)\\)", x)) {
transform_fun <- "log-log"
} else {
# 1. try: log(x + number)
plus_minus <- .safe(
eval(parse(text = gsub("log\\(([^,\\+)]*)(.*)\\)", "\\2", x)))
)
# 2. try: log(number + x)
if (is.null(plus_minus)) {
plus_minus <- NULL
# make sure we definitly have a "+" in the log-transformation
if (grepl("+", x, fixed = TRUE)) {
# 1. try: log(x + number)
plus_minus <- .safe(
eval(parse(text = gsub("log\\(([^,\\+)]*)(.*)\\)", "\\1", x)))
eval(parse(text = gsub("log\\(([^,\\+)]*)(.*)\\)", "\\2", x)))
)
# 2. try: log(number + x)
if (is.null(plus_minus)) {
plus_minus <- .safe(
eval(parse(text = gsub("log\\(([^,\\+)]*)(.*)\\)", "\\1", x)))
)
}
}
if (is.null(plus_minus) || is.function(plus_minus)) {
transform_fun <- "log"
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-find_transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,19 @@ test_that("find_transformation - detect powers", {
)
)
})


test_that("find_transformation - works with log-edge cases", {
set.seed(1)
n <- 30
x <- rlnorm(n = n, meanlog = 3, sdlog = 1)
y <- exp(2 + log(x) + rnorm(n, sd = 3))
d <- data.frame(x, y)
m <- lm(log(y) ~ log(x), data = d)

expect_identical(find_transformation(m), "log")
expect_identical(
find_transformation(m, include_all = TRUE),
list(response = c(y = "log"), conditional = c(x = "log"))
)
})

0 comments on commit d771f2c

Please sign in to comment.