[R] density() returns a density function that does not add up to 1

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Fri Aug 30 22:43:24 CEST 2002


Jonathan Li <jonqli at labs.agilent.com> writes:

> Thank you for the message. It helps a lot. I see where my method was
> wrong.
> Now another case:
> 
> > dd <- c(rnorm(1000,0,1), rnorm(100, 100,1), rnorm(10, 1000,1))
> > ddd <- density(dd)
> > sum( diff(ddd$x)*ddd$y)
> [1] 2.856715
> 
> If you add more extreme tails, the thing gets worse:
> 
> > dd <- c(rnorm(1000,0,1), rnorm(100, 100,1), rnorm(10, 1000,1), rnorm(5, 5000,1))
> > ddd <- density(dd)
> > sum( diff(ddd$x) * ddd$y)
> [1] 13.90902
> 
> I am not sure what is going on now?

You're trying to calculate and then integrate a very spiky density.
Try plotting it and then increase the number of points used to
approximate it:

> dd <- c(rnorm(1000,0,1), rnorm(100, 100,1), rnorm(10, 1000,1), rnorm(5, 5000,1))
>  ddd <- density(dd,n=2^15)
>  sum( diff(ddd$x) * ddd$y)
[1] 1.000014
Warning message: 
longer object length
        is not a multiple of shorter object length in: diff(ddd$x) * ddd$y 


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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 mailing list