I am trying to do an automatic box-cox conversion (which should be generally useful for normal data for people), but with a mistake formulate my optimization in such a way that R optim is fine. This usually works, but I don’t understand why it fails on variables with extreme tilt.
The idea is to select the Lambda parameter in the box-cox transform, which minimizes the absolute amount of data set distortion.
library(car)
library(moments)
xskew <- function(data,par){
abs(skewness(bcPower(data,lambda=par[1]),na.rm=T))
}
boxit <- function(x){
res <- optim(par=c(-5,5), xskew, data=x+1)
print(res$par)
return(bcPower(x+1,lambda=res$par[1]))
This usually works well, for example:
> skewness(rbeta(1000,12,3))
[1] -0.6439532
becomes
> skewness(boxit(rbeta(1000,12,3)))
[1] -5.980757e-08
--- almost 0 skew.
But on one extremely skewed variable, I get:
Error in optim(par = c(-5, 5), xskew, data = x + 1) (from #2) :
function cannot be evaluated at initial parameters
My thoughts are probably the following:
- Do not catch how bcPower works with values near zero or infinity.
- , - , , .