[R] Integral of PDF

Samuel Le Samuel.Le at srlglobal.com
Fri Dec 3 09:57:13 CET 2010


Hi,

You need to be careful with -Inf and Inf in R, I suppose they are some large numbers arbitrarily chosen by R, but not infinite quantities. So the function integrate can return errors if the function to integrate doesn't have negligible values beyond those large numbers.
I ran the following to convince myself:

> integrate(function(x){return(ifelse(x<500,0,1/(x^2)))},-Inf,Inf)
0 with absolute error < 0
> integrate(function(x){return(ifelse(x<500,0,1/(x^2)))},500,Inf)
0.001999999 with absolute error < 0.000011

The second result shouldn't be greater that the first one but...

And to solve your problem:
> integrate(function(x) dnorm(x, 500,50), 500-1000, 500+1000)
1 with absolute error < 0.000074

I centred the interval of integration on 500 instead of on 0 if you integrate between -Inf and Inf.

HTH,

Samuel

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Doran, Harold
Sent: 02 December 2010 21:22
To: r-help at r-project.org
Subject: [R] Integral of PDF

The integral of any probability density from -Inf to Inf should equal 1, correct? I don't understand last result below.

> integrate(function(x) dnorm(x, 0,1), -Inf, Inf)
1 with absolute error < 9.4e-05

> integrate(function(x) dnorm(x, 100,10), -Inf, Inf)
1 with absolute error < 0.00012

> integrate(function(x) dnorm(x, 500,50), -Inf, Inf)
8.410947e-11 with absolute error < 1.6e-10

> all.equal(integrate(function(x) dnorm(x, 500,50), -Inf, Inf)$value, 0)
[1] TRUE

> sessionInfo()
R version 2.10.1 (2009-12-14)
i386-pc-mingw32

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] statmod_1.4.6      mlmRev_0.99875-1   lme4_0.999375-35   Matrix_0.999375-33 lattice_0.17-26

loaded via a namespace (and not attached):
[1] grid_2.10.1   nlme_3.1-96   stats4_2.10.1 tools_2.10.1

        [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
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.


__________ Information from ESET NOD32 Antivirus, version of virus signature database 5668 (20101202) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus signature database 5669 (20101203) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



More information about the R-help mailing list