boxcox_transform.Rd
Wrapper function to the boxcox
function; estimates the optimal parameter
for the Box-Cox power transformation and
then applies the appropriate power transformation
to the outcome variable.
boxcox_transform(
x,
outcome = NULL,
parameter_grid = seq(-3, 3, 0.01),
output = TRUE
)
Either
A numeric vector of positive values;
A data frame with an outcome variable and set of predictors for a linear regression.
An optional character string with
the column name for the outcome variable in x
.
Otherwise, the first column in x
is assumed
to be the outcome variable.
Vector specifying the grid of parameters to explore for maximum likelihood estimation.
Logical; if TRUE
, returns the
outcome variable following the power transformation.
Either the maximum likelihood estimate for the power transformation parameter or a vector for the outcome variable following the transformation.
The Box-Cox power transformation for a vector of positive values \(x\) and parameter \(\lambda\) is:
$$ f(x) = \frac{x^{\lambda} - 1}{\lambda}. $$
For \(\lambda = 0\), one simply uses the log transform.
# Simulate 100 values from the normal distribution
set.seed(3)
z <- rnorm(100)
# Transform the simulated values
x <- (z * .5 + 1)^(1 / .5)
# Maximum likelihood estimate for
# transformation parameter
boxcox_transform(x, output = FALSE)
#> [1] 0.44
# Histogram of x, transform of x, and
# original simulated values
layout(rbind(1, 2, 3))
hist(x)
hist(boxcox_transform(x))
hist(z)