# [R] lookup function for density(...) objects

Ross Ihaka ihaka at stat.auckland.ac.nz
Sat May 12 03:01:18 CEST 2001

```Martin Maechler pointed out to me that you wanted the cumulative
probabilities.  Here is a quick and dirty solution.

#  Given a kernel density estimate, this function
#  carries out a (very quick and dirty) numerical
#  integration, and then fits a spline to get a function
#  which can be used to look up cumulative probabilities.

smoothed.df <-
function(d)
{
F <- cumsum(d\$y)
F <- F/F[length(F)]
splinefun(d\$x, F)
}

# Generate a bimodal test distribution
# Estimate the desnsity and distribution function
x <- rnorm(1000) + ifelse(runif(1000) > .5, -3, 3)
d <- density(x)
F <- smoothed.df(d)   # F returns cumulative probs

# Plot the true and estimated distribution function
curve(0.5 * dnorm(x, -3) + 0.5 * dnorm(x, 3), col="red")
lines(d)

# Plot the true and estimated distribution function
F <- smoothed.d

--
Ross Ihaka                         Email:  ihaka at stat.auckland.ac.nz
Department of Statistics           Phone:  (64-9) 373-7599 x 5054
University of Auckland             Fax:    (64-9) 373-7018
Private Bag 92019, Auckland
New Zealand
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```