# The z-distribution
#
# R's function 'pnorm' calculates the area under the normal distribution
# below a z-score. By default, it uses a standard normal distribution
# (mean 0, s.d. 1).
#
# For example, to find the area under the standard normal below z=1, we use:
pnorm(1)
# Note that the tables in the book and the Excel spreadsheet give you
# the areas ABOVE z. To get the values in these tables we need to subtract
# the result from 'pnorm' from 1:
1-pnorm(1)
# You should see that this value matches the value in the third column in
# the standard normal table for z=1.
# The area under the standard normal below z = -2 is:
pnorm(-2)
# The area under the standard normal between z=1 and z=2 can be found
# by finding the area below z=2 and subtracting the area below z=1:
pnorm(2) - pnorm(1)
# The area between z = -2 and z=1 can be caculated similarly:
pnorm(1) - pnorm(-2)
# To go the other way and find z-scores from areas we use
# the function 'qnorm'.
#
# For example, the z-score for which 95% of the area of the
# standard normal falls below is:
qnorm(.95)
# This is, of course the same as the z-score for which 5% falls above.
# To find the z-score for which 10% of the area lies below we use:
qnorm(.1)
# To find the values of z that bracket the middle 95% is found
# by first finding the lower z-score. This is the z-score
# for which only 2.5% falls below:
qnorm(.025)
# Since the z-distribution is symmetric, the upper value of z
# is just the positive sign of this:
-qnorm(.025)
# In general, if you want to find the z scores that bracket
# the middle p% we can first set a variable 'p' to some value:
p <- .5
# And then use 'c' to concatenate the two z-scores into a single
# vector:
c(qnorm((1-p)/2),-qnorm((1-p)/2))