Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 26, 2024
1 parent a01f07f commit b4ca258
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 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.20.1.8
Version: 0.20.1.9
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 @@ -32,6 +32,9 @@
* `find_terms()` and `find_transformation()` now better cope with inverse
transformations of the response value, such as `1/y`.

* `get_transformation()` now returns more transformations for power-transformed
response variables.

## Bug fixes

* `null_model()` now correctly handles zero-inflated models from package
Expand Down
18 changes: 12 additions & 6 deletions R/get_transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,18 @@ get_transformation <- function(x, verbose = TRUE) {
} else if (transform_fun == "inverse") {
out <- list(transformation = function(x) 1 / x, inverse = function(x) x^-1)
} else if (transform_fun == "power") {
## TODO: detect power - can we turn this into a function?
# power <- .safe(gsub("\\(|\\)", "", gsub("(.*)(\\^|\\*\\*)\\s*(\\d+|[()])", "\\3", find_terms(x)[["response"]])))
# if (is.null(power)) {
# power <- 2
# }
out <- list(transformation = function(x) x^2, inverse = sqrt)
## TODO: detect power - can we turn this into a generic function?
trans_power <- .safe(gsub("\\(|\\)", "", gsub("(.*)(\\^|\\*\\*)\\s*(\\d+|[()])", "\\3", find_terms(x)[["response"]]))) # nolint
if (is.null(trans_power)) {
trans_power <- "2"
}
out <- switch(trans_power,
`0.5` = list(transformation = function(x) x^0.5, inverse = function(x) x^2),
`3` = list(transformation = function(x) x^3, inverse = function(x) x^(1 / 3)),
`4` = list(transformation = function(x) x^4, inverse = function(x) x^0.25),
`5` = list(transformation = function(x) x^5, inverse = function(x) x^0.2),
list(transformation = function(x) x^2, inverse = sqrt)
)
} else if (transform_fun == "expm1") {
out <- list(transformation = expm1, inverse = log1p)
} else if (transform_fun == "log-log") {
Expand Down
11 changes: 11 additions & 0 deletions tests/testthat/test-get_transformation.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("get_transformation - detect powers", {
data(iris)
m <- lm(Sepal.Length^2 ~ Species, data = iris)
fun <- get_transformation(m)
expect_identical(fun$transformation(2), 4)
expect_identical(fun$inverse(4), 2)
m <- lm(I(Sepal.Length^3) ~ Species, data = iris)
fun <- get_transformation(m)
expect_identical(fun$transformation(2), 8)
expect_identical(fun$inverse(8), 2)
})

0 comments on commit b4ca258

Please sign in to comment.