1
2# This library 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 library 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 library; 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#  sampleLmoments        Computes sample L-moments
21################################################################################
22
23
24sampleLmoments <-
25function(x, rmax=4)
26{
27    # A function implemented by Diethelm Wuertz
28
29    # Description
30    #   Computes sample L-moments
31
32    # Note:
33    #   This function is borrowed from package ...
34    #   Author:
35
36    # FUNCTION:
37
38    # L Moments:
39    data <- as.matrix(x)
40    n <- dim(data)[1]
41    p <- dim(data)[2]
42    x <- array(, c(p, n))
43    L  <- array(, c(p, rmax))
44    for (i in 1:p) x[i, ] <- sort(data[, i])
45    if (rmax == 1) return(rowMeans(x))
46    bcoef <- array(, c(rmax, n))
47    bcoefm <- array(, c(rmax, p, n))
48    b <- array(, c(p, rmax))
49    bcoef[1, ] <- seq(0, 1, by = (1/(n-1)))
50    bcoefm[1, , ] <- t(array(rep(bcoef[1, ], p), c(n, p)))
51    b[, 1] <- rowMeans(x)
52    b[, 2] <- rowMeans(bcoefm[1, , ] * x)
53    L[, 1] = b[, 1]
54    if (rmax > 2) {
55        for (r in 2:(rmax-1)) {
56            rr <- r+1
57            bcoef[r, ]<-bcoef[r-1,]*seq((-(r-1)/(n-r)),1, by = (1/(n-r)))
58            bcoefm[r, , ]<-t(array(rep(bcoef[r,],p),c(n, p)))
59            b[, rr] <- rowMeans(bcoefm[r, , ]*x)
60        }
61    }
62    for (r in 1:(rmax-1)) {
63        L[, r+1] <- 0
64        for (k in 0:r) {
65            kk <- k+1
66            L[, r+1] <- L[, r+1]+(-1)^(r-k)*gamma(r+k+1) /
67                (gamma(k+1)^2) / gamma(r-k+1)*b[, kk]
68        }
69    }
70    L = as.vector(L)
71    names(L) = paste("L", 1:rmax, sep = "")
72
73    # Return Value:
74    L
75}
76
77
78################################################################################
79
80