Optimise the Value of Lambda for a Box-Cox Transformation
opt_bc.RdOptimise the value of \(\lambda\) for a Box-Cox transformation by minimising skewness and return the optimally transformed data together with the optimal \(\lambda\) and corresponding skewness minimum as attributes.
Usage
opt_bc(x, interval = c(-5, 5), tol = 1e-10)Value
A numeric vector the same length as x with attributes lambda and
skew representing the optimal value of \(\lambda\) and corresponding skewness minimum respectively.
Details
Uses the functions boxcox3 to create a Box-Cox transformation function for the given dataset,
skewness to calculate the skewness of the transformed data and optimise
to search for the value of \(\lambda\) that minimises skewness.
Examples
runif(20) |> opt_bc()
#> [1] -0.389487956 -0.291273863 -0.387104761 -0.153246526 -0.255784964
#> [6] -0.119256282 -0.549328473 -0.106595569 -0.003647609 -0.393740450
#> [11] -0.464333828 -0.204016602 -0.342711961 -0.309740061 -0.133072727
#> [16] -0.353891453 -0.504438923 -0.078480620 -0.125376769 -0.505873266
#> attr(,"skew")
#> [1] 1.257495e-08
#> attr(,"lambda")
#> [1] 1.819868
rlnorm(20) |> opt_bc()
#> [1] -0.24011633 -1.05259584 -0.88894011 1.18323416 0.13336890 0.51256879
#> [7] -1.44742738 -1.27931013 0.29213838 1.52708517 0.24222190 1.50245976
#> [13] 0.55149568 0.64394978 -0.10877018 0.17509341 -0.08953948 2.33332720
#> [19] 1.47680090 0.80598527
#> attr(,"skew")
#> [1] 4.644734e-09
#> attr(,"lambda")
#> [1] 0.1942435
rpois(100, 3) |> opt_bc() ## Provokes warning
#> Warning: NA/NaN replaced by maximum positive value
#> [1] 2.3897589 0.9006722 0.9006722 2.3897589 3.0518408 0.9006722
#> [7] -1.3786378 3.6782702 1.6800267 2.3897589 2.3897589 0.9006722
#> [13] 0.9006722 2.3897589 2.3897589 0.0000000 3.0518408 0.0000000
#> [19] 3.0518408 0.0000000 1.6800267 0.9006722 2.3897589 0.0000000
#> [25] 3.6782702 0.9006722 0.9006722 3.0518408 3.0518408 0.0000000
#> [31] 3.0518408 2.3897589 0.9006722 5.9462986 0.9006722 3.6782702
#> [37] 0.9006722 -1.3786378 0.0000000 3.0518408 0.9006722 0.9006722
#> [43] -1.3786378 3.6782702 0.9006722 2.3897589 0.9006722 3.0518408
#> [49] 0.9006722 4.2765255 1.6800267 2.3897589 3.0518408 2.3897589
#> [55] 0.9006722 0.0000000 0.0000000 2.3897589 1.6800267 2.3897589
#> [61] 0.9006722 4.2765255 2.3897589 3.0518408 4.2765255 0.9006722
#> [67] 2.3897589 0.9006722 -1.3786378 1.6800267 1.6800267 0.9006722
#> [73] 3.0518408 1.6800267 4.2765255 -1.3786378 2.3897589 0.9006722
#> [79] 3.6782702 2.3897589 2.3897589 2.3897589 1.6800267 0.9006722
#> [85] 1.6800267 0.9006722 2.3897589 1.6800267 0.9006722 3.6782702
#> [91] 0.9006722 0.0000000 1.6800267 0.9006722 0.0000000 3.6782702
#> [97] 2.3897589 2.3897589 1.6800267 1.6800267
#> attr(,"skew")
#> [1] 1.567888e-08
#> attr(,"lambda")
#> [1] 0.7253537
rpois(100, 3) |> opt_bc(c(-3, 5)) ## Redefining interval avoids warning
#> [1] 0.0000000 0.9190893 0.0000000 2.4963116 2.4963116 3.9004999
#> [7] 0.0000000 4.5620315 0.9190893 2.4963116 2.4963116 0.0000000
#> [13] 1.7373471 2.4963116 1.7373471 1.7373471 0.9190893 -1.2835173
#> [19] 4.5620315 0.0000000 2.4963116 3.2140286 3.2140286 1.7373471
#> [25] 3.2140286 -1.2835173 0.0000000 0.0000000 2.4963116 0.9190893
#> [31] 0.0000000 0.0000000 3.9004999 0.9190893 2.4963116 0.9190893
#> [37] 1.7373471 3.2140286 0.0000000 0.9190893 0.9190893 -1.2835173
#> [43] 3.2140286 0.9190893 2.4963116 2.4963116 1.7373471 4.5620315
#> [49] 0.9190893 0.0000000 2.4963116 -1.2835173 2.4963116 0.0000000
#> [55] -1.2835173 2.4963116 3.2140286 3.9004999 0.9190893 0.0000000
#> [61] 1.7373471 2.4963116 -1.2835173 2.4963116 0.9190893 -1.2835173
#> [67] 0.0000000 2.4963116 1.7373471 0.9190893 0.9190893 2.4963116
#> [73] 2.4963116 3.9004999 1.7373471 1.7373471 0.0000000 1.7373471
#> [79] 3.2140286 1.7373471 3.2140286 1.7373471 2.4963116 0.9190893
#> [85] 3.2140286 2.4963116 0.9190893 0.0000000 0.0000000 2.4963116
#> [91] 0.9190893 0.0000000 4.5620315 1.7373471 0.0000000 1.7373471
#> [97] 0.0000000 3.9004999 -1.2835173 0.0000000
#> attr(,"skew")
#> [1] 8.504711e-09
#> attr(,"lambda")
#> [1] 0.7791091