[R] Computing skewness and kurtosis with the moments package

From: Roger Leigh <rleigh_at_whinlatter.ukfsn.org>
Date: Fri 08 Sep 2006 - 09:41:00 GMT


Hi,

I'm a newcomer to R, having previously used SPSS. One problem I have run into is computing kurtosis. A test dataset is here:

http://www.whinlatter.ukfsn.org/2401.dat

> library(moments)
> data <- read.table("2401.dat", header=T)
> attach(data)
> loglen <- log10(Length)

With SPSS, I get
  Skewness -0.320
  Kurtosis -1.138

With R:
> skewness(loglen)

[1] -0.317923
> kurtosis(loglen)

[1] 1.860847

Using the example skew and kurtosis functions from M. J. Crawley's "Statistics: An introduction using R": pp 69 and 72:

> mskew(loglen)

[1] -0.3158337
> mkurtosis(loglen)

[1] -1.155441

The kurtosis value here matches the SPSS calculation somewhat more closely, but is still not exactly the same.

Looking at the functions, there is some difference between them:

> skewness

function (x, na.rm = FALSE)
{

    if (is.matrix(x))

        apply(x, 2, skewness, na.rm = na.rm)     else if (is.vector(x)) {

        if (na.rm) 
            x <- x[!is.na(x)]
        n <- length(x)
        (sum((x - mean(x))^3)/n)/(sum((x - mean(x))^2)/n)^(3/2)
    }
    else if (is.data.frame(x))

        sapply(x, skewness, na.rm = na.rm)     else skewness(as.vector(x), na.rm = na.rm) }
> mskew

function(x) {
m3 <- sum((x - mean(x))^3)/length(x)
s3 <- sqrt(var(x))^3
m3/s3
}
> kurtosis

function (x, na.rm = FALSE)
{

    if (is.matrix(x))

        apply(x, 2, kurtosis, na.rm = na.rm)     else if (is.vector(x)) {

        if (na.rm) 
            x <- x[!is.na(x)]
        n <- length(x)
        n * sum((x - mean(x))^4)/(sum((x - mean(x))^2)^2)
    }
    else if (is.data.frame(x))

        sapply(x, kurtosis, na.rm = na.rm)     else kurtosis(as.vector(x), na.rm = na.rm) }
> mkurtosis

function(x) {
m4 <- sum((x - mean(x))^4)/length(x)
s4 <- var(x)^2
m4/s4 - 3
}

Are any of these functions incorrect, or are there several different methods of computing the skew and kurtosis values?

Are there any more appropriate R packages I should consider using?

Many thanks,
Roger

-- 
  .''`.  Roger Leigh
 : :' :  Debian GNU/Linux             http://people.debian.org/~rleigh/
 `. `'   Printing on GNU/Linux?       http://gutenprint.sourceforge.net/
   `-    GPG Public Key: 0x25BFB848   Please GPG sign your mail.

______________________________________________ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

  • application/pgp-signature attachment: stored
Received on Fri Sep 08 19:45:58 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 09 Sep 2006 - 00:30:04 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.