在R中转换左偏数据

时间:2019-12-10 14:48:39

标签: r statistics transformation skew

我有一列左斜,我需要对其进行变换。所以我尝试了

library(car)
vect<-c(1516201202, 1526238001, 1512050372, 1362933719, 1516342174, 1526502557 ,1523548827, 1512241202,1526417785, 1517846464)
powerTransform(vect)

向量中的值是13位数字的unix纪元时间戳,像这样,我有几千个值,将其中的10个值粘贴到这里,我对整列进行了相同的操作。这给了我一个错误

Error in qr.resid(xqr, w * fam(Y, lambda, j = TRUE, ...)) : NA/NaN/Inf in foreign function call (arg 5)

我期待转换后的列。关于如何在R中执行此操作的任何想法?

谢谢 拉吉

1 个答案:

答案 0 :(得分:1)

通常,car::powerTransform返回一个powerTransform对象(这是一个list,其中包含估计的Box-Cox变换参数)。要获取转换后的值,您需要使用bcPower输出对象car::powerTransform来转换原始数据。

很遗憾,您没有提供示例数据,因此下面是基于iris数据集的示例。

library(car)

# Box-Cox transformation of `Sepal.Length`
df <- iris
trans <- powerTransform(df$Sepal.Length)
# Or the same using formula syntax:
# trans <- powerTransform(Sepal.Length ~ 1, data = df)

# Add the transformed `Sepal.Length` data to the original `data.frame`
df <- cbind(
    df,
    Sepal.Length_trans = bcPower(
        with(iris, cbind(Sepal.Length)), coef(trans))[, 1])

# Show a histogram of the Box-Cox-transformed data    
library(ggplot2)
ggplot(df, aes(Sepal.Length_trans)) +
    geom_histogram(aes(Sepal.Length_trans), bins = 30)

enter image description here