1
2# This R package is free software; you can redistribute it and/or
3# modify it under the terms of the GNU Library General Public
4# License as published by the Free Software Foundation; either
5# version 2 of the License, or (at your option) any later version.
6#
7# This R package is distributed in the hope that it will be useful,
8# but WITHOUT ANY WARRANTY; without even the implied warranty of
9# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10# GNU Library General Public License for more details.
11#
12# You should have received a copy of the GNU Library General
13# Public License along with this R package; if not, write to the
14# Free Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15# MA  02111-1307  USA
16
17
18################################################################################
19# FUNCTION:                 DESCRIPTION:
20#  skewness                  Returns a number which is the skewness of the data
21#  skewness.default          Default method
22#  skewness.data.frame       Method for objects of class data.frame
23#  skewness.POSIXct          Method for objects of class POSIXct
24#  skewness.POSIXlt          Method for objects of class POSIXlt
25################################################################################
26
27# ---------------------------------------------------------------------------- #
28# Roxygen Tags
29#' @export
30# ---------------------------------------------------------------------------- #
31skewness <-
32    function (x, ...)
33{
34    # A function implemented by Diethelm Wuertz
35
36    # FUNCTION:
37
38    # Return Value:
39    UseMethod("skewness")
40}
41
42
43# ---------------------------------------------------------------------------- #
44# Roxygen Tags
45#' @export
46# ---------------------------------------------------------------------------- #
47skewness.default <-
48    function (x, na.rm = FALSE, method = c("moment", "fisher"), ...)
49{
50    # A function implemented by Diethelm Wuertz
51
52    # Description:
53    #   Returns the value of the skewness of a distribution function.
54
55    # Details:
56    #   Missing values can be handled.
57
58    # FUNCTION:
59
60    # Method:
61    method = match.arg(method)
62
63    # Warnings:
64    if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
65        warning("argument is not numeric or logical: returning NA")
66        return(as.numeric(NA))}
67
68    stopifnot(NCOL(x) == 1)
69
70    # Remove NAs:
71    if (na.rm) x = x[!is.na(x)]
72
73    # Skewness:
74    n = length(x)
75    if (is.integer(x)) x = as.numeric(x)
76
77    # Selected Method:
78    if (method == "moment") {
79        skewness = sum((x-mean(x))^3/sqrt(as.numeric(var(x)))^3)/length(x)
80    }
81    if (method == "fisher") {
82        if (n < 3)
83            skewness = NA
84        else
85            skewness = ((sqrt(n*(n-1))/(n-2))*(sum(x^3)/n))/((sum(x^2)/n)^(3/2))
86    }
87
88    # Add Control Attribute:
89    attr(skewness, "method") <- method
90
91    # Return Value:
92    skewness
93}
94
95
96# ---------------------------------------------------------------------------- #
97# Roxygen Tags
98#' @export
99# ---------------------------------------------------------------------------- #
100skewness.data.frame <-
101    function (x, ...)
102{
103    # A function implemented by Diethelm Wuertz
104
105    # FUNCTION:
106
107    # Return Value:
108    sapply(x, skewness, ...)
109}
110
111
112# ---------------------------------------------------------------------------- #
113# Roxygen Tags
114#' @export
115# ---------------------------------------------------------------------------- #
116skewness.POSIXct <-
117    function (x, ...)
118{
119    # A function implemented by Diethelm Wuertz
120
121    # FUNCTION:
122
123    # Return Value:
124    structure(skewness(unclass(x), ...), oldClass(x))
125}
126
127
128# ---------------------------------------------------------------------------- #
129# Roxygen Tags
130#' @export
131# ---------------------------------------------------------------------------- #
132skewness.POSIXlt <-
133    function (x, ...)
134{
135    # A function implemented by Diethelm Wuertz
136
137    # FUNCTION:
138
139    # Return Value:
140    as.POSIXlt(skewness(as.POSIXct(x), ...))
141}
142
143
144################################################################################
145
146