
Calculate weights from return periods or frequencies
calculate_weight.RdCompute trapezoidal integration weights corresponding to return periods or exceedance frequencies.
Usage
calculate_weight(x, margin = 1, type = c("period", "frequency"))Arguments
- x
Numeric vector, array,
data.frame, orSectorobject containing return periods or exceedance frequencies.- margin
Integer or integer vector indicating the margin(s) over which weights are calculated when
xis an array (default is 1).- type
Character scalar specifying whether
xcontains return periods ("period", default, where frequency \(p = 1 / x\)) or exceedance frequencies ("frequency").
Value
Object of the same class and dimensions (or length) as x containing
the computed trapezoidal weights, preserving names, dimnames, and NA positions.
Details
In flood risk assessment and cost-benefit analysis, expected annual damage (EAD) or expected annual benefit (EAB) is evaluated by numerically integrating damages across flood exceedance frequencies using the trapezoidal rule. This function computes the integration weights \(w_i\) assigned to each discrete flood event such that the integral \(\int D(p) dp\) is approximated by \(\sum w_i D(p_i)\).
The function accepts several data structures for x:
Numeric vector: weights are computed directly by applying the trapezoidal formula to the vector.
Array (multi-dimensional): weights are calculated along the margin(s) specified by
marginusingapply(), preserving array dimensions and dimension names.data.frame: converted internally to a matrix withmargin = 2(column-wise computation) and returned as a data frame with matching column names and row names.Sector: extracts the"flood"component of the object and computes its corresponding weights.
Missing values (NA) are preserved at their original positions without
preventing or biasing the weight calculation across the defined flood events.
Author
Frédéric Grelot, frederic.grelot.1994_cran@m4x.org
Examples
# Vector of return periods
x1 = c(4, 10, 30, 100, 1000)
names(x1) = c("min", "small", "medium", "large", "max")
calculate_weight(x1)
#> min small medium large max
#> 0.07500000 0.10833333 0.04500000 0.01616667 0.00450000
# Missing values (NA) are preserved at their positions
x_na = c(4, NA, 30, 100, 1000)
calculate_weight(x_na)
#> [1] 0.10833333 NA 0.12000000 0.01616667 0.00450000
# Matrix and array handling
x2 = c(10, 15, 50, 100, 1500)
calculate_weight(cbind(x2), margin = 2)
#> x2
#> [1,] 0.016666667
#> [2,] 0.040000000
#> [3,] 0.028333333
#> [4,] 0.009666667
#> [5,] 0.004666667
y1 = cbind(x1, x2)
calculate_weight(y1, 2)
#> x1 x2
#> min 0.07500000 0.016666667
#> small 0.10833333 0.040000000
#> medium 0.04500000 0.028333333
#> large 0.01616667 0.009666667
#> max 0.00450000 0.004666667
y2 = apply(y1, 2, sample, 5)
calculate_weight(y2, 2)
#> x1 x2
#> [1,] 0.00450000 0.009666667
#> [2,] 0.04500000 0.040000000
#> [3,] 0.01616667 0.016666667
#> [4,] 0.07500000 0.028333333
#> [5,] 0.10833333 0.004666667
y3 = y1 * rep(2:3, each = 5)
if (FALSE) z = abind::abind(y1, y2, along = 3, make.names = TRUE) # \dontrun{}
z = array(cbind(y1, y3), dim = c(dim(y1), 2), dimnames = c(dimnames(y1), list(c("y1", "y3"))))
calculate_weight(z, 2:3)
#> , , y1
#>
#> x1 x2
#> min 0.07500000 0.016666667
#> small 0.10833333 0.040000000
#> medium 0.04500000 0.028333333
#> large 0.01616667 0.009666667
#> max 0.00450000 0.004666667
#>
#> , , y3
#>
#> x1 x2
#> min 0.037500000 0.005555556
#> small 0.054166667 0.013333333
#> medium 0.022500000 0.009444444
#> large 0.008083333 0.003222222
#> max 0.002250000 0.001555556
#>
# Data frame handling
df = data.frame(x1, x2)
calculate_weight(df)
#> x1 x2
#> min 0.07500000 0.016666667
#> small 0.10833333 0.040000000
#> medium 0.04500000 0.028333333
#> large 0.01616667 0.009666667
#> max 0.00450000 0.004666667