If it matters to you (why?) use pmax(0, kde$y).
Actually, it bothers me because I need sufficient precision of numerical
calculation in this case, where the density estimate is around zero. To
illustrate why I concern about it, I'd like to introduce the problem I am
working with.
The problem is
This is called 'rounding error', and has been discussed here previously.
If it matters to you (why?) use pmax(0, kde$y).
When doing numerical calculations you should always be aware that the
numerical results will differ from algebraic ones, and that is all that is
happening here.
On Sat, 30
Why? And how to solve it? The code and result are following,
> data=rnorm(50)
>
> kde=density(data,n=20,from=-1,to=10)
>
> kde$x;kde$y
[1] -1.000 -0.4210526 0.1578947 0.7368421 1.3157895 1.8947368
[7] 2.4736842 3.0526316 3.6315789 4.2105263 4.7894737 5.3684211
[13] 5.9473684 6.