李俊杰
2006-Dec-29 17:54 UTC
[R] Why giving negative density when doing kernel density estimate?
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.0000000 -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.5263158 7.1052632 7.6842105 8.2631579 8.8421053 [19] 9.4210526 10.0000000 [1] 2.422392e-01 3.877025e-01 4.746580e-01 2.757747e-01 1.787630e-01 [6] 1.102396e-01 2.331694e-02 3.294412e-04 2.260746e-07 6.996146e-12 [11] -1.179461e-18 -1.226790e-17 8.892545e-18 1.144173e-17 -1.881253e-17 [16] -2.782621e-17 -5.314722e-18 -1.691545e-17 -1.986261e-17 -2.498227e-17 Best, Junjie Li Tsinghua Univercity [[alternative HTML version deleted]]
Prof Brian Ripley
2006-Dec-30 09:13 UTC
[R] Why giving negative density when doing kernel density estimate?
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 Dec 2006, ?????? wrote:> 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.0000000 -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.5263158 7.1052632 7.6842105 8.2631579 8.8421053 > [19] 9.4210526 10.0000000 > [1] 2.422392e-01 3.877025e-01 4.746580e-01 2.757747e-01 1.787630e-01 > [6] 1.102396e-01 2.331694e-02 3.294412e-04 2.260746e-07 6.996146e-12 > [11] -1.179461e-18 -1.226790e-17 8.892545e-18 1.144173e-17 -1.881253e-17 > [16] -2.782621e-17 -5.314722e-18 -1.691545e-17 -1.986261e-17 -2.498227e-17 > > Best, > > Junjie Li > > Tsinghua Univercity > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- 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