shabbychef / fromo

Fast Robust Moments in R with Rcpp
3 stars 1 forks source link

Alternative skew calculations #20

Closed doug-friedman closed 7 years ago

doug-friedman commented 7 years ago

Any chance you'd include an option for alternative skew calculations similar to e1071's skewness function? I know it's not high on the priority list, but it'd be a real nice to have.

shabbychef commented 7 years ago

e1071 computes skew as the moments package does, but with an alternative scaling. You can achieve the 'type 3' scaling by multiplying by ((n-1)/n)^(3/2), as shown here:

library(fromo)
library(e1071)
library(moments)

set.seed(1234)
x <- rnorm(1000)
resu <- fromo::skew4(x)

# matches moments::skewness
msku <- moments::skewness(x)
stopifnot(abs(msku - resu[1]) < 1e-14)

# e1071 uses a different normalizing constant
esku <- e1071::skewness(x,type=3)
stopifnot(abs(esku - resu[1] * ((resu[4]-1)/(resu[4]))^(3/2)) < 1e-14)

There are unit tests in fromo to ensure that it's skew4 at least matches that of the moments package.

doug-friedman commented 7 years ago

Gotcha. Thanks!

doug-friedman commented 7 years ago

P.S. This library is amazingly fast! Thanks again for the prompt reply!

shabbychef commented 7 years ago

This is a good reminder for me that the documentation should leave zero ambiguity about what is being computed. I will close this one and open a doc issue for that. Glad you find the package useful.