[R] percentage from density()
Rolf Turner
rolf.turner at xtra.co.nz
Sat Jan 28 00:09:17 CET 2012
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
More information about the R-help
mailing list