From b4ca258612a9cc296931ef4607768cfb182aa21c Mon Sep 17 00:00:00 2001 From: Daniel Date: Wed, 26 Jun 2024 15:12:08 +0200 Subject: [PATCH] ... --- DESCRIPTION | 2 +- NEWS.md | 3 +++ R/get_transformation.R | 18 ++++++++++++------ tests/testthat/test-get_transformation.R | 11 +++++++++++ 4 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 tests/testthat/test-get_transformation.R diff --git a/DESCRIPTION b/DESCRIPTION index 74079e905..9ae5c6845 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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", diff --git a/NEWS.md b/NEWS.md index 5c0aa02a8..12305c672 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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 diff --git a/R/get_transformation.R b/R/get_transformation.R index 4e7f6848b..21c9c84d6 100644 --- a/R/get_transformation.R +++ b/R/get_transformation.R @@ -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") { diff --git a/tests/testthat/test-get_transformation.R b/tests/testthat/test-get_transformation.R new file mode 100644 index 000000000..db95650bc --- /dev/null +++ b/tests/testthat/test-get_transformation.R @@ -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) +})