[R] Computing inverse cdf (quantile function) from a KDE

David L Carlson dcarlson at tamu.edu
Thu Jul 12 06:09:22 CEST 2012

Something like this? 

x <- rnorm(100)
KS <- bkde(x)
diff <- KS$x[2] - KS$x[1]
yc <- cumsum(KS$y*diff)
approx(yc, KS$x, runif(1000))

If you are going to use approx() repeatedly, you can create a
function with approxfun().

David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77840-4352

----- Original Message ----- 

From: "firdaus.janoos" <fjanoos at bwh.harvard.edu> 
To: r-help at r-project.org 
Sent: Wednesday, July 11, 2012 9:33:30 AM 
Subject: [R] Computing inverse cdf (quantile function) from a KDE 


I wanted to know if there is a simple way of getting the inverse cdf for a 
KDE estimate of a density (using the ks or KernSmooth packages) in R ? 

The method I'm using now is to perform a numerical integration of the pdf 
to get the cdf and then doing a search for the desired probablity value, 
which is highly inefficient and very slow. 


[[alternative HTML version deleted]] 

R-help at r-project.org mailing list 
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html 
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list