Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Jun 28, 2022
1 parent a81c92c commit c66b8f1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
18 changes: 16 additions & 2 deletions R/find_transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#' or exp-transforming, was applied to the response variable (dependent
#' variable) in a regression formula. Currently, following patterns are
#' detected: `log`, `log1p`, `log2`, `log10`, `exp`, `expm1`, `sqrt`,
#' `log(x+<number>)` and `log-log`.
#' `log(x+<number>)`, `log-log` and `power` (to 2nd power, like `I(x^2)`).
#'
#' @param x A regression model.
#' @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
#' log-transforming.
#' log-transforming. For unknown transformations, returns `NULL`.
#'
#' @examples
#' # identity, no transformation
Expand Down Expand Up @@ -91,5 +91,19 @@ find_transformation <- function(x) {
}
}


# (unknown) I-transformation

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

This comment has been minimized.

Copy link
@mattansb

mattansb Jul 5, 2022

Member

Can this include some sort of catch all?
What would foo(y) ~ 1 return?

This comment has been minimized.

Copy link
@strengejacke

strengejacke Jul 5, 2022

Author Member

"identidy", I think. We could probably have an "unknown" category.



# power-transformation

if (any(grepl("I\\((.*)\\^\\s*2\\)", rv))) {
transform_fun <- "power"
}

transform_fun
}
10 changes: 9 additions & 1 deletion R/get_transformation.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#' transform the response variable; `$inverse`, the inverse-function of
#' `$transformation` (can be used for "back-transformation"). If no
#' transformation was applied, both list-elements `$transformation` and
#' `$inverse` just return `function(x) x`.
#' `$inverse` just return `function(x) x`. If transformation is unknown,
#' `NULL` is returned.
#'
#' @examples
#' # identity, no transformation
Expand All @@ -38,6 +39,11 @@
get_transformation <- function(x) {
transform_fun <- find_transformation(x)

# unknown
if (is.null(transform_fun)) {
return(NULL)
}

if (transform_fun == "identity") {
out <- list(transformation = function(x) x, inverse = function(x) x)
} else if (transform_fun == "log") {
Expand All @@ -52,6 +58,8 @@ get_transformation <- function(x) {
out <- list(transformation = exp, inverse = log)
} else if (transform_fun == "sqrt") {
out <- list(transformation = sqrt, inverse = function(x) x^2)
} else if (transform_fun == "power") {
out <- 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
4 changes: 2 additions & 2 deletions man/find_transformation.Rd

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

3 changes: 2 additions & 1 deletion man/get_transformation.Rd

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

0 comments on commit c66b8f1

Please sign in to comment.