4

I tried to calculate the integral of the density of the normal distribution with expected value 200 and standard deviation 20. From -Inf to Inf this should be 1.

I get the following:

> integrate(dnorm, mean=200, sd=20,-Inf, Inf)$value
[1] 1.429508e-08

For expected values below 169 I get the right value, 1. How can I get the right value for bigger expected values?

Richard Telford
  • 9,558
  • 6
  • 38
  • 51
Juli Schmitt
  • 41
  • 1
  • 2

2 Answers2

6

Alternatively

integrate(dnorm, mean=200, sd=20, lower= -Inf, upper= Inf, abs.tol = 0)$value
[1] 1

See here.

And to see what is going on, note the number of subdivisions in the following:

js <- integrate(dnorm, mean=200, sd=20, lower = -Inf, upper = Inf)
as <- integrate(dnorm, mean=200, sd=20, lower = -1e4, upper = 1e4)
cj <- integrate(dnorm, mean=200, sd=20, lower = -Inf, upper = Inf, abs.tol = 0)

str(js)
List of 5
 $ value       : num 1.43e-08
 $ abs.error   : num 2.77e-08
 $ subdivisions: int 2
 $ message     : chr "OK"
 $ call        : language integrate(f = dnorm, lower = -Inf, upper = Inf, mean = 200, sd = 20)
 - attr(*, "class")= chr "integrate"

str(as)
List of 5
 $ value       : num 1
 $ abs.error   : num 2e-07
 $ subdivisions: int 9
 $ message     : chr "OK"
 $ call        : language integrate(f = dnorm, lower = -10000, upper = 10000, mean = 200,      sd = 20)
 - attr(*, "class")= chr "integrate"

str(cj)
List of 5
 $ value       : num 1
 $ abs.error   : num 9.37e-05
 $ subdivisions: int 12
 $ message     : chr "OK"
 $ call        : language integrate(f = dnorm, lower = -Inf, upper = Inf, mean = 200, sd = 20,      abs.tol = 0)
 - attr(*, "class")= chr "integrate"
coffeinjunky
  • 11,254
  • 39
  • 57
3

Setting the interval finite seems to help

integrate(dnorm, mean=200, sd=20, -1e4, 1e4)
# 1 with absolute error < 2e-07
Andrey Shabalin
  • 4,389
  • 1
  • 19
  • 18