Short introduction to TLMoments

2019-01-02

TLMoments is a set of functions whose main functionality is the calculation of Trimmed L-moments and their parameter and quantile estimates. One of the goals is to reduce computation time compared to existing implementations (in packages like lmomco, Lmoments, Lmom), therefore the core functions are written in C++ (see vignette “comparison of computation time” for speed comparisons). Furthermore, the package expands the combinations of trimmings that can be used to estimate distribution parameters in comparison to existing packages (which mainly supports parameter estimation with L-moments). To ensure an easy usage, the package only contains a small set of functions. This vignette gives a short introduction to the most important ones and their usage.

library(TLMoments)
sessionInfo()$otherPkgs$TLMoments$Version
## [1] "0.7.4.1"

Calculation of empirical TL-moments, parameter and quantile estimates.

First we have a look at the basic functionality of calculating TL-moments and parameter and quantile estimates. Let assume we have a simple random data vector generated from a GEV distribution:

xvec <- rgev(100, loc = 10, scale = 5, shape = .2)

TL-moments are calculated by the function TLMoments with arguments leftrim, rightrim, and max.order (generating an object of class TLMoments):

TLMoments(xvec)
## $lambdas
##        L1        L2        L3        L4 
## 14.950691  4.774633  1.500572  1.060520 
## 
## $ratios
##        T1        T2        T3        T4 
##        NA 0.3193587 0.3142801 0.2221156
TLMoments(xvec, leftrim = 0, rightrim = 1, max.order = 2)
## $lambdas
##        L1        L2 
## 10.176058  2.455546 
## 
## $ratios
##        T1        T2 
##        NA 0.2413062

We can generate parameters estimates by putting a TLMoments-object to the function parameters and specifying argument distr:

tlm <- TLMoments(xvec)
parameters(tlm, distr = "gev")
##       loc     scale     shape 
## 10.389199  5.415716  0.213737
tlm <- TLMoments(xvec, rightrim = 1)
parameters(tlm, distr = "gev")
##        loc      scale      shape 
## 10.3775420  5.4108675  0.2194308

This generates an object of class parameters, which can be transmitted to quantiles to calculate quantile estimations:

tlm <- TLMoments(xvec)
quantiles(parameters(tlm, distr = "gev"), c(.9, .99, .999))
##      0.9     0.99    0.999 
## 26.03986 52.78145 95.95333
tlm <- TLMoments(xvec, rightrim = 1)
quantiles(parameters(tlm, distr = "gev"), c(.9, .99, .999))
##      0.9     0.99    0.999 
## 26.12285 53.38207 97.97605

Support for different data types:

These three functions, TLMoments, parameters, and quantiles, provide the main functionality of the package. In the code above we used single data vectors only, but the same functions can be used for data matrices, lists, and data.frames as well. To demonstrate this, let’s generate sample data of these four types:

xmat <- matrix(rgev(100), nc = 4)
xvec <- xmat[, 3]
xlist <- lapply(1L:ncol(xmat), function(i) xmat[, i])
xdat <- data.frame(station = rep(1:4, each = 25), hq = as.vector(xmat))

Note that the type of the dimensions lambdas and ratios returned by TLMoments matches the input type:

TLMoments(xvec, leftrim = 0, rightrim = 1)
## $lambdas
##          L1          L2          L3          L4 
## -0.30506767  0.37352736 -0.05144985  0.02152011 
## 
## $ratios
##         T1         T2         T3         T4 
##         NA -1.2244082 -0.1377405  0.0576132
TLMoments(xmat, leftrim = 0, rightrim = 1)
## $lambdas
##           [,1]        [,2]        [,3]        [,4]
## L1  0.06850925 -0.16359443 -0.30506767 -0.36658968
## L2  0.45251767  0.50222458  0.37352736  0.36326275
## L3 -0.00452832  0.03671644 -0.05144985  0.01562659
## L4  0.01489757  0.09911787  0.02152011  0.04295633
## 
## $ratios
##           [,1]        [,2]       [,3]        [,4]
## T1          NA          NA         NA          NA
## T2  6.60520543 -3.06993693 -1.2244082 -0.99092465
## T3 -0.01000695  0.07310761 -0.1377405  0.04301733
## T4  0.03292152  0.19735766  0.0576132  0.11825140
TLMoments(xlist, leftrim = 0, rightrim = 1)
## $lambdas
## $lambdas[[1]]
##          L1          L2          L3          L4 
##  0.06850925  0.45251767 -0.00452832  0.01489757 
## 
## $lambdas[[2]]
##          L1          L2          L3          L4 
## -0.16359443  0.50222458  0.03671644  0.09911787 
## 
## $lambdas[[3]]
##          L1          L2          L3          L4 
## -0.30506767  0.37352736 -0.05144985  0.02152011 
## 
## $lambdas[[4]]
##          L1          L2          L3          L4 
## -0.36658968  0.36326275  0.01562659  0.04295633 
## 
## 
## $ratios
## $ratios[[1]]
##          T1          T2          T3          T4 
##          NA  6.60520543 -0.01000695  0.03292152 
## 
## $ratios[[2]]
##          T1          T2          T3          T4 
##          NA -3.06993693  0.07310761  0.19735766 
## 
## $ratios[[3]]
##         T1         T2         T3         T4 
##         NA -1.2244082 -0.1377405  0.0576132 
## 
## $ratios[[4]]
##          T1          T2          T3          T4 
##          NA -0.99092465  0.04301733  0.11825140
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1)
## $lambdas
##   station          L1        L2          L3         L4
## 1       1  0.06850925 0.4525177 -0.00452832 0.01489757
## 2       2 -0.16359443 0.5022246  0.03671644 0.09911787
## 3       3 -0.30506767 0.3735274 -0.05144985 0.02152011
## 4       4 -0.36658968 0.3632628  0.01562659 0.04295633
## 
## $ratios
##   station         T2          T3         T4
## 1       1  6.6052054 -0.01000695 0.03292152
## 2       2 -3.0699369  0.07310761 0.19735766
## 3       3 -1.2244082 -0.13774052 0.05761320
## 4       4 -0.9909246  0.04301733 0.11825140

This holds when parameter and quantile estimations are calculated:

tlm <- TLMoments(xvec, leftrim = 0, rightrim = 1)
parameters(tlm, "gev")
##         loc       scale       shape 
## -0.08239945  0.87245252 -0.39986733
tlm <- TLMoments(xmat, leftrim = 0, rightrim = 1)
parameters(tlm, "gev")
##              [,1]        [,2]        [,3]        [,4]
## loc    0.21807669 -0.07937746 -0.08239945 -0.28472057
## scale  1.05824489  1.13763051  0.87245252  0.83466260
## shape -0.07319134  0.11867159 -0.39986733  0.05093618
tlm <- TLMoments(xlist, leftrim = 0, rightrim = 1)
parameters(tlm, "gev")
## [[1]]
##         loc       scale       shape 
##  0.21807669  1.05824489 -0.07319134 
## 
## [[2]]
##         loc       scale       shape 
## -0.07937746  1.13763051  0.11867159 
## 
## [[3]]
##         loc       scale       shape 
## -0.08239945  0.87245252 -0.39986733 
## 
## [[4]]
##         loc       scale       shape 
## -0.28472057  0.83466260  0.05093618
tlm <- TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1)
parameters(tlm, "gev")
##   station         loc     scale       shape
## 1       1  0.21807669 1.0582449 -0.07319134
## 2       2 -0.07937746 1.1376305  0.11867159
## 3       3 -0.08239945 0.8724525 -0.39986733
## 4       4 -0.28472057 0.8346626  0.05093618
tlm <- TLMoments(xvec, leftrim = 0, rightrim = 1)
quantiles(parameters(tlm, "gev"), c(.99, .999))
##     0.99    0.999 
## 1.752748 1.961636
tlm <- TLMoments(xmat, leftrim = 0, rightrim = 1)
quantiles(parameters(tlm, "gev"), c(.99, .999))
##           [,1]      [,2]     [,3]     [,4]
## 0.99  4.351368  6.881994 1.752748 4.041977
## 0.999 5.955643 12.093473 1.961636 6.624878
tlm <- TLMoments(xlist, leftrim = 0, rightrim = 1)
quantiles(parameters(tlm, "gev"), c(.99, .999))
## [[1]]
##     0.99    0.999 
## 4.351368 5.955643 
## 
## [[2]]
##      0.99     0.999 
##  6.881994 12.093473 
## 
## [[3]]
##     0.99    0.999 
## 1.752748 1.961636 
## 
## [[4]]
##     0.99    0.999 
## 4.041977 6.624878
tlm <- TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1)
quantiles(parameters(tlm, "gev"), c(.99, .999))
##   station     0.99     0.999
## 1       1 4.351368  5.955643
## 2       2 6.881994 12.093473
## 3       3 1.752748  1.961636
## 4       4 4.041977  6.624878

Distributions

TLMoments offers functions (distributions, density, quantile, random number generation) for the generalized extreme value distribution (gev), Gumbel distribution (gum), generalized Pareto distribution (gpd), and three-parameter lognormal distribution (ln3) in the common p|d|q|r-syntax. The parameter (and quantile) estimation functionality works for all of them, but more complex functionality like estimation of the covariance matrix of parameter or quantile estimators only works for GEV by now.

TL-moment ratio diagram

Version 0.7.4 added functionality to plot TL-moment ratio diagrams of arbitrary trimming orders. Simply plot an object of TLMoments. Argument distr can be used to specify displayed theoretical distributions. Note that ggplot2 is used. Therefore changes or additions have to be made by adding ggplot2-specific functions.

tlm <- TLMoments(xmat, leftrim = 0, rightrim = 1)
plot(tlm)

plot(tlm, distr = c("gev", "gpd", "exp", "gum"))

Calculations using theoretical TL-moments and parameters

The functions as.TLMoments and as.parameters can be used to construct TLMoments- or parameters-objects of theoretical values (not calculated from data). These objects can be used in the same way like before (to convert between TL-moments and their parameters or to calculate the corresponding quantiles):

(tlm <- as.TLMoments(c(14.1, 4.3, 1.32)))
## $lambdas
##    L1    L2    L3 
## 14.10  4.30  1.32 
## 
## $ratios
##        T1        T2        T3 
##        NA 0.3049645 0.3069767
parameters(tlm, distr = "gev")
##        loc      scale      shape 
## 10.0134305  4.9448851  0.2034746
quantiles(parameters(tlm, distr = "gev"), c(.9, .99, .999))
##      0.9     0.99    0.999 
## 24.12668 47.67693 84.80024
(param <- as.parameters(loc = 10, scale = 5, shape = .2, distr = "gev"))
##   loc scale shape 
##  10.0   5.0   0.2
quantiles(param, c(.9, .99, .999))
##      0.9     0.99    0.999 
## 24.21069 47.73413 84.51684
TLMoments(param)
## $lambdas
##         L1         L2         L3         L4 
## 14.1057429  4.3279754  1.3204343  0.9436158 
## 
## $ratios
##        T1        T2        T3        T4 
##        NA 0.3068236 0.3050928 0.2180271
TLMoments(param, rightrim = 1)
## $lambdas
##        L1        L2        L3        L4 
## 9.7777681 2.2556564 0.2512127 0.2553529 
## 
## $ratios
##        T1        T2        T3        T4 
##        NA 0.2306924 0.1113701 0.1132056

Note, that we can simply use the TLMoments-function to calculate TL-moments corresponding to a parameters-object.

Summary functions

Objects of type TLMoments, parameters, or quantiles (i.e. results from the functions of the same name) feature summary-functions, which give confidence intervals and an overview of the data.

tlm <- TLMoments(rgev(100), leftrim = 0, rightrim = 1)

summary(tlm)
## 1 data row(s) with n = 100.
## TL(0,1) calculated. 
## 
## Approximate 0.9% confidence interval of TL moments: 
##           LCL  lambda_hat        UCL
## L1 -0.3490313 -0.16126329 0.02650471
## L2  0.4248955  0.47983015 0.53476482
## L3 -0.0459109 -0.01735657 0.01119775
## L4  0.0164171  0.03962353 0.06282996
## Approximate 0.9% confidence interval of TL moment ratios: 
##            LCL     tau_hat        UCL
## T2 -6.49047182 -2.97544572 0.53958039
## T3 -0.09576752 -0.03617233 0.02342286
## T4  0.02949126  0.08257824 0.13566523
summary(tlm, ci.level = .95, select = 3:4)
## 1 data row(s) with n = 100.
## TL(0,1) calculated. 
## 
## Approximate 0.95% confidence interval of TL moments: 
##            LCL  lambda_hat       UCL
## L3 -0.05138115 -0.01735657 0.0166680
## L4  0.01197136  0.03962353 0.0672757
## Approximate 0.95% confidence interval of TL moment ratios: 
##           LCL    tau_hat       UCL
## T3 0.01156619 0.08257824 0.1535903
## T4 0.01932120 0.08257824 0.1458353
summary(parameters(tlm, "gev"))
## 1 data row(s) with n = 100.
## TL(0,1) used to generate GEV parameters. 
## 
## Approximate 0.9% confidence interval of parameters: 
##              LCL      param        UCL
## loc   -0.1915065  0.0228976 0.23730170
## scale  0.9786995  1.1277375 1.27677556
## shape -0.3015356 -0.1368030 0.02792957
summary(quantiles(parameters(tlm, "gev"), .99))
## 1 data row(s) with n = 100.
## TL(0,1) used to generate GEV parameters to calculate 0.99 quantile estimates. 
## 
## Approximate 0.9% confidence interval of quantiles: 
##           LCL quantile      UCL
## 0.99 2.653721 3.872972 5.092223

At the moment, the summary functions does not work for data in lists or data.frames.

Magrittr syntax

TLMoments is built to support the use in magrittr syntax. The nesting of functions can be written more readable as:

library(magrittr)

TLMoments(xvec, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>% 
  quantiles(c(.99, .999))
##     0.99    0.999 
## 1.752748 1.961636