问题描述
我正在尝试进行自动Box-cox转换(这通常对规范数据的人们很有用),但是在以R的乐观主义者可以接受的方式来表达我的优化方面遇到了麻烦.它通常可以工作,但是我不清楚是什么导致它在极端偏斜的变量上失败.
I'm trying to make an automatic box-cox transform (which should be generally useful to folks norming data), but having trouble phrasing my optimization in a way that R's optim is OK with. It generally works, but I'm unclear on what's causing it to fail on variables with extreme skew.
想法是在box-cox变换中选择Lambda参数,以使数据集偏度的绝对值最小.
The idea is to choose the parameter of Lambda in the box-cox transform that minimizes the absolute value of the skewness of the dataset.
library(car)
library(moments)
xskew <- function(data,par){
abs(skewness(bcPower(data,lambda=par[1]),na.rm=T)) # minimize abs(skew)
}
boxit <- function(x){
res <- optim(par=c(-5,5), xskew, data=x+1) # find argmin(^) lambda
print(res$par)
return(bcPower(x+1,lambda=res$par[1]))
这通常效果很好,例如:
This generally works quite well, for example:
> skewness(rbeta(1000,12,3))
[1] -0.6439532
成为
> skewness(boxit(rbeta(1000,12,3)))
[1] -5.980757e-08
-几乎为0偏斜.
但是在一个极其偏斜的变量上,我得到了:
But on one extremely skewed variable, I'm getting:
Error in optim(par = c(-5, 5), xskew, data = x + 1) (from #2) :
function cannot be evaluated at initial parameters
我的想法可能是:
- 不了解bcPower函数如何处理接近零或无穷大的值.
- 滥用乐观情绪
- 也许做些更愚蠢的事情,因为我正在构架一些可能无法融合的事情.
推荐答案
糟糕,我使用的是2参数求解器,而不是使用具有明确的上下限的1-参数求解器.我需要的最佳通话是:
Oops, I was using a 2 param solver instead of using a 1-param solver with explicit lower, upper bounds. The optim call I needed was:
optim(par=-2, xskew, x=x, method="Brent", lower=-20, upper=20)
稍微重新定义了xskew函数调用.
And slight redefinition to the xskew function call.
这篇关于优化Box-cox转换,无法在初始参数处评估功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!