[R] Kernel density

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed May 28 14:41:12 CEST 2003


On Wed, 28 May 2003, Christian Hennig wrote:

> I want to fit a kernel density estimator by bkde of library KernSmooth.
> I need only the density value at the point 0. 
> I do not understand the following behaviour:

It's not designed to do that.  And why use a linear-binning method for
just one point?

gridsize has to be at least 6, it seems.

> > q <- rnorm(100) 
> > bkq <- bkde(q, bandwidth=0.11, gridsize=1, range.x=c(0,0))
> Error in 0:L : NA/NaN argument
> > bkq <- bkde(q, bandwidth=0.11, gridsize=1, range.x=c(-1,1))
> > bkq
> $x
> [1] -1
> 
> $y
> [1] NA
> 
> > bkq <- bkde(q, bandwidth=0.11, gridsize=3, range.x=c(-1,1))
> Warning message: 
> longer object length
> 	is not a multiple of shorter object length in: kappa * gcounts 
> > bkq
> $x
> [1] -1  0  1
> 
> $y
> [1] NA NA NA
> 
> > bkq <- bkde(q, bandwidth=0.11, range.x=c(-1,1))
> (works, I get 401 proper y-values)
> 
> Adding truncate=FALSE does not change anything.
> Do I really need to generate 401 (default gridsize) y-fits to extract the
> one value I am interested in? What's the problem with specifying gridsize?

Do read and debug the code, but also consider if this is a useful 
thing to do compared to, say,

> density(q, bw=0.11, n=1, from=0, to=0)

Call:
        density(x = q, bw = 0.11, n = 1, from = 0, to = 0)

Data: q (100 obs.);     Bandwidth 'bw' = 0.11

       x                y
 Min.   :0        Min.   :0.3202
 1st Qu.:0        1st Qu.:0.3202
 Median :0        Median :0.3202
 Mean   :0        Mean   :0.3202
 3rd Qu.:0        3rd Qu.:0.3202
 Max.   :0        Max.   :0.3202


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list