I’m not understanding how to fix this problem. G is a set of values for which I know that the range is [0, 1e+12].
> G = c(500,10000, 5001, 103, 10, 10000)
> H = density(G)
> sum(diff(H$x)*H$y) # Area under the curve should be 1
[1] 0.999989
However, to be able to compare two datasets, I want to provide from and to to the density function so that it makes sense to compare these distributions. Therefore, I did this:
> H = density(G, from=0, to=1e+12)
> sum(diff(H$x)*H$y)
[1] 2576.354
Warning message:
In diff(H$x) * H$y :
longer object length is not a multiple of shorter object length
The reason became evident when I printed the estimated densities:
[1] 1.315415e-04 1.102126e-07 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 9.471181e-26
[8] 1.565915e-22 2.563577e-26 4.272754e-23 0.000000e+00 0.000000e+00 8.951887e-26 1.516549e-22
[15] 3.870985e-25 6.612221e-22 0.000000e+00 0.000000e+00 1.275922e-25 2.216194e-22 0.000000e+00
[22] 0.000000e+00 5.567175e-26 9.835567e-23 0.000000e+00 0.000000e+00 1.866999e-25 3.355962e-22
[29] 1.544394e-25 2.800493e-22 6.026824e-26 1.102559e-22 0.000000e+00 0.000000e+00 0.000000e+00
....
....
[484] 2.430850e-22 1.346442e-25 0.000000e+00 0.000000e+00 1.135491e-22 6.399622e-26 0.000000e+00
[491] 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00
[498] 8.914726e-23 5.240562e-26 2.189813e-22 1.297914e-25 4.773404e-22 2.852380e-25 3.289275e-22
[505] 1.981486e-25 1.382136e-23 8.393153e-27 8.090789e-24 4.952458e-27 3.042214e-24 1.876931e-27
[512] 1.809917e-22
While R is correctly estimating the density to be 0 at extreme points, some are non-zero. Is this a floating point error I’m facing or am I doing something fundamentally wrong? Any suggestions?
C’mon. You failed to reproduce the warning message. I suspect that is the key to the discrepancy. the
diff(x)function gives you a vector that is one element shorter than the y.R operations include argument recycling, so the first density diff element will get multiplied by the largest y element.