Skip to contents

Calculate the proportion of non-positive values (\(\le 0\)) of an indicator (such as NPV) conditioned on intervals of one or more input parameters.

Usage

prop_negative(x, y, breaks = 10)

Arguments

x

A numeric vector, list of numeric vectors, or data frame of input parameters along which bins are formed.

y

Numeric vector of indicator values (e.g. Net Present Value) of the same length as the vectors in x.

breaks

Integer scalar, number of equal-width intervals used to discretize each parameter in x (default is 10).

Value

An array or table (with dimensions corresponding to each variable in x) containing the proportion of non-positive values (\(\le 0\)) of y in each bin.

Details

The function bins each parameter in x into breaks equal-width intervals across its range using findInterval(). It then aggregates the indicator vector y within each multi-dimensional bin using tapply() to compute the proportion of values satisfying \(y \le 0\).

This is commonly used in CBA uncertainty analysis to estimate the probability of an unprofitable investment (NPV \(\le\) 0) conditioned on varying parameter ranges.

Author

Frédéric Grelot, frederic.grelot.1994_cran@m4x.org

Examples

case = import_case(
    system.file("extdata", "case", "simple.ods", package = "floodam.cba")
)
#> import Case from /tmp/RtmpvMoJJK/temp_libpath2b69f6f14b9e9/floodam.cba/extdata/case/simple.ods
#>  ... done
result = perform_sa(case, n = 100, verbose = FALSE)
u_a = result$u_a
cost_col = grep("^cost_", colnames(u_a), value = TRUE)[1]

# Proportion of negative NPV according to cost bins
prop_negative(u_a[[cost_col]], u_a[["npv"]], breaks = 5)
#> v
#>     80000     89986     99972    109959    119945 
#> 0.4102564 0.3863636 0.4324324 0.4615385 0.5952381