Short introduction to TLMoments

2019-08-19

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 16.04.6 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/libblas/libblas.so.3.6.0
## LAPACK: /usr/lib/lapack/liblapack.so.3.6.0
## 
## 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.4.4
## [5] Rcpp_1.0.2       
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.20   MASS_7.3-51.1   magrittr_1.5    evaluate_0.14  
##  [5] stringi_1.4.3   goftest_1.1-1   rmarkdown_1.14  tools_3.6.1    
##  [9] stringr_1.4.0   xfun_0.8        yaml_2.2.0      compiler_3.6.1 
## [13] htmltools_0.3.6 evd_2.3-3       knitr_1.24

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.4350230  3.8329276  1.0524561  0.8707856 
## 
## $ratios
##        T1        T2        T3        T4 
##        NA 0.2852937 0.2745828 0.2271855
TLMoments(xvec, leftrim = 0, rightrim = 1, max.order = 2)
## $lambdas
##       L1       L2 
## 9.602095 2.085354 
## 
## $ratios
##        T1        T2 
##        NA 0.2171769

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.8830614 4.6751772 0.1572745
tlm <- TLMoments(xvec, rightrim = 1)
parameters(tlm, distr = "gev")
##         loc       scale       shape 
## 10.01145889  4.75920227  0.08507644

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.50627 41.44065 68.24733
tlm <- TLMoments(xvec, rightrim = 1)
quantiles(parameters(tlm, distr = "gev"), c(.9, .99, .999))
##      0.9     0.99    0.999 
## 21.81532 36.80671 54.74941

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.392575620 -0.23478483 -0.07699404
## L2  0.351945944  0.41439350  0.47684106
## L3 -0.000678173  0.03139597  0.06347011
## L4  0.040330797  0.05987723  0.07942365
## Approximate 90% confidence interval of TL moment ratios: 
##             LCL     tau_hat        UCL
## T2 -3.082761009 -1.76499266 -0.4472243
## T3  0.003362454  0.07576366  0.1481649
## T4  0.095115889  0.14449364  0.1938714
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.34548645 -0.1673744 0.01073773
## scale  0.79848501  0.9373650 1.07624501
## shape -0.03516084  0.1245602 0.28428127
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.301384 5.654061 8.006739

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.006822729 0.03139597 0.06961467
## L4  0.036586219 0.05987723 0.08316823
## Approximate 95% confidence interval of TL moment ratios: 
##           LCL   tau_hat       UCL
## T3 0.05822228 0.1444936 0.2307650
## T4 0.08565642 0.1444936 0.2033309
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.03516084 0.1245602 0.2842813

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  26.38807 36.80671 47.22535
## 0.999 28.19215 54.74941 81.30667

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.046573137 0.336121123 0.027048826 0.003654516 
## 
## $ratios
##         T1         T2         T3         T4 
##         NA 7.21706002 0.08047345 0.01087262
TLMoments(xmat, leftrim = 0, rightrim = 1)
## $lambdas
##          [,1]        [,2]        [,3]        [,4]
## L1 0.05819231  0.09958952 0.046573137  0.28281816
## L2 0.48490596  0.55574749 0.336121123  0.54520359
## L3 0.04662357 -0.09601736 0.027048826 -0.02602346
## L4 0.01538407  0.08638490 0.003654516  0.02517979
## 
## $ratios
##          [,1]       [,2]       [,3]        [,4]
## T1         NA         NA         NA          NA
## T2 8.33281914  5.5803814 7.21706002  1.92775311
## T3 0.09614971 -0.1727716 0.08047345 -0.04773164
## T4 0.03172589  0.1554391 0.01087262  0.04618420
TLMoments(xlist, leftrim = 0, rightrim = 1)
## $lambdas
## $lambdas[[1]]
##         L1         L2         L3         L4 
## 0.05819231 0.48490596 0.04662357 0.01538407 
## 
## $lambdas[[2]]
##          L1          L2          L3          L4 
##  0.09958952  0.55574749 -0.09601736  0.08638490 
## 
## $lambdas[[3]]
##          L1          L2          L3          L4 
## 0.046573137 0.336121123 0.027048826 0.003654516 
## 
## $lambdas[[4]]
##          L1          L2          L3          L4 
##  0.28281816  0.54520359 -0.02602346  0.02517979 
## 
## 
## $ratios
## $ratios[[1]]
##         T1         T2         T3         T4 
##         NA 8.33281914 0.09614971 0.03172589 
## 
## $ratios[[2]]
##         T1         T2         T3         T4 
##         NA  5.5803814 -0.1727716  0.1554391 
## 
## $ratios[[3]]
##         T1         T2         T3         T4 
##         NA 7.21706002 0.08047345 0.01087262 
## 
## $ratios[[4]]
##          T1          T2          T3          T4 
##          NA  1.92775311 -0.04773164  0.04618420
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1)
## $lambdas
##   station         L1        L2          L3          L4
## 1       1 0.05819231 0.4849060  0.04662357 0.015384074
## 2       2 0.09958952 0.5557475 -0.09601736 0.086384899
## 3       3 0.04657314 0.3361211  0.02704883 0.003654516
## 4       4 0.28281816 0.5452036 -0.02602346 0.025179793
## 
## $ratios
##   station       T2          T3         T4
## 1       1 8.332819  0.09614971 0.03172589
## 2       2 5.580381 -0.17277157 0.15543912
## 3       3 7.217060  0.08047345 0.01087262
## 4       4 1.927753 -0.04773164 0.04618420

This holds when parameter and quantile estimations are calculated:

TLMoments(xvec, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
##        loc      scale      shape 
## 0.09827229 0.75838612 0.13496667
TLMoments(xmat, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
##            [,1]       [,2]       [,3]       [,4]
## loc   0.1186181  0.4719949 0.09827229  0.5050355
## scale 1.0843450  1.2819166 0.75838612  1.2831648
## shape 0.1692812 -0.4970545 0.13496667 -0.1654239
TLMoments(xlist, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
## [[1]]
##       loc     scale     shape 
## 0.1186181 1.0843450 0.1692812 
## 
## [[2]]
##        loc      scale      shape 
##  0.4719949  1.2819166 -0.4970545 
## 
## [[3]]
##        loc      scale      shape 
## 0.09827229 0.75838612 0.13496667 
## 
## [[4]]
##        loc      scale      shape 
##  0.5050355  1.2831648 -0.1654239
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
##   station        loc     scale      shape
## 1       1 0.11861808 1.0843450  0.1692812
## 2       2 0.47199492 1.2819166 -0.4970545
## 3       3 0.09827229 0.7583861  0.1349667
## 4       4 0.50503555 1.2831648 -0.1654239
TLMoments(xvec, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>% 
  quantiles(c(.99, .999))
##     0.99    0.999 
## 4.933701 8.752843
TLMoments(xmat, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>%
  quantiles(c(.99, .999))
##            [,1]     [,2]     [,3]     [,4]
## 0.99   7.668746 2.788943 4.933701 4.637790
## 0.999 14.336684 2.967768 8.752843 5.787586
TLMoments(xlist, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>% 
  quantiles(c(.99, .999))
## [[1]]
##      0.99     0.999 
##  7.668746 14.336684 
## 
## [[2]]
##     0.99    0.999 
## 2.788943 2.967768 
## 
## [[3]]
##     0.99    0.999 
## 4.933701 8.752843 
## 
## [[4]]
##     0.99    0.999 
## 4.637790 5.787586
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>% 
  quantiles(c(.99, .999))
##   station     0.99     0.999
## 1       1 7.668746 14.336684
## 2       2 2.788943  2.967768
## 3       3 4.933701  8.752843
## 4       4 4.637790  5.787586

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.