firdaus.janoos
2012-Jul-11 14:33 UTC
[R] Computing inverse cdf (quantile function) from a KDE
Hello, 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. Thanks, -fj [[alternative HTML version deleted]]
David L Carlson
2012-Jul-12 04:09 UTC
[R] Computing inverse cdf (quantile function) from a KDE
Something like this? library(KernSmooth) 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 Hello, 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. Thanks, -fj [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
If you are going to be doing a lot of this then you might want to consider using logspline density estimates (logspline package) instead of kernel density estimates. On Wed, Jul 11, 2012 at 8:33 AM, firdaus.janoos <fjanoos at bwh.harvard.edu> wrote:> Hello, > > 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. > > Thanks, > -fj > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Gregory (Greg) L. Snow Ph.D. 538280 at gmail.com