Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 26, 2024
1 parent 15ba43d commit 2d73c7d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,8 @@ S3method(find_terms,aovlist)
S3method(find_terms,bfsl)
S3method(find_terms,default)
S3method(find_terms,mipo)
S3method(find_transformation,character)
S3method(find_transformation,default)
S3method(find_weights,brmsfit)
S3method(find_weights,default)
S3method(find_weights,gls)
Expand Down
52 changes: 35 additions & 17 deletions R/find_transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#' `log(x+<number>)`, `log-log`, `power` (to 2nd power, like `I(x^2)`), and
#' `inverse` (like `1/y`).
#'
#' @param x A regression model.
#' @param x A regression model or a character string of the response value.
#' @param ... Currently not used.
#'
#' @return A string, with the name of the function of the applied transformation.
#' Returns `"identity"` for no transformation, and e.g. `"log(x+3)"` when
#' a specific values was added to the response variables before
Expand All @@ -26,32 +28,48 @@
#' # log+2
#' model <- lm(log(Sepal.Length + 2) ~ Species, data = iris)
#' find_transformation(model)
#'
#' # inverse, response provided as character string
#' find_transformation("1 / y")
#' @export
find_transformation <- function(x, ...) {
UseMethod("find_transformation")
}


#' @export
find_transformation <- function(x) {
find_transformation.default <- function(x, ...) {
# validation check
if (is.null(x) || is.data.frame(x) || !is_model(x)) {
return(NULL)
}

rv <- find_terms(x)[["response"]]
find_transformation(rv)
}


#' @export
find_transformation.character <- function(x, ...) {
transform_fun <- "identity"

# remove whitespaces
x <- gsub(" ", "", x, fixed = TRUE)

# log-transformation

if (any(grepl("log\\((.*)\\)", rv))) {
if (any(grepl("log\\((.*)\\)", x))) {
# do we have log-log models?
if (grepl("log\\(log\\((.*)\\)\\)", rv)) {
if (grepl("log\\(log\\((.*)\\)\\)", x)) {
transform_fun <- "log-log"
} else {
# 1. try: log(x + number)
plus_minus <- .safe(
eval(parse(text = gsub("log\\(([^,\\+)]*)(.*)\\)", "\\2", rv)))
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", rv)))
eval(parse(text = gsub("log\\(([^,\\+)]*)(.*)\\)", "\\1", x)))
)
}
if (is.null(plus_minus)) {
Expand All @@ -65,40 +83,40 @@ find_transformation <- function(x) {

# log1p-transformation

if (any(grepl("log1p\\((.*)\\)", rv))) {
if (any(grepl("log1p\\((.*)\\)", x))) {
transform_fun <- "log1p"
}


# expm1-transformation

if (any(grepl("expm1\\((.*)\\)", rv))) {
if (any(grepl("expm1\\((.*)\\)", x))) {
transform_fun <- "expm1"
}


# log2/log10-transformation

if (any(grepl("log2\\((.*)\\)", rv))) {
if (any(grepl("log2\\((.*)\\)", x))) {
transform_fun <- "log2"
}

if (any(grepl("log10\\((.*)\\)", rv))) {
if (any(grepl("log10\\((.*)\\)", x))) {
transform_fun <- "log10"
}


# exp-transformation

if (any(grepl("exp\\((.*)\\)", rv))) {
if (any(grepl("exp\\((.*)\\)", x))) {
transform_fun <- "exp"
}


# sqrt-transformation

if (any(grepl("sqrt\\((.*)\\)", rv))) {
plus_minus <- eval(parse(text = gsub("sqrt\\(([^,\\+)]*)(.*)\\)", "\\2", rv)))
if (any(grepl("sqrt\\((.*)\\)", x))) {
plus_minus <- eval(parse(text = gsub("sqrt\\(([^,\\+)]*)(.*)\\)", "\\2", x)))
if (is.null(plus_minus)) {
transform_fun <- "sqrt"
} else {
Expand All @@ -109,21 +127,21 @@ find_transformation <- function(x) {

# inverse-transformation

if (any(startsWith(rv, "1/"))) {
if (any(startsWith(x, "1/"))) {
transform_fun <- "inverse"
}


# (unknown) I-transformation

if (any(grepl("I\\((.*)\\)", rv))) {
if (any(grepl("I\\((.*)\\)", x))) {
transform_fun <- NULL
}


# power-transformation

if (any(grepl("(.*)(\\^|\\*\\*)\\s?-?(\\d+|[()])", rv))) {
if (any(grepl("(.*)(\\^|\\*\\*)\\s?-?(\\d+|[()])", x))) {
transform_fun <- "power"
}

Expand Down
9 changes: 7 additions & 2 deletions man/find_transformation.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat/test-find_transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ test_that("find_transformation - identity", {
test_that("find_transformation - log", {
model <- lm(log(Sepal.Length) ~ Species, data = iris)
expect_identical(find_transformation(model), "log")
expect_identical(find_transformation("log(Sepal.Length)"), "log")
})

test_that("find_transformation - log+x", {
Expand All @@ -16,6 +17,7 @@ test_that("find_transformation - log+x", {
test_that("find_transformation - log+x 2", {
model <- lm(log(2 + Sepal.Length) ~ Species, data = iris)
expect_identical(find_transformation(model), "log(x+2)")
expect_identical(find_transformation("log(2 + Sepal.Length)"), "log(x+2)")
})

test_that("find_transformation - log-log", {
Expand Down Expand Up @@ -52,6 +54,7 @@ test_that("find_transformation - inverse", {
data(iris)
model <- lm(1 / Sepal.Length ~ Species, data = iris)
expect_identical(find_transformation(model), "inverse")
expect_identical(find_transformation("1 / Sepal.Length"), "inverse")
})

test_that("find_transformation - detect powers", {
Expand All @@ -72,6 +75,7 @@ test_that("find_transformation - detect powers", {
expect_identical(insight::find_transformation(m5), "power")
expect_identical(insight::find_transformation(m6), "power")
expect_identical(insight::find_transformation(m7), "power")
expect_identical(insight::find_transformation("Sepal.Length^-0.5"), "power")

# power **

Expand Down

0 comments on commit 2d73c7d

Please sign in to comment.