[R] Skewness and Kurtosis

Diethelm Wuertz wuertz at itp.phys.ethz.ch
Thu Oct 28 01:44:10 CEST 2004


Hello,

fBasics has functions for skewness and kurtosis:
Comments on these functions are welcome!

Diethelm Wuertz


# **********************************************************


kurtosis =
function (x, ...)
{   # A function implemented by D. Wuertz

    # FUNCTION:
   
    UseMethod("kurtosis")
}


# 
------------------------------------------------------------------------------


kurtosis.default =
function (x, na.rm = FALSE, ...)
{   # A function implemented by D. Wuertz
 
    # Description:
    #   Returns the value of the kurtosis of a
    #   distribution function. Missing values
    #   can be handled.
   
    # FUNCTION:
   
    # Warnings:
    if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
        warning("argument is not numeric or logical: returning NA")
        return(as.numeric(NA))}
       
    # Remove NAs:
    if (na.rm) x = x[!is.na(x)]

    # Kurtosis:
    n = length(x)
    if (is.integer(x)) x = as.numeric(x)
    kurtosis = sum((x-mean(x))^4/var(x)^2)/length(x) - 3
   
    # Return Value:
    kurtosis 
}


# 
------------------------------------------------------------------------------


kurtosis.data.frame =
function (x, ...)
{   # A function implemented by D. Wuertz

    sapply(x, kurtosis, ...)
}



# 
******************************************************************************


skewness =
function (x, ...)
{   # A function implemented by D. Wuertz

    UseMethod("skewness")
}


# 
------------------------------------------------------------------------------


skewness.default =
function (x, na.rm = FALSE, ...)
{   # A function implemented by D. Wuertz
 
    # Description:
    #   Returns the value of the skewness of a
    #   distribution function. Missing values
    #   can be handled.
   
    # FUNCTION:
   
    # Warnings:
    if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
        warning("argument is not numeric or logical: returning NA")
        return(as.numeric(NA))}
       
    # Remove NAs:
    if (na.rm) x = x[!is.na(x)]

    # Skewness:
    n = length(x)
    if (is.integer(x)) x = as.numeric(x)
    skewness = sum((x-mean(x))^3/sqrt(var(x))^3)/length(x)
   
    # Return Value:
    skewness 
}


# 
------------------------------------------------------------------------------


skewness.data.frame =
function (x, ...)
{   # A function implemented by D. Wuertz

    sapply(x, skewness, ...)
}



Vito Ricci wrote:

>Hi,
>
>in which R-package I could find skewness and kurtosis
>measures for a distribution?
>
>I built some functions:
>
>gamma1<-function(x)
>{
>m=mean(x)
>n=length(x)
>s=sqrt(var(x))
>m3=sum((x-m)^3)/n
>g1=m3/(s^3)
>return(g1)
>}
>
>skewness<-function(x)
>{
>m=mean(x)
>me=median(x)
>s=sqrt(var(x))
>sk=(m-me)/s
>return(sk)
>}
>
>bowley<-function(x)
>{
>q<-as.vector(quantile(x,prob=c(.25,.50,.75)))
>b=(q[3]+q[1]-2*q[2])/(q[3]-q[2])
>return(b)
>}
>
>b3<-function(x)
>{
>m=mean(x)
>me=median(x)
>n=length(x)
>d=sum(abs(x-me))/n
>b=(m-me)/d
>return(b)
>}
>
>but I'm looking for some already included in a
>package.
>
>Thanks in advance.
>Best
>Vito
>
>
>=====
>Diventare costruttori di soluzioni
>
>"The business of the statistician is to catalyze 
>the scientific learning process."  
>George E. P. Box
>
>
>Visitate il portale http://www.modugno.it/
>e in particolare la sezione su Palese http://www.modugno.it/archivio/cat_palese.shtml
>
>______________________________________________
>R-help at 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
>
>  
>




More information about the R-help mailing list