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 toaccomplish this task? Thanks. Rajiv. -------- Rajiv Prasad, Postdoctoral Research Associate, Hydrology Group Pacific Northwest National Laboratory, P.O. Box 999, MSIN K9-33, Richland, WA 99352 Voice: (509) 375-2096 Fax: (509) 372-6089 Email: rajiv.prasad at pnl.gov -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 curve(F(x), add=TRUE) -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Well if that sort of thing is what is wanted, using a Johnson or Pearson system fit may also be useful. Functions for the Johnson system are available in the package SuppDists. Ross Ihaka wrote:> > 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 > curve(F(x), add=TRUE) > > -- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- Bob Wheeler --- (Reply to: bwheeler at echip.com) ECHIP, Inc. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._