Short introduction

2018-08-21

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"

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.285317  4.200885  1.273578  1.078051 
## 
## $ratios
##        T1        T2        T3        T4 
##        NA 0.2940702 0.3031690 0.2566246
TLMoments(xvec, leftrim = 0, rightrim = 1, max.order = 2)
## $lambdas
##       L1       L2 
## 10.08443  2.19548 
## 
## $ratios
##        T1        T2 
##        NA 0.2177099

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.3040897  4.8653262  0.1981018
tlm <- TLMoments(xvec, rightrim = 1)
parameters(tlm, distr = "gev")
##         loc       scale       shape 
## 10.50996766  5.00744917  0.08798852

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 
## 24.10032 46.83789 82.23518
tlm <- TLMoments(xvec, rightrim = 1)
quantiles(parameters(tlm, distr = "gev"), c(.9, .99, .999))
##      0.9     0.99    0.999 
## 22.97162 38.90495 58.10469

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.27716613  0.44331222 -0.00350671  0.07781896 
## 
## $ratios
##          T1          T2          T3          T4 
##          NA -1.59944587 -0.00791025  0.17553985
TLMoments(xmat, leftrim = 0, rightrim = 1)
## $lambdas
##             [,1]        [,2]        [,3]         [,4]
## L1 -0.2218269150 -0.29940771 -0.27716613 -0.157893866
## L2  0.4776272172  0.37431685  0.44331222  0.496007697
## L3  0.0007514416  0.02522882 -0.00350671  0.097622005
## L4  0.0347470498  0.03554614  0.07781896  0.003938307
## 
## $ratios
##            [,1]        [,2]        [,3]         [,4]
## T1           NA          NA          NA           NA
## T2 -2.153152682 -1.25019110 -1.59944587 -3.141399404
## T3  0.001573281  0.06739963 -0.00791025  0.196815504
## T4  0.072749309  0.09496271  0.17553985  0.007940013
TLMoments(xlist, leftrim = 0, rightrim = 1)
## $lambdas
## $lambdas[[1]]
##            L1            L2            L3            L4 
## -0.2218269150  0.4776272172  0.0007514416  0.0347470498 
## 
## $lambdas[[2]]
##          L1          L2          L3          L4 
## -0.29940771  0.37431685  0.02522882  0.03554614 
## 
## $lambdas[[3]]
##          L1          L2          L3          L4 
## -0.27716613  0.44331222 -0.00350671  0.07781896 
## 
## $lambdas[[4]]
##           L1           L2           L3           L4 
## -0.157893866  0.496007697  0.097622005  0.003938307 
## 
## 
## $ratios
## $ratios[[1]]
##           T1           T2           T3           T4 
##           NA -2.153152682  0.001573281  0.072749309 
## 
## $ratios[[2]]
##          T1          T2          T3          T4 
##          NA -1.25019110  0.06739963  0.09496271 
## 
## $ratios[[3]]
##          T1          T2          T3          T4 
##          NA -1.59944587 -0.00791025  0.17553985 
## 
## $ratios[[4]]
##           T1           T2           T3           T4 
##           NA -3.141399404  0.196815504  0.007940013
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1)
## $lambdas
##   station         L1        L2            L3          L4
## 1       1 -0.2218269 0.4776272  0.0007514416 0.034747050
## 2       2 -0.2994077 0.3743169  0.0252288170 0.035546143
## 3       3 -0.2771661 0.4433122 -0.0035067105 0.077818960
## 4       4 -0.1578939 0.4960077  0.0976220050 0.003938307
## 
## $ratios
##   station        T2           T3          T4
## 1       1 -2.153153  0.001573281 0.072749309
## 2       2 -1.250191  0.067399629 0.094962711
## 3       3 -1.599446 -0.007910250 0.175539851
## 4       4 -3.141399  0.196815504 0.007940013

This holds when parameter and quantile estimations are calculated:

tlm <- TLMoments(xvec, leftrim = 0, rightrim = 1)
parameters(tlm, "gev")
##         loc       scale       shape 
## -0.13251801  1.03618952 -0.06816311
tlm <- TLMoments(xmat, leftrim = 0, rightrim = 1)
parameters(tlm, "gev")
##              [,1]       [,2]        [,3]       [,4]
## loc   -0.07509325 -0.2325888 -0.13251801 -0.1831963
## scale  1.11360818  0.8503835  1.03618952  1.0290182
## shape -0.04554581  0.1059676 -0.06816311  0.3784279
tlm <- TLMoments(xlist, leftrim = 0, rightrim = 1)
parameters(tlm, "gev")
## [[1]]
##         loc       scale       shape 
## -0.07509325  1.11360818 -0.04554581 
## 
## [[2]]
##        loc      scale      shape 
## -0.2325888  0.8503835  0.1059676 
## 
## [[3]]
##         loc       scale       shape 
## -0.13251801  1.03618952 -0.06816311 
## 
## [[4]]
##        loc      scale      shape 
## -0.1831963  1.0290182  0.3784279
tlm <- TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1)
parameters(tlm, "gev")
##   station         loc     scale       shape
## 1       1 -0.07509325 1.1136082 -0.04554581
## 2       2 -0.23258882 0.8503835  0.10596763
## 3       3 -0.13251801 1.0361895 -0.06816311
## 4       4 -0.18319630 1.0290182  0.37842792
tlm <- TLMoments(xvec, leftrim = 0, rightrim = 1)
quantiles(parameters(tlm, "gev"), c(.99, .999))
##     0.99    0.999 
## 3.959147 5.575842
tlm <- TLMoments(xmat, leftrim = 0, rightrim = 1)
quantiles(parameters(tlm, "gev"), c(.99, .999))
##           [,1]     [,2]     [,3]     [,4]
## 0.99  4.546612 4.808569 3.959147 12.60257
## 0.999 6.524446 8.427293 5.575842 34.22047
tlm <- TLMoments(xlist, leftrim = 0, rightrim = 1)
quantiles(parameters(tlm, "gev"), c(.99, .999))
## [[1]]
##     0.99    0.999 
## 4.546612 6.524446 
## 
## [[2]]
##     0.99    0.999 
## 4.808569 8.427293 
## 
## [[3]]
##     0.99    0.999 
## 3.959147 5.575842 
## 
## [[4]]
##     0.99    0.999 
## 12.60257 34.22047
tlm <- TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1)
quantiles(parameters(tlm, "gev"), c(.99, .999))
##   station      0.99     0.999
## 1       1  4.546612  6.524446
## 2       2  4.808569  8.427293
## 3       3  3.959147  5.575842
## 4       4 12.602568 34.220473

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.31304673 -0.168268506 -0.02349028
## L2  0.31014955  0.367669806  0.42519006
## L3 -0.02161505  0.009469396  0.04055384
## L4  0.04533674  0.060865230  0.07639372
## Approximate 0.9% confidence interval of TL moment ratios: 
##            LCL     tau_hat        UCL
## T2 -4.14641326 -2.18501854 -0.2236238
## T3 -0.05825758  0.02575516  0.1097679
## T4  0.12429576  0.16554318  0.2067906
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.02757001 0.009469396 0.04650880
## L4  0.04236189 0.060865230 0.07936857
## Approximate 0.95% confidence interval of TL moment ratios: 
##           LCL   tau_hat       UCL
## T3 0.06543582 0.1655432 0.2656505
## T4 0.11639385 0.1655432 0.2146925
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.2345662 -0.07298919 0.08858783
## scale  0.7319655  0.85056337 0.96916120
## shape -0.1487695  0.01120871 0.17118690
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.487728 3.942359 5.396989

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 
## 3.959147 5.575842