[R] help in density estimation

Duncan Murdoch murdoch.duncan at gmail.com
Thu Sep 23 18:52:09 CEST 2010


  On 23/09/2010 11:42 AM, wangguojie2006 wrote:
> b<-runif(1000,0,1)
> f<-density(b)

f is a list of things, including x values where the density is computed, 
and y values for the density there.  So you could do it by linear 
interpolation using approx or approxfun.  For example

 > b <- runif(1000,0,1)
 > flist <- density(b)
 > f <- approxfun(flist$x, flist$y)
 > f(0.2)
[1] 0.9717893
 > f(-1)
[1] NA

If you don't like the NA for an out-of-range argument, then choose 
something different from the default for the "rule" argument to approxfun.

Duncan Murdoch



More information about the R-help mailing list