Rollmean vs. Integrate.xy for computing Integrals I have a density function in R that reflects an underlying null distribution, for example:
 density_null=density(rnorm(100)) 

I want to integrate between 0 and some point. Using the package integrate.xy, I get:
number=-1
library(sfsmisc)
pvalue=integrate.xy(density_null$x,density_null$y,min(density_null$x),number)
#pvalue=.0301

However when I use the rollMean package for integrating:
library(zoo)
Avg.pos <- number;xt <- diff(density_null$x[density_null$x<Avg.pos]);yt <- rollmean(density_null$y[density_null$x<Avg.pos],2);
pvalue=sum(xt*yt)
#pvalue=.028

Thus, my question is: Which of these two methods is better for integration of density plots? I realize the difference is small, but my this is leading to a significant difference down the analysis pipeline I am doing that is sensitive to pvalue changes. Also, my actual data is not really a normal distribution, but is a density distribution derived from other data, so analytical methods for hypothesis testing relying on properties of distributions will not work.
 A: Rollmean appears to be better I benchmarked this for normal distributions at least. This benchmark does 1000 integrations, where each distribution had 10,000 points. As a true value, I simply take the number of points below the number.
 bench_mark=data.frame()
bench_mark=data.frame()

for (i in 1:1000){
    nums=rnorm(10000)
    density_null=density(nums)
     #Get random number
    number=runif(1,-1,-.5)
    z=length(nums[nums <=number]);true_integral=z/length(nums)

library(sfsmisc)
pvalue_integrate_xy=integrate.xy(density_null$x,density_null$y,min(density_null$x),number)
 library(zoo)
 Avg.pos <- number;xt <- diff(density_null$x[density_null$x<Avg.pos]);yt <- rollmean(density_null$y[density_null$x<Avg.pos],2);
pvalue_rollmean=sum(xt*yt)
bench_mark[i,1]=true_integral 
bench_mark[i,2]=pvalue_integrate_xy
bench_mark[i,3]=pvalue_rollmean
message(i)}
#benchmark using root mean squared error
library(Metrics)
rmse(bench_mark[,1],bench_mark[,2])
rmse(bench_mark[,1],bench_mark[,3])

The third column (i.e. rollMean) has an RMSE of about 0.0019, while integrate.xy has an RMSE of .0026. 
