Finding Optimal Lambda for Box-Cox Transform in R

前端 未结 2 496
无人及你
无人及你 2021-02-06 08:23

I am trying to transform data in a vector in R.

This is not for linear regression so I don\'t have a predictor and response relationship. I am simply using a model that

相关标签:
2条回答
  • 2021-02-06 08:46

    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?

    0 讨论(0)
  • 2021-02-06 08:47

    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)
    
    0 讨论(0)
提交回复
热议问题