discretise: Discretise a Distribution

View source: R/discretize.R

discretiseR Documentation

Discretise a Distribution

Description

Bins a distribution by specified breakpoints. Left and right endpoints of +/- infinity are implied.

Usage

discretise(
  distribution,
  breakpoints,
  midpoints = c("median", "mean", "label"),
  values,
  closed = c("right", "left")
)

discretize(
  distribution,
  breakpoints,
  midpoints = c("median", "mean", "label"),
  values,
  closed = c("right", "left")
)

Arguments

distribution

Distribution to discretise.

breakpoints

Vector of breakpoints separating each bin.

midpoints, values

Specify the values associated with each new bin by either specifying a computation of the midpoint, or as a vector of values (of length one more than that of breakpoints) applied to the bins from smallest to largest. If specified, values will override midpoint.

returns

A finite distribution with the specified bins.

Note

NA breakpoints and values (if specified) are silently removed. Will throw an error if the lengths of values does not end up being one more than that of breakpoints.

Examples

p1 <- dst_norm(0, 1)
p2 <- discretise(p1, breakpoints = -2:2)
p3 <- discretise(p1, breakpoints = -2:2, midpoints = "mean")
plot(p1, "cdf", from = -3, to = 3)
plot(p2, "cdf", from = -3, to = 3, add = TRUE, col = "blue", n = 1000)
plot(p3, "cdf", from = -3, to = 3, add = TRUE, col = "red", n = 1000)

discretise(dst_exp(0.1), breakpoints = numeric())

dst_norm(0, 1) %>%
  slice_left(-2) %>%
  slice_right(2) %>%
  discretize(-2:2)

vincenzocoia/distplyr documentation built on March 5, 2024, 9:45 p.m.