In this recipe we will first generate some Gaussian numbers and then, using the pnorm() and qnorm() functions, we will calculate the area to the left and to the right of x=2, and get the 90th quantile to plot the density.
- Generate 10000 Gaussian random numbers:
vals = rnorm(10000,0,1)
- Plot the density and draw a red line at x=2:
plot(density(vals))
abline(v=2,col="red")
- Calculate the area to the left and to the right of x=2, using the pnorm() function and use the qnorm() quantile function to get the 97.7th quantile:
print(paste("Area to the left of x=2",pnorm(2,0,1)))
print(paste("Area to the right of x=2",1-pnorm(2,0,1)))
print(paste("90th Quantile: x value that has 97.72% to the left",qnorm(0.9772,0,1)))
After running the preceding code, we get the following output:
The following screenshot shows the density with a vertical line at 2: