Learn R Programming

MVN (version 6.1)

power_transform: Apply Power Transformation to Numeric Data

Description

Applies a power transformation to numeric input data using the car::powerTransform function. Supported transformation families include Box-Cox ("bcPower"), Box-Cox with negative values ("bcnPower"), and Yeo-Johnson ("yjPower"). The function estimates either optimal or rounded lambda values for each numeric variable and transforms the data accordingly.

Usage

power_transform(
  data,
  family = c("bcPower", "bcnPower", "yjPower"),
  type = c("optimal", "rounded")
)

Value

A list containing two elements. The first is a data frame of transformed numeric columns. The second is a named numeric vector of the lambda values used for the transformation.

Arguments

data

A numeric vector, matrix, or data frame. Only numeric columns will be transformed. Non-numeric columns are dropped with a warning.

family

A character string specifying the transformation family. Must be one of "bcPower", "bcnPower", or "yjPower".

type

A character string specifying whether to use the estimated optimal lambda values ("optimal") or the rounded values ("rounded").

Details

Rows with missing values are removed prior to estimating lambda parameters. A warning is issued if any non-numeric columns are dropped or if any rows are excluded due to missingness. The same estimated lambda values are then applied to the original data (excluding dropped rows or columns).

Examples

Run this code
if (requireNamespace("car", quietly = TRUE)) {
  x <- rnorm(100, mean = 10, sd = 2)
  y <- rexp(100, rate = 0.2)
  df <- data.frame(x = x, y = y)
  result <- power_transform(df, family = "bcPower", type = "optimal")
  head(result$data)
  result$lambda
}

Run the code above in your browser using DataLab