pdf2cdf: Integrate a PDF to get a CDF

Description Usage Arguments Value Examples

View source: R/pdf2cdf.R

Description

Numerically integrates a PDF to get a CDF using the trapezoid rule. This function is designed for use on the output of kernel density estimators like base::density or KernSmooth::bkde.

Usage

1
pdf2cdf(p, x = NULL, normalize = TRUE, expand = FALSE)

Arguments

p

The PDF to integrate, either as a vector of probabilities or as a list with elements named 'x' and 'y'. Probabilities (p or p$y) should be non-negative; negative values will be silently set to 0.

x

The x-coordinates of the PDF. If p is an atomic vector and x is not given, x is assumed to be 1:length(p). Values of x must be strictly increasing, but need not be uniformly spaced.

normalize

Logical; if true, normalizes the PDF to integrate exactly to 1. (PDF estimates produced by base::density often need this correction.)

expand

Logical; if true, returns a CDF two elements longer than the input PDF that includes the two fencepost values added onto the ends for integration. If normalize=TRUE, the fenceposts will be identically 0 and 1.

Value

A list with components 'x' and 'y'.

x: vector of x coordinates for the distribution.

y: vector of probabilities that the distribution takes on a value less than or equal to the corresponding x.

Examples

1
2
3
4
5
library(nor1mix)
r = rnorMix(1e4,norMix(c(-3,3)))
par(mfrow=c(2,1))
plot(density(r))
plot(pdf2cdf(density(r)),type='l')

sethmcg/climod documentation built on Nov. 19, 2021, 11:12 p.m.