[R] Problem with density()?
thomas at biostat.washington.edu
Thu Aug 10 19:45:49 CEST 2000
On Thu, 10 Aug 2000, Nels Tomlinson wrote:
> Hello, all,
> I'm running Version 1.1.0 (June 15, 2000), on Mandrake 6.1 Linux.
> I think that I'm getting nonsense results from the density function.
> When I feed it a sample generated by v<-runif(1000,0,1), I get back some
> small negative numbers for density values, as follows:
If you do
I think you will find that the negative values happen at x values outside
the range of the data. When I tried they were all <-0.1 or >1.1, with a
bandwidth of 0.065. As the density really is zero here it's not
surprising that the estimate is effectively zero. (It's not a bug, it's a
It is unfortunate, I suppose, that the density is slightly negative rather
than slightly positive, but it wouldn't make much practical difference to
your problem: you don't want to divide by it anyway.
With the obvious implementation of kernel densities the estimate would
have to be non-negative even with rounding error, and would be zero or NaN
at these points. However, the FFT implementation is way faster.
> > dump("v","/home/tomlinso/auction/test.vector.R")
> > slime<-density(v,kernel="r")
> > sort(slime$y)
>  -1.869303e-16 -1.110223e-16 -1.110223e-16 -1.065198e-16
>  -1.016304e-16 -9.779196e-17 -9.427232e-17 -9.368320e-17
>  -8.169144e-17 -7.225386e-17 -6.740389e-17 -6.142515e-17
>  -5.652376e-17 -5.501082e-17 -5.348798e-17 -5.347797e-17
>  -5.043385e-17 -4.948219e-17 -4.835455e-17 -4.633728e-17
>  -4.039872e-17 -3.836554e-17 -3.826879e-17 -3.646713e-17
>  -2.825989e-17 -2.821749e-17 -2.743282e-17 -2.678935e-17
>  -2.518910e-17 -2.510075e-17 -2.436936e-17 -1.919740e-17
>  -1.757412e-17 -1.673804e-17 -1.615867e-17 -1.327275e-17
>  -1.286342e-17 -9.888095e-18 -8.665413e-18 -6.783308e-18
>  -2.394122e-19 7.552237e-18 1.063422e-17 2.765485e-17
>  4.637632e-17 4.727742e-17 6.705682e-17 6.914125e-17
>  6.663715e-04 1.703646e-03 3.401654e-03 8.696669e-03
>  1.861714e-02 2.548490e-02 2.719238e-02 3.502090e-02
> I don't think that I should be getting negative numbers, even tiny
> ones. These are close enough to zero to cause trouble, since the
> estimator I'm implementing calls for dividing by those density values!
> Any clues about what hit me and what I can do about it would be
> Nels Tomlinson
> /\ / _ / _ --- _ / , _ _ _
> / \/ (- / _) / () //) / / / )_) ()/ )
> 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
Assistant Professor, Biostatistics
University of Washington, Seattle
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