Search code examples
rnormal-distributionquartile

R function to calculate area under the normal curve between adjacent standard deviations


I'm looking into GoF (goodness of fit) testing, and wanted to see if the quantiles of a vector of data followed the expected frequency of a normal distribution N(0, 1), and before running the chi square test, I generated these frequencies for the normal distribution:

< -2 SD's (standard deviations), between -2 and -1 SD's, between -1 and 0 SD's, between 0 and 1 SD's, between 1 and 2 SD's, and more than 2 SD's.

To do so I took the long route:

(Normal_distr <- c(pnorm(-2), pnorm(-1) - pnorm(-2), pnorm(0) - pnorm(-1),
                  pnorm(1) - pnorm(0), pnorm(2) - pnorm(1), pnorm(2, lower.tail = F)))

[1]    0.02275013 0.13590512 0.34134475 0.34134475 0.13590512 0.02275013

I see that the symmetry allows me to cut down the length of the code, but isn't there an easier way... something (I don't think this will work, but the idea of...) like pnorm(-2:-1) returning an identical value to pnorm(-1) - pnorm(-2) = 0.13590512?

Question: Is there an R function that calculates the area under the normal curve between quantiles so that we can pass a vector such as c(-3:3) through it, as opposed to subtracting pnorm()'s of adjacent standard deviations or other quantiles?


Solution

  • I'n not sure if there is a specific function to do this, but you can do it pretty simply like so:

    #Get difference between adjacent quantiles
    diff(pnorm(-2:-1))
    [1] 0.1359051
    
    #Get area under normal curve from -2 to 2 sd's:
    sum(diff(pnorm(-2:2)))
    [1] 0.9544997