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.
## R version 4.4.2 (2024-10-31)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.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/libopenblasp-r0.3.26.so; LAPACK version 3.12.0
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_US.UTF-8 LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## time zone: Etc/UTC
## tzcode source: system (glibc)
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## other attached packages:
## [1] lmom_3.2 Lmoments_1.3-1 lmomco_2.5.1 TLMoments_0.7.5.3
## [5] Rcpp_1.0.14 rmarkdown_2.29
##
## loaded via a namespace (and not attached):
## [1] cli_3.6.4 knitr_1.49 rlang_1.1.5 xfun_0.51
## [5] jsonlite_1.9.0 buildtools_1.0.0 htmltools_0.5.8.1 maketools_1.3.2
## [9] sys_3.4.3 sass_0.4.9 evaluate_1.0.3 jquerylib_0.1.4
## [13] MASS_7.3-64 fastmap_1.2.0 yaml_2.3.10 lifecycle_1.0.4
## [17] evd_2.3-7.1 compiler_4.4.2 goftest_1.2-3 digest_0.6.37
## [21] R6_2.6.1 bslib_0.9.0 tools_4.4.2 cachem_1.1.0
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:
TL-moments are calculated by the function TLMoments
with
arguments leftrim
, rightrim
, and
max.order
(generating an object of class
TLMoments
):
## $lambdas
## L1 L2 L3 L4
## 13.659190 4.616082 1.922535 1.429602
##
## $ratios
## T1 T2 T3 T4
## NA 0.3379470 0.4164864 0.3097003
## $lambdas
## L1 L2
## 9.043108 2.020160
##
## $ratios
## T1 T2
## NA 0.2233922
We can calculate parameter estimates by putting a
TLMoments
-object to the function parameters
and specifying argument distr
:
## loc scale shape
## 8.9846954 4.2366760 0.3517167
## loc scale shape
## 9.0555215 4.3131999 0.3095828
This generates an object of class parameters
, which can
be transmitted to quantiles
to calculate quantile
estimations:
## 0.9 0.99 0.999
## 23.52008 57.68233 133.68223
## 0.9 0.99 0.999
## 23.0863 53.0012 113.3468
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.
## 1 data row(s) with n = 100.
## TL(0,1) calculated.
##
## Approximate 90% confidence interval of TL moments:
## LCL lambda_hat UCL
## L1 -0.19613513 -0.03938472 0.11736568
## L2 0.34916082 0.39770412 0.44624741
## L3 -0.04016782 -0.01778308 0.00460166
## L4 0.02579652 0.04498367 0.06417082
## Approximate 90% confidence interval of TL moment ratios:
## LCL tau_hat UCL
## T2 -50.33143488 -10.09792875 30.13557737
## T3 -0.10184354 -0.04471435 0.01241484
## T4 0.05790561 0.11310838 0.16831116
## 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.05769581 0.1202399 0.298175599
## scale 0.81264098 0.9357240 1.058806992
## shape -0.32372058 -0.1579219 0.007876755
## 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.228568 3.179936 4.131304
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.
## 1 data row(s) with n = 100.
## TL(0,1) calculated.
##
## Approximate 95% confidence interval of TL moments:
## LCL lambda_hat UCL
## L3 -0.04445615 -0.01778308 0.008889983
## L4 0.02212077 0.04498367 0.067846570
## Approximate 95% confidence interval of TL moment ratios:
## LCL tau_hat UCL
## T3 0.04503476 0.1131084 0.1811820
## T4 0.04733022 0.1131084 0.1788866
## 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.3237206 -0.1579219 0.007876755
At the moment, the summary functions do not work for data in lists or data.frames.
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 31.60618 53.0012 74.39621
## 0.999 33.09235 113.3468 193.60119
In the following this syntax is used for a clearer presentation.
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:
## $lambdas
## L1 L2 L3 L4
## 0.03779884 0.34415705 -0.02425376 0.02365193
##
## $ratios
## T1 T2 T3 T4
## NA 9.10496401 -0.07047294 0.06872423
## $lambdas
## [,1] [,2] [,3] [,4]
## L1 -0.43766317 -0.07794448 0.03779884 -0.254201690
## L2 0.42244034 0.41504134 0.34415705 0.554637092
## L3 -0.02182104 -0.02124299 -0.02425376 -0.005112218
## L4 0.03338220 0.06789106 0.02365193 0.020156168
##
## $ratios
## [,1] [,2] [,3] [,4]
## T1 NA NA NA NA
## T2 -0.96521794 -5.32483284 9.10496401 -2.18187807
## T3 -0.05165473 -0.05118284 -0.07047294 -0.00921723
## T4 0.07902228 0.16357662 0.06872423 0.03634118
## $lambdas
## $lambdas[[1]]
## L1 L2 L3 L4
## -0.43766317 0.42244034 -0.02182104 0.03338220
##
## $lambdas[[2]]
## L1 L2 L3 L4
## -0.07794448 0.41504134 -0.02124299 0.06789106
##
## $lambdas[[3]]
## L1 L2 L3 L4
## 0.03779884 0.34415705 -0.02425376 0.02365193
##
## $lambdas[[4]]
## L1 L2 L3 L4
## -0.254201690 0.554637092 -0.005112218 0.020156168
##
##
## $ratios
## $ratios[[1]]
## T1 T2 T3 T4
## NA -0.96521794 -0.05165473 0.07902228
##
## $ratios[[2]]
## T1 T2 T3 T4
## NA -5.32483284 -0.05118284 0.16357662
##
## $ratios[[3]]
## T1 T2 T3 T4
## NA 9.10496401 -0.07047294 0.06872423
##
## $ratios[[4]]
## T1 T2 T3 T4
## NA -2.18187807 -0.00921723 0.03634118
## $lambdas
## station L1 L2 L3 L4
## 1 1 -0.43766317 0.4224403 -0.021821042 0.03338220
## 2 2 -0.07794448 0.4150413 -0.021242993 0.06789106
## 3 3 0.03779884 0.3441570 -0.024253760 0.02365193
## 4 4 -0.25420169 0.5546371 -0.005112218 0.02015617
##
## $ratios
## station T2 T3 T4
## 1 1 -0.9652179 -0.05165473 0.07902228
## 2 2 -5.3248328 -0.05118284 0.16357662
## 3 3 9.1049640 -0.07047294 0.06872423
## 4 4 -2.1818781 -0.00921723 0.03634118
This holds when parameter and quantile estimations are calculated:
## loc scale shape
## 0.1943014 0.8109804 -0.2226868
## [,1] [,2] [,3] [,4]
## loc -0.2620599 0.0941783 0.1943014 -0.07176618
## scale 0.9945881 0.9771293 0.8109804 1.29681214
## shape -0.1752112 -0.1740319 -0.2226868 -0.07129628
## [[1]]
## loc scale shape
## -0.2620599 0.9945881 -0.1752112
##
## [[2]]
## loc scale shape
## 0.0941783 0.9771293 -0.1740319
##
## [[3]]
## loc scale shape
## 0.1943014 0.8109804 -0.2226868
##
## [[4]]
## loc scale shape
## -0.07176618 1.29681214 -0.07129628
## station loc scale shape
## 1 1 -0.26205993 0.9945881 -0.17521118
## 2 2 0.09417830 0.9771293 -0.17403192
## 3 3 0.19430141 0.8109804 -0.22268678
## 4 4 -0.07176618 1.2968121 -0.07129628
## 0.99 0.999
## 2.528641 3.053925
## [,1] [,2] [,3] [,4]
## 0.99 2.879082 3.187451 2.528641 5.014220
## 0.999 3.722117 4.021251 3.053925 7.001591
## [[1]]
## 0.99 0.999
## 2.879082 3.722117
##
## [[2]]
## 0.99 0.999
## 3.187451 4.021251
##
## [[3]]
## 0.99 0.999
## 2.528641 3.053925
##
## [[4]]
## 0.99 0.999
## 5.014220 7.001591
TLMoments(xdat, hq ~ station, leftrim = 0, rightrim = 1) %>%
parameters("gev") %>%
quantiles(c(.99, .999))
## station 0.99 0.999
## 1 1 2.879082 3.722117
## 2 2 3.187451 4.021251
## 3 3 2.528641 3.053925
## 4 4 5.014220 7.001591
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.
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.
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):
## $lambdas
## L1 L2 L3
## 14.10 4.30 1.32
##
## $ratios
## T1 T2 T3
## NA 0.3049645 0.3069767
## loc scale shape
## 10.0134305 4.9448851 0.2034746
## 0.9 0.99 0.999
## 24.12668 47.67693 84.80024
## loc scale shape
## 10.0 5.0 0.2
## 0.9 0.99 0.999
## 24.21069 47.73413 84.51684
## $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
## $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.