Finding Optimal Lambda for Box-Cox Transform in R
Note that y~1
counts as a linear model in R, so you can use the boxcox
function from MASS:
tmp <- exp(rnorm(10))
out <- boxcox(lm(tmp~1))
range(out$x[out$y > max(out$y)-qchisq(0.95,1)/2])
I think that the most important part of that function is not that it finds a "best" lambda, but that it finds the confidence interval for lambda, then encourages you to think about what the different transformations mean and combine that with the science behind the data. If the "best" lambda for your data is 0.41, but the interval contains 0.5 and there is scientific reasoning why a square root transform makes sense, then why use 0.41 instead of 0.5?
For applying box cox transformation on vector, use forecast package in r:
library(forecast)
# to find optimal lambda
lambda = BoxCox.lambda( vector )
# now to transform vector
trans.vector = BoxCox( vector, lambda)