Skip to content

Commit

Permalink
Accessing term transformations in formula
Browse files Browse the repository at this point in the history
Fixes #374
  • Loading branch information
strengejacke committed Nov 4, 2024
1 parent c9b0ba8 commit c6c1c02
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
* `export_table()` can now split tables into more than three tables when
`table_width` is used (formerly, the maximum number of split tables was three).

* `find_transformation()` gets a `full_model` argument, to check all model terms
for transformations.

## Bug fix

* `clean_parameters()` now uses the correct labels for the random effects
Expand Down
27 changes: 20 additions & 7 deletions R/find_transformation.R
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#' @title Find possible transformation of response variables
#' @title Find possible transformation of model variables
#' @name find_transformation
#'
#' @description This functions checks whether any transformation, such as log-
#' 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(y+<number>)`, `log-log`, `power` (e.g. to 2nd power, like `I(y^2)`),
#' `inverse` (like `1/y`), `scale` (e.g., `y/3`), and `box-cox` (e-g-,
#' `(y^lambda - 1) / lambda`).
#' variable) in a regression formula. Optionally, all model terms can also be
#' checked. Currently, following patterns are detected: `log`, `log1p`,
#' `log2`, `log10`, `exp`, `expm1`, `sqrt`, `log(y+<number>)`, `log-log`,
#' `power` (e.g. to 2nd power, like `I(y^2)`), `inverse` (like `1/y`), `scale`
#' (e.g., `y/3`), and `box-cox` (e-g-, `(y^lambda - 1) / lambda`).
#'
#' @param x A regression model or a character string of the formulation of the
#' response variable.
#' @param full_model Logical, if `TRUE`, does not only check the response
#' variable, but all model terms.
#' @param ... Currently not used.
#'
#' @return A string, with the name of the function of the applied transformation.
Expand All @@ -31,6 +33,10 @@
#' model <- lm(log(Sepal.Length + 2) ~ Species, data = iris)
#' find_transformation(model)
#'
#' # find transformation for all model terms
#' model <- lm(mpg ~ log(wt) + I(gear^2) + exp(am), data = mtcars)
#' find_transformation(model, full_model = TRUE)
#'
#' # inverse, response provided as character string
#' find_transformation("1 / y")
#' @export
Expand All @@ -40,7 +46,7 @@ find_transformation <- function(x, ...) {


#' @export
find_transformation.default <- function(x, ...) {
find_transformation.default <- function(x, full_model = FALSE, ...) {
# validation check
if (is.null(x) || is.data.frame(x) || !is_model(x)) {
return(NULL)
Expand All @@ -52,6 +58,13 @@ find_transformation.default <- function(x, ...) {
find_transformation(i[["response"]])
})
unlist(result)
} else if (full_model) {
lapply(find_terms(x), function(i) {
stats::setNames(
unlist(lapply(i, find_transformation), use.names = FALSE),
clean_names(i)
)
})
} else {
# "raw" response
rv <- find_terms(x)[["response"]]
Expand Down
19 changes: 13 additions & 6 deletions man/find_transformation.Rd

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

0 comments on commit c6c1c02

Please sign in to comment.