Java Mailing List Archive

http://www.r-help.com/

Home » R Help for Statistical Computing »

Re: [R] percentage from density()

Rolf Turner

2012-01-28

Replies: Find Java Web Hosting

Author LoginPost Reply
On 28/01/12 11:44, Duke wrote:
> Hi folks,
>
> I know that density function will give a estimated density for a give
> dataset. Now from that I want to have a percentage estimation for a
> certain range. For examle:
>
> > y = density(c(-20,rep(0,98),20))
> > plot(y, xlim=c(-4,4))
>
> Now if I want to know the percentage of data lying in (-20,2).
> Basically it should be the area of the curve from -20 to 2. Anybody
> knows a simple function to do it?

You could try:

  foo <- with(y,splinefun(x,y))
  integrate(foo,lower=-20,upper=2)

Note that

  integrate(foo,lower=min(y$x),upper=max(y$x))

yields "1.000951 with absolute error < 0.00011", rather than giving
exactly 1, so there's a bit of slop in the system.

  cheers,

     Rolf Turner

______________________________________________
R-help@(protected)
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.
©2008 r-help.com - Jax Systems, LLC, U.S.A.