Short introduction to TLMoments

2021-03-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 4.0.3 (2020-10-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.2 LTS
## 
## Matrix products: default
## BLAS:   /usr/lib/x86_64-linux-gnu/openblas-pthread/libblas.so.3
## LAPACK: /usr/lib/x86_64-linux-gnu/openblas-pthread/liblapack.so.3
## 
## 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.6      TLMoments_0.7.5.2
## [5] Rcpp_1.0.5       
## 
## loaded via a namespace (and not attached):
##  [1] digest_0.6.27   MASS_7.3-53     magrittr_2.0.1  evaluate_0.14  
##  [5] rlang_0.4.9     stringi_1.5.3   goftest_1.2-2   rmarkdown_2.5  
##  [9] tools_4.0.3     stringr_1.4.0   xfun_0.19       yaml_2.2.1     
## [13] compiler_4.0.3  htmltools_0.5.0 evd_2.3-3       knitr_1.30

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.833255  4.675748  1.249194  0.863840 
## 
## $ratios
##        T1        T2        T3        T4 
##        NA 0.3152207 0.2671645 0.1847490
TLMoments(xvec, leftrim = 0, rightrim = 1, max.order = 2)
## $lambdas
##        L1        L2 
## 10.157506  2.569916 
## 
## $ratios
##        T1        T2 
##        NA 0.2530066

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 
## 10.5272433  5.7779131  0.1465354
tlm <- TLMoments(xvec, rightrim = 1)
parameters(tlm, distr = "gev")
##        loc      scale      shape 
## 10.4596694  5.7336716  0.1775593

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 
## 25.92993 48.46836 79.59035
tlm <- TLMoments(xvec, rightrim = 1)
quantiles(parameters(tlm, distr = "gev"), c(.9, .99, .999))
##      0.9     0.99    0.999 
## 26.32093 51.25183 88.25322

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.25335519 -0.092447217 0.06846075
## L2  0.36829912  0.415599121 0.46289912
## L3 -0.03350809 -0.005876476 0.02175513
## L4  0.01265855  0.031485200 0.05031185
## Approximate 90% confidence interval of TL moment ratios: 
##             LCL     tau_hat        UCL
## T2 -12.44298010 -4.49552876 3.45192259
## T3  -0.08074150 -0.01413977 0.05246196
## T4   0.02793487  0.07575858 0.12358229
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.1364830  0.04839260 0.23326823
## scale  0.8422698  0.97283876 1.10340772
## shape -0.2455817 -0.08313226 0.07931718
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 2.532227 3.767291 5.002356

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.038801570 -0.005876476 0.02704862
## L4  0.009051862  0.031485200 0.05391854
## Approximate 95% confidence interval of TL moment ratios: 
##             LCL    tau_hat       UCL
## T3 -0.003602279 0.07575858 0.1551194
## T4  0.018773116 0.07575858 0.1327440
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.2455817 -0.08313226 0.07931718

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  33.87560 51.25183  68.62805
## 0.999 36.72897 88.25322 139.77747

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.19269767  0.44464019  0.01408278  0.02403312 
## 
## $ratios
##          T1          T2          T3          T4 
##          NA -2.30744975  0.03167230  0.05405072
TLMoments(xmat, leftrim = 0, rightrim = 1)
## $lambdas
##          [,1]        [,2]        [,3]         [,4]
## L1 0.33049401 -0.47895312 -0.19269767 -0.101899802
## L2 0.57619815  0.33669420  0.44464019  0.400527383
## L3 0.03339335  0.05358771  0.01408278 -0.031092575
## L4 0.02707849  0.06532183  0.02403312 -0.004447998
## 
## $ratios
##          [,1]       [,2]        [,3]        [,4]
## T1         NA         NA          NA          NA
## T2 1.74344506 -0.7029795 -2.30744975 -3.93060021
## T3 0.05795464  0.1591584  0.03167230 -0.07762909
## T4 0.04699510  0.1940094  0.05405072 -0.01110535
TLMoments(xlist, leftrim = 0, rightrim = 1)
## $lambdas
## $lambdas[[1]]
##         L1         L2         L3         L4 
## 0.33049401 0.57619815 0.03339335 0.02707849 
## 
## $lambdas[[2]]
##          L1          L2          L3          L4 
## -0.47895312  0.33669420  0.05358771  0.06532183 
## 
## $lambdas[[3]]
##          L1          L2          L3          L4 
## -0.19269767  0.44464019  0.01408278  0.02403312 
## 
## $lambdas[[4]]
##           L1           L2           L3           L4 
## -0.101899802  0.400527383 -0.031092575 -0.004447998 
## 
## 
## $ratios
## $ratios[[1]]
##         T1         T2         T3         T4 
##         NA 1.74344506 0.05795464 0.04699510 
## 
## $ratios[[2]]
##         T1         T2         T3         T4 
##         NA -0.7029795  0.1591584  0.1940094 
## 
## $ratios[[3]]
##          T1          T2          T3          T4 
##          NA -2.30744975  0.03167230  0.05405072 
## 
## $ratios[[4]]
##          T1          T2          T3          T4 
##          NA -3.93060021 -0.07762909 -0.01110535
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1)
## $lambdas
##   station         L1        L2          L3           L4
## 1       1  0.3304940 0.5761982  0.03339335  0.027078490
## 2       2 -0.4789531 0.3366942  0.05358771  0.065321830
## 3       3 -0.1926977 0.4446402  0.01408278  0.024033120
## 4       4 -0.1018998 0.4005274 -0.03109258 -0.004447998
## 
## $ratios
##   station         T2          T3          T4
## 1       1  1.7434451  0.05795464  0.04699510
## 2       2 -0.7029795  0.15915841  0.19400937
## 3       3 -2.3074497  0.03167230  0.05405072
## 4       4 -3.9306002 -0.07762909 -0.01110535

This holds when parameter and quantile estimations are calculated:

TLMoments(xvec, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
##         loc       scale       shape 
## -0.08264292  1.02634353  0.02489929
TLMoments(xmat, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
##             [,1]       [,2]        [,3]        [,4]
## loc   0.44374064 -0.4748555 -0.08264292  0.08621523
## scale 1.31507868  0.7208620  1.02634353  0.94377460
## shape 0.08479819  0.3023811  0.02489929 -0.24097347
TLMoments(xlist, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
## [[1]]
##        loc      scale      shape 
## 0.44374064 1.31507868 0.08479819 
## 
## [[2]]
##        loc      scale      shape 
## -0.4748555  0.7208620  0.3023811 
## 
## [[3]]
##         loc       scale       shape 
## -0.08264292  1.02634353  0.02489929 
## 
## [[4]]
##         loc       scale       shape 
##  0.08621523  0.94377460 -0.24097347
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1) %>% 
  parameters("gev")
##   station         loc     scale       shape
## 1       1  0.44374064 1.3150787  0.08479819
## 2       2 -0.47485547 0.7208620  0.30238113
## 3       3 -0.08264292 1.0263435  0.02489929
## 4       4  0.08621523 0.9437746 -0.24097347
TLMoments(xvec, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>% 
  quantiles(c(.99, .999))
##     0.99    0.999 
## 4.919708 7.652700
TLMoments(xmat, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>%
  quantiles(c(.99, .999))
##           [,1]      [,2]     [,3]     [,4]
## 0.99   7.84285  6.721962 4.919708 2.710084
## 0.999 12.79285 16.388730 7.652700 3.261360
TLMoments(xlist, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>% 
  quantiles(c(.99, .999))
## [[1]]
##     0.99    0.999 
##  7.84285 12.79285 
## 
## [[2]]
##      0.99     0.999 
##  6.721962 16.388730 
## 
## [[3]]
##     0.99    0.999 
## 4.919708 7.652700 
## 
## [[4]]
##     0.99    0.999 
## 2.710084 3.261360
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1) %>% 
  parameters("gev") %>% 
  quantiles(c(.99, .999))
##   station     0.99    0.999
## 1       1 7.842850 12.79285
## 2       2 6.721962 16.38873
## 3       3 4.919708  7.65270
## 4       4 2.710084  3.26136

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.