Skip to contents

Optimise 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)

Arguments

x

a numeric vector of data to be transformed using optimised values.

interval

a vector containing the end-points of the interval to be searched for the minimum.

tol

the desired accuracy.

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.

See also

skewness

Other boxcox: boxcox3()

Examples

runif(20) |> opt_bc()
#>  [1] -0.314671546 -0.487459633 -0.351738249 -0.333842651 -0.845747595
#>  [6] -0.231712404 -0.227734177 -0.009283771 -0.029439394 -0.589327755
#> [11] -0.522696513 -0.656591404 -0.780202252 -0.456639730 -0.492348001
#> [16] -0.218320738 -0.754828147 -0.282507142 -0.870261776 -0.621304537
#> attr(,"skew")
#> [1] 1.562456e-09
#> attr(,"lambda")
#> [1] 1.090555
rlnorm(20) |> opt_bc()
#>  [1]  0.91383337  0.17571240  0.24220731  1.55941560  0.11172494 -0.13444677
#>  [7] -2.00424626 -0.28119515 -0.31591440  1.03933660  0.06991240 -0.64944211
#> [13] -0.05002735 -0.25307076  0.43988879  2.57408103  0.04647731  0.56944736
#> [19]  0.11784641 -2.00604296
#> attr(,"skew")
#> [1] 2.976226e-10
#> attr(,"lambda")
#> [1] -0.04998641
rpois(100, 3) |> opt_bc()         ## Provokes warning
#> Warning: NA/Inf replaced by maximum positive value
#>   [1]  2.3219564  2.3219564  0.8886786  0.8886786  0.8886786  2.3219564
#>   [7]  1.6431935  2.3219564  1.6431935  2.9494335  2.3219564  2.9494335
#>  [13] -1.4501483  2.9494335  5.1483239  1.6431935  0.8886786  2.3219564
#>  [19]  1.6431935  1.6431935  2.9494335  1.6431935  0.8886786  3.5388524
#>  [25]  2.9494335  0.8886786  0.8886786  2.3219564  0.0000000  0.0000000
#>  [31]  0.0000000  2.3219564  2.9494335  3.5388524  1.6431935  1.6431935
#>  [37]  2.3219564  0.8886786 -1.4501483  2.9494335  0.0000000  5.1483239
#>  [43]  1.6431935  0.0000000  2.9494335 -1.4501483  1.6431935  0.8886786
#>  [49]  2.9494335  2.9494335  2.3219564  0.8886786  2.3219564  0.8886786
#>  [55]  1.6431935  0.8886786  1.6431935  0.8886786  1.6431935  3.5388524
#>  [61]  4.0983958  2.9494335  2.9494335  1.6431935  2.3219564  4.6335743
#>  [67]  2.3219564  0.8886786  0.8886786  2.3219564  2.9494335  0.8886786
#>  [73] -1.4501483  3.5388524  1.6431935  2.3219564  2.3219564  0.8886786
#>  [79]  0.8886786  2.3219564  2.3219564  0.0000000  2.9494335  0.0000000
#>  [85]  2.9494335  0.0000000  1.6431935  0.8886786  2.3219564  0.0000000
#>  [91]  3.5388524  0.8886786  0.8886786  2.9494335  2.9494335  0.0000000
#>  [97]  2.9494335  2.3219564  0.8886786  5.6455813
#> attr(,"skew")
#> [1] 3.822212e-09
#> attr(,"lambda")
#> [1] 0.6895846
rpois(100, 3) |> opt_bc(c(-3, 5)) ## Redefining interval avoids warning
#>   [1]  0.9195554  3.9062541  0.9195554 -1.2813094  0.0000000  3.2182117
#>   [7]  0.9195554  0.9195554 -1.2813094  3.9062541  0.9195554  2.4990467
#>  [13]  0.9195554  3.2182117  0.9195554  4.5694489  1.7388097  2.4990467
#>  [19]  3.2182117  2.4990467  0.9195554  0.0000000  0.0000000  2.4990467
#>  [25]  1.7388097  2.4990467  0.9195554  4.5694489  2.4990467  3.2182117
#>  [31]  4.5694489  0.9195554  2.4990467  0.9195554 -1.2813094  1.7388097
#>  [37]  1.7388097  0.9195554  3.2182117  1.7388097  4.5694489 -1.2813094
#>  [43]  2.4990467  0.9195554  3.9062541  2.4990467  2.4990467  2.4990467
#>  [49]  1.7388097  0.9195554  1.7388097  0.9195554  2.4990467  1.7388097
#>  [55]  0.9195554  3.9062541  0.9195554  0.0000000  1.7388097  0.9195554
#>  [61]  0.0000000  3.9062541  2.4990467  2.4990467  1.7388097  1.7388097
#>  [67]  0.0000000  0.9195554  0.0000000  2.4990467  2.4990467  3.9062541
#>  [73]  0.0000000  4.5694489  0.9195554  2.4990467  2.4990467  0.0000000
#>  [79]  1.7388097  2.4990467  1.7388097  1.7388097  0.9195554 -1.2813094
#>  [85]  4.5694489  0.0000000  2.4990467  3.2182117  3.2182117  1.7388097
#>  [91]  3.2182117 -1.2813094  0.0000000  0.0000000  2.4990467  0.9195554
#>  [97]  0.0000000  0.0000000  3.9062541  0.9195554
#> attr(,"skew")
#> [1] 9.547003e-09
#> attr(,"lambda")
#> [1] 0.7804517