Skip to content

help in density estimation

6 messages · Duncan Murdoch, Greg Snow, wangguojie2006 +1 more

#
Hi, guys,

I'm using kernel "density" estimation. But how can I return to a density
estimation for a fixed point?

For example,

b<-runif(1000,0,1)
f<-density(b)

How can I get the value of density(b) at b=0.5?

Your help is extremely appreciated. Thanks.

Jay
#
On 23/09/2010 11:42 AM, wangguojie2006 wrote:
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
#
On 23-Sep-10 16:52:09, Duncan Murdoch wrote:
Or, perhaps more transparently (and more explicitly modifiable):

  b<-runif(1000,0,1)
  f <- density(b, from=0, to=1, n=512)
  plot(f$x, f$y, type="l", col="blue",
       xlim=c(0,1), ylim=c(0,1.5))     ## Plot the density estimate
  x0 <- 0.5                            ## Target value of x
  i0 <- max(which(f$x <= x0))
  i1 <- min(which(f$x > x0))
  u0 <- f$x[i0] ; v0 <- f$y[i1]
  u1 <- f$x[i1] ; v1 <- f$y[i1]
  y0 <- v0 + (v1-v0)*(x0-u0)/(u1-u0)   ## Linear interpolation
  points(x0, y0, pch="+", col="red")   ## Add interpolated point

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 23-Sep-10                                       Time: 18:05:41
------------------------------ XFMail ------------------------------
#
You could do:

b <- runif(10000, 0, 1)
tmp <- density(b, from=0.5, to=0.5, n=1)
tmp$y

As one direct approach.

You could also look at the logspline package for an alternative for density estimation that provides you with a density function (and also allows for finite  domains).
#
There was a typo error in my code below. See the inserted correction.
On 23-Sep-10 17:05:45, Ted Harding wrote:
b<-runif(1000,0,1)
  f <- density(b, from=0, to=1, n=512)
  plot(f$x, f$y, type="l", col="blue",
       xlim=c(0,1), ylim=c(0,1.5))     ## Plot the density estimate
  x0 <- 0.5                            ## Target value of x
  i0 <- max(which(f$x <= x0))
  i1 <- min(which(f$x > x0))
##u0 <- f$x[i0] ; v0 <- f$y[i1]        ## WRONG!
  u0 <- f$x[i0] ; v0 <- f$y[i0]        ## Correction
  u1 <- f$x[i1] ; v1 <- f$y[i1]
  y0 <- v0 + (v1-v0)*(x0-u0)/(u1-u0)   ## Linear interpolation
  points(x0, y0, pch="+", col="red")   ## Add interpolated point
--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 23-Sep-10                                       Time: 20:38:53
------------------------------ XFMail ------------------------------