Short introduction to TLMoments

2019-12-04

TLMoments is a set of functions whose main functionality is the calculation of trimmed L-moments (TL-moments) and resulting estimates of distribution parameters and quantiles.
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++ using Rcpp (see vignette “comparison of computation time” for speed comparisons). The package expands the combinations of trimmings that can be used to estimate distribution parameters in comparison to existing packages (which currently mainly support 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 how to use them.

library(TLMoments)
sessionInfo()
## R version 3.6.1 (2019-07-05)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 18.04.3 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
## 
## locale:
##  [1] LC_CTYPE=de_DE.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=de_DE.UTF-8        LC_COLLATE=C              
##  [5] LC_MONETARY=de_DE.UTF-8    LC_MESSAGES=de_DE.UTF-8   
##  [7] LC_PAPER=de_DE.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] lmom_2.8        Lmoments_1.3-1  lmomco_2.3.2    TLMoments_0.7.5
## [5] Rcpp_1.0.3     
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.23   MASS_7.3-51.4   magrittr_1.5    evaluate_0.14  
##  [5] rlang_0.4.2     stringi_1.4.3   goftest_1.2-2   rmarkdown_1.18 
##  [9] tools_3.6.1     stringr_1.4.0   xfun_0.11       yaml_2.2.0     
## [13] compiler_3.6.1  htmltools_0.4.0 evd_2.3-3       knitr_1.26

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 
## 13.307373  4.265298  1.584106  1.134269 
## 
## $ratios
##        T1        T2        T3        T4 
##        NA 0.3205214 0.3713941 0.2659297
TLMoments(xvec, leftrim = 0, rightrim = 1, max.order = 2)
## $lambdas
##       L1       L2 
## 9.042076 2.010893 
## 
## $ratios
##        T1        T2 
##        NA 0.2223929

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

tlm <- TLMoments(xvec)
parameters(tlm, distr = "gev")
##       loc     scale     shape 
## 9.0838160 4.3178244 0.2921094
tlm <- TLMoments(xvec, rightrim = 1)
parameters(tlm, distr = "gev")
##       loc     scale     shape 
## 9.1014184 4.3386328 0.2817051

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 
##  22.82589  50.96551 105.47141
tlm <- TLMoments(xvec, rightrim = 1)
quantiles(parameters(tlm, distr = "gev"), c(.9, .99, .999))
##       0.9      0.99     0.999 
##  22.73195  49.98010 101.49850

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 90% confidence interval of TL moments: 
##             LCL  lambda_hat         UCL
## L1 -0.468198066 -0.31421388 -0.16022969
## L2  0.346630661  0.40815981  0.46968896
## L3  0.009205845  0.04162182  0.07403779
## L4  0.029321128  0.04988760  0.07045408
## Approximate 90% confidence interval of TL moment ratios: 
##            LCL    tau_hat        UCL
## T2 -2.05558661 -1.2989872 -0.5423878
## T3  0.02807478  0.1019743  0.1758738
## T4  0.07128153  0.1222257  0.1731698
summary(parameters(tlm, "gev"))
## 1 data row(s) with n = 100.
## TL(0,1) used to generate GEV parameters. 
## 
## Approximate 90% confidence interval of parameters: 
##               LCL      param         UCL
## loc   -0.44063458 -0.2677310 -0.09482748
## scale  0.76992029  0.9095193  1.04911838
## shape  0.02116238  0.1819059  0.34264946
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 90% confidence interval of quantiles: 
##           LCL quantile     UCL
## 0.99 3.476975 6.276963 9.07695

The default confidence interval level is 90%, but it can be set using the argument ci.level. The argument select can be used to subset the results, which can be handy when analysing large data matrices.

summary(tlm, ci.level = .95, select = 3:4)
## 1 data row(s) with n = 100.
## TL(0,1) calculated. 
## 
## Approximate 95% confidence interval of TL moments: 
##            LCL lambda_hat        UCL
## L3 0.002995804 0.04162182 0.08024783
## L4 0.025381136 0.04988760 0.07439407
## Approximate 95% confidence interval of TL moment ratios: 
##           LCL   tau_hat       UCL
## T3 0.03416894 0.1222257 0.2102824
## T4 0.06152198 0.1222257 0.1829293
summary(parameters(tlm, "gev"), select = "shape")
## 1 data row(s) with n = 100.
## TL(0,1) used to generate GEV parameters. 
## 
## Approximate 90% confidence interval of parameters: 
##              LCL     param       UCL
## shape 0.02116238 0.1819059 0.3426495

At the moment, the summary functions do 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)) %>% 
  summary()
## 1 data row(s) with n = 100.
## TL(0,1) used to generate GEV parameters to calculate 0.99, 0.999 quantile estimates. 
## 
## Approximate 90% confidence interval of quantiles: 
##            LCL quantile       UCL
## 0.99  30.63574  49.9801  69.32445
## 0.999 32.57021 101.4985 170.42678

In the following this syntax is used for a clearer presentation.

Support for different data types

The 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.084747883  0.474594690  0.015476185  0.001516841 
## 
## $ratios
##           T1           T2           T3           T4 
##           NA -5.600077228  0.032609268  0.003196077
TLMoments(xmat, leftrim = 0, rightrim = 1)
## $lambdas
##             [,1]        [,2]         [,3]        [,4]
## L1 -1.503563e-01 -0.18686268 -0.084747883 -0.40541839
## L2  3.730287e-01  0.44000730  0.474594690  0.46031111
## L3 -1.275474e-02  0.05751490  0.015476185  0.02442167
## L4  3.781969e-05  0.05301803  0.001516841  0.04717643
## 
## $ratios
##             [,1]       [,2]         [,3]        [,4]
## T1            NA         NA           NA          NA
## T2 -2.4809653723 -2.3547094 -5.600077228 -1.13539770
## T3 -0.0341923736  0.1307135  0.032609268  0.05305471
## T4  0.0001013855  0.1204935  0.003196077  0.10248814
TLMoments(xlist, leftrim = 0, rightrim = 1)
## $lambdas
## $lambdas[[1]]
##            L1            L2            L3            L4 
## -1.503563e-01  3.730287e-01 -1.275474e-02  3.781969e-05 
## 
## $lambdas[[2]]
##          L1          L2          L3          L4 
## -0.18686268  0.44000730  0.05751490  0.05301803 
## 
## $lambdas[[3]]
##           L1           L2           L3           L4 
## -0.084747883  0.474594690  0.015476185  0.001516841 
## 
## $lambdas[[4]]
##          L1          L2          L3          L4 
## -0.40541839  0.46031111  0.02442167  0.04717643 
## 
## 
## $ratios
## $ratios[[1]]
##            T1            T2            T3            T4 
##            NA -2.4809653723 -0.0341923736  0.0001013855 
## 
## $ratios[[2]]
##         T1         T2         T3         T4 
##         NA -2.3547094  0.1307135  0.1204935 
## 
## $ratios[[3]]
##           T1           T2           T3           T4 
##           NA -5.600077228  0.032609268  0.003196077 
## 
## $ratios[[4]]
##          T1          T2          T3          T4 
##          NA -1.13539770  0.05305471  0.10248814
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1)
## $lambdas
##   station          L1        L2          L3           L4
## 1       1 -0.15035626 0.3730287 -0.01275474 3.781969e-05
## 2       2 -0.18686268 0.4400073  0.05751490 5.301803e-02
## 3       3 -0.08474788 0.4745947  0.01547619 1.516841e-03
## 4       4 -0.40541839 0.4603111  0.02442167 4.717643e-02
## 
## $ratios
##   station        T2          T3           T4
## 1       1 -2.480965 -0.03419237 0.0001013855
## 2       2 -2.354709  0.13071352 0.1204935269
## 3       3 -5.600077  0.03260927 0.0031960771
## 4       4 -1.135398  0.05305471 0.1024881385

This holds when parameter and quantile estimations are calculated:

TLMoments(xvec, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
##        loc      scale      shape 
## 0.03184944 1.09508895 0.02706016
TLMoments(xmat, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
##               [,1]       [,2]       [,3]        [,4]
## loc   -0.008700524 -0.1596152 0.03184944 -0.31061413
## scale  0.876470414  0.9621488 1.09508895  1.05298286
## shape -0.131932885  0.2432288 0.02706016  0.07374234
TLMoments(xlist, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
## [[1]]
##          loc        scale        shape 
## -0.008700524  0.876470414 -0.131932885 
## 
## [[2]]
##        loc      scale      shape 
## -0.1596152  0.9621488  0.2432288 
## 
## [[3]]
##        loc      scale      shape 
## 0.03184944 1.09508895 0.02706016 
## 
## [[4]]
##         loc       scale       shape 
## -0.31061413  1.05298286  0.07374234
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
##   station          loc     scale       shape
## 1       1 -0.008700524 0.8764704 -0.13193288
## 2       2 -0.159615246 0.9621488  0.24322876
## 3       3  0.031849443 1.0950890  0.02706016
## 4       4 -0.310614132 1.0529829  0.07374234
TLMoments(xvec, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>% 
  quantiles(c(.99, .999))
##     0.99    0.999 
## 5.396388 8.348993
TLMoments(xmat, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>%
  quantiles(c(.99, .999))
##           [,1]      [,2]     [,3]     [,4]
## 0.99  3.013791  7.994936 5.396388 5.456194
## 0.999 3.963972 17.110286 8.348993 9.173942
TLMoments(xlist, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>% 
  quantiles(c(.99, .999))
## [[1]]
##     0.99    0.999 
## 3.013791 3.963972 
## 
## [[2]]
##      0.99     0.999 
##  7.994936 17.110286 
## 
## [[3]]
##     0.99    0.999 
## 5.396388 8.348993 
## 
## [[4]]
##     0.99    0.999 
## 5.456194 9.173942
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>% 
  quantiles(c(.99, .999))
##   station     0.99     0.999
## 1       1 3.013791  3.963972
## 2       2 7.994936 17.110286
## 3       3 5.396388  8.348993
## 4       4 5.456194  9.173942

Distributions

TLMoments offers distribution functions (cdf, pdf, 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.

data <- matrix(rgev(25 * 10, shape = .2), ncol = 10)
plot(TLMoments(data))

plot(TLMoments(data)) + ggplot2::theme_minimal()

plot(TLMoments(data, rightrim = 1), distr = c("gev", "gpd", "exp", "gum"))

Calculations using theoretical/given TL-moments and parameters

The functions as.TLMoments and as.parameters can be used to construct TLMoments- or parameters-objects of given 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.