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

Ross Ihaka ihaka at stat.auckland.ac.nz
Fri May 11 06:55:46 CEST 2001

"Prasad, Rajiv" wrote:
> Hi folks:
> Is there a lookup function that returns the variate given the cumulative
> probability for an object returned by the density(...) function?
> > mydata _ as.vector(mymatrix)
> > mydata.density _ density(mydata)
> > mydata.p80 _ lookup(mydata.density, p=0.8)  # is there any function to
> accomplish this task?

One simple thing to do is to obtain an interpolating spline through the
(x,y) values returned by "density".  Something like this:

	> y <- rnorm(1000)
	> d <- density(y)
	> f <- splinefun(d$x, d$y)
	> f(seq(-3,3,by=.1))

Note that splinefun returns a function which can be used to do the
lookup. If you'd prefer linear interpolation, you can use approxfun.

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

More information about the R-help mailing list