Re: [R] integrate

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Sat 24 Jun 2006 - 01:13:34 EST

On Fri, 23 Jun 2006, Rogério Rosa da Silva wrote:

> Dear All,
>
> My doubt about how to integrate a simple kernel density estimation goes on.
>
> I have seen the recent posts on integrate density estimation, which seem
> similar to my question. However, I haven't found a solution.
>
> I have made two simple kernel density estimation by:
>
> kde.1 <-density(x, bw=sd(x), kernel="gaussian")$y # x<- c(2,3,5,12)
> kde.2 <-density(y, bw=sd(y), kernel="gaussian")$y # y<- c(4,2,4,11)
>
> Now I would like to integrate the difference in the estimated density
> values, i.e.:
>
> diff.kde <- abs (kde.1- kde.2)
>
> How can I integrate diff.kde over -Inf to Inf ?

Well, the answer is zero.

Computationally this is a bit tricky. You can turn the density estimates into functions with approxfun()
x<-rexp(100)
kde<-density(x)
  f<-approxfun(kde$x,kde$y,rule=2)
integrate(f,-1,10)
1.000936 with absolute error < 3.3e-05

But if you want to integrate over -Inf to Inf you need the function to specify the values outside the range of the data. The only value that will work over the range -Inf to Inf is zero
> f<-approxfun(kde$x,kde$y,yleft=0,yright=0)
> integrate(f,-1,10)

1.00072 with absolute error < 1.5e-05
> integrate(f,-Inf,Inf)
1.000811 with absolute error < 2.3e-05

         -thomas



R-help@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 Received on Sat Jun 24 02:42:47 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 24 Jun 2006 - 04:12:22 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.