quantiles: Calculating quantiles from distribution parameters

Description Usage Arguments Value See Also Examples

View source: R/quantiles.R

Description

Calculates quantiles from distribution parameters received by parameters or from a named vector.

Usage

1
quantiles(x, p, distr = attr(x, "distribution"))

Arguments

x

object returned by parameters or a named vector. In the latter you have to specify the distr-argument.

p

numeric vector giving the quantiles to calculate.

distr

character object defining the distribution. Supported types are "gev", "gum" and "gpd". You do not need to set this, if x is from parameters.

Value

numeric vector, matrix, list, or data.frame of the quantiles and with class quantiles. The object contains the following attributes:

The attributes are hidden in the print-function for a clearer presentation.

See Also

PWMs, TLMoments, parameters, summary.quantiles

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# Generating data sets:
xmat <- matrix(rnorm(100), nc = 4)
xvec <- xmat[, 3]
xlist <- lapply(1L:ncol(xmat), function(i) xmat[, i])
xdat <- data.frame(
 station = rep(letters[1:2], each = 50),
 season = rep(c("S", "W"), 50),
 hq = as.vector(xmat)
)

# Calculating quantiles from parameters-object
tlm <- TLMoments(xvec, leftrim = 0, rightrim = 1)
quantiles(parameters(tlm, "gev"), c(.9, .99))
tlm <- TLMoments(xmat, leftrim = 1, rightrim = 1)
quantiles(parameters(tlm, "gum"), c(.9, .95, .999))
tlm <- TLMoments(xlist)
quantiles(parameters(tlm, "gpd"), .999)
tlm <- TLMoments(xdat, hq ~ station, leftrim = 2, rightrim = 3)
quantiles(parameters(tlm, "gev"), seq(.1, .9, .1))
tlm <- TLMoments(xdat, hq ~ station + season, leftrim = 0, rightrim = 2)
quantiles(parameters(tlm, "gum"), seq(.1, .9, .1))

# Distribution can be overwritten (but parameters have to fit)
tlm <- TLMoments(xvec, leftrim = 0, rightrim = 1)
params <- parameters(tlm, "gev")
quantiles(params, c(.9, .99))
quantiles(params[1:2], c(.9, .99), distr = "gum")
evd::qgumbel(c(.9, .99), loc = params[1], scale = params[2])


# Using magrittr
library(magrittr)
rgev(50, shape = .3) %>%
  TLMoments(leftrim = 0, rightrim = 1) %>%
  parameters("gev") %>%
  quantiles(c(.99, .999))

# Calculating quantiles to given parameters for arbitrary functions
quantiles(c(mean = 10, sd = 3), c(.95, .99), "norm")
qnorm(c(.95, .99), mean = 10, sd = 3)

# These give errors:
#quantiles(c(loc = 10, scale = 5, shape = .3), c(.95, .99), "notexistingdistribution")
#quantiles(c(loc = 10, scale = 5, shpe = .3), c(.95, .99), "gev") # wrong arguments

Example output

Loading required package: Rcpp
     0.9     0.99 
1.386940 2.226781 
          [,1]     [,2]     [,3]     [,4]
0.9   1.429702 1.513019 1.553972 1.407733
0.95  1.992741 2.089046 2.068524 1.966030
0.999 5.072248 5.239594 4.882840 5.019608
[[1]]
   0.999 
1.290895 

[[2]]
   0.999 
2.263077 

[[3]]
   0.999 
2.061374 

[[4]]
   0.999 
2.082739 

  station        0.1        0.2        0.3         0.4        0.5       0.6
1       a -1.2696432 -0.7702387 -0.4278499 -0.14924353 0.09782902 0.3303556
2       b -0.9560959 -0.5634144 -0.2854363 -0.05288771 0.15900635 0.3642194
        0.7       0.8      0.9
1 0.5611081 0.8048266 1.090857
2 0.5745931 0.8059323 1.093816
  station season        0.1        0.2        0.3         0.4        0.5
1       a      S -0.8686954 -0.5370182 -0.2682127 -0.01534509  0.2431189
2       b      S -1.1010287 -0.7378556 -0.4435244 -0.16664461  0.1163630
3       a      W -1.3648505 -1.0053446 -0.7139854 -0.43990144 -0.1597515
4       b      W -0.9855607 -0.6384943 -0.3572166 -0.09261633  0.1778400
        0.6       0.7       0.8      0.9
1 0.5257749 0.8584300 1.2927755 1.987740
2 0.4258599 0.7901038 1.2656946 2.026652
3 0.1466203 0.5071862 0.9779747 1.731248
4 0.4736108 0.8217005 1.2761990 2.003408
     0.9     0.99 
1.386940 2.226781 
     0.9     0.99 
1.811583 3.728750 
[1] 1.811583 3.728750
    0.99    0.999 
14.65417 40.72519 
    0.95     0.99 
14.93456 16.97904 
[1] 14.93456 16.97904

TLMoments documentation built on Dec. 4, 2019, 5:06 p.m.