In this lab we’ll investigate the probability distribution that is most central to statistics: the normal distribution. If we are confident that our data are nearly normal, that opens the door to many powerful statistical methods. Here we’ll use the graphical tools of R to assess the normality of our data and also learn how to generate random numbers from a normal distribution.

Note on the lab write up. If you want to use an iscam function, but you don’t want the plot to print (in the knitted file), then type the following as the beginning of the chunk:

  to start R chunk: ```{r fig.keep="none"}

A goal for today is to make sure you know how to compute probabilities and cutoff values using both the normal distributions (iscamnormprob, iscaminvnorm, pnorm, and qnorm).

The Data

This week we’ll be working with measurements of body dimensions. This data set contains measurements from 247 men and 260 women, most of whom were considered healthy young adults. Let’s take a quick peek at the first few rows of the data.

library(dplyr)
library(ggplot2)
library(oilabs)
# note that you can load ISCAM.RData to get the iscamnormprob function
load(url("http://www.rossmanchance.com/iscam3/ISCAM.RData"))
data(bdims)  # The data live in the oilabs package, so we load them in with the data() command
glimpse(bdims)  # don't pring this when you turn in the lab!!
## Observations: 507
## Variables: 25
## $ bia.di <dbl> 42.9, 43.7, 40.1, 44.3, 42.5, 43.3, 43.5, 44.4, 43.5, 4...
## $ bii.di <dbl> 26.0, 28.5, 28.2, 29.9, 29.9, 27.0, 30.0, 29.8, 26.5, 2...
## $ bit.di <dbl> 31.5, 33.5, 33.3, 34.0, 34.0, 31.5, 34.0, 33.2, 32.1, 3...
## $ che.de <dbl> 17.7, 16.9, 20.9, 18.4, 21.5, 19.6, 21.9, 21.8, 15.5, 2...
## $ che.di <dbl> 28.0, 30.8, 31.7, 28.2, 29.4, 31.3, 31.7, 28.8, 27.5, 2...
## $ elb.di <dbl> 13.1, 14.0, 13.9, 13.9, 15.2, 14.0, 16.1, 15.1, 14.1, 1...
## $ wri.di <dbl> 10.4, 11.8, 10.9, 11.2, 11.6, 11.5, 12.5, 11.9, 11.2, 1...
## $ kne.di <dbl> 18.8, 20.6, 19.7, 20.9, 20.7, 18.8, 20.8, 21.0, 18.9, 2...
## $ ank.di <dbl> 14.1, 15.1, 14.1, 15.0, 14.9, 13.9, 15.6, 14.6, 13.2, 1...
## $ sho.gi <dbl> 106.2, 110.5, 115.1, 104.5, 107.5, 119.8, 123.5, 120.4,...
## $ che.gi <dbl> 89.5, 97.0, 97.5, 97.0, 97.5, 99.9, 106.9, 102.5, 91.0,...
## $ wai.gi <dbl> 71.5, 79.0, 83.2, 77.8, 80.0, 82.5, 82.0, 76.8, 68.5, 7...
## $ nav.gi <dbl> 74.5, 86.5, 82.9, 78.8, 82.5, 80.1, 84.0, 80.5, 69.0, 8...
## $ hip.gi <dbl> 93.5, 94.8, 95.0, 94.0, 98.5, 95.3, 101.0, 98.0, 89.5, ...
## $ thi.gi <dbl> 51.5, 51.5, 57.3, 53.0, 55.4, 57.5, 60.9, 56.0, 50.0, 5...
## $ bic.gi <dbl> 32.5, 34.4, 33.4, 31.0, 32.0, 33.0, 42.4, 34.1, 33.0, 3...
## $ for.gi <dbl> 26.0, 28.0, 28.8, 26.2, 28.4, 28.0, 32.3, 28.0, 26.0, 2...
## $ kne.gi <dbl> 34.5, 36.5, 37.0, 37.0, 37.7, 36.6, 40.1, 39.2, 35.5, 3...
## $ cal.gi <dbl> 36.5, 37.5, 37.3, 34.8, 38.6, 36.1, 40.3, 36.7, 35.0, 3...
## $ ank.gi <dbl> 23.5, 24.5, 21.9, 23.0, 24.4, 23.5, 23.6, 22.5, 22.0, 2...
## $ wri.gi <dbl> 16.5, 17.0, 16.9, 16.6, 18.0, 16.9, 18.8, 18.0, 16.5, 1...
## $ age    <int> 21, 23, 28, 23, 22, 21, 26, 27, 23, 21, 23, 22, 20, 26,...
## $ wgt    <dbl> 65.6, 71.8, 80.7, 72.6, 78.8, 74.8, 86.4, 78.4, 62.0, 8...
## $ hgt    <dbl> 174.0, 175.3, 193.5, 186.5, 187.2, 181.5, 184.0, 184.5,...
## $ sex    <fctr> m, m, m, m, m, m, m, m, m, m, m, m, m, m, m, m, m, m, ...

You’ll see that for every observation we have 25 measurements, many of which are either diameters or girths. You can learn about what the variable names mean by bringing up the help page.

?bdims

We’ll be focusing on just three columns to get started: weight in kg (wgt), height in cm (hgt), and sex (m indicates male, f indicates female).

Since males and females tend to have different body dimensions, it will be useful to create two additional data sets: one with only men and another with only women.

mdims <- bdims %>%
  filter(sex == "m")
fdims <- bdims %>%
  filter(sex == "f")
  1. Make a plot (or plots) to visualize the distributions of men’s and women’s heights.
    How do their centers, shapes, and spreads compare? Try the qplot function!
qplot(... geom="histogram")

The normal distribution

In your description of the distributions, did you use words like bell-shaped or normal? It’s tempting to say so when faced with a unimodal symmetric distribution.

To see how accurate that description is, we can plot a normal distribution curve on top of a histogram to see how closely the data follow a normal distribution. This normal curve should have the same mean and standard deviation as the data. We’ll be working with women’s heights first, so let’s store them as a separate object and then calculate some statistics that will be referenced later.

fhgtmean <- mean(fdims$hgt)
fhgtsd   <- sd(fdims$hgt)

Next we make a density histogram to use as the backdrop and use the lines function to overlay a normal probability curve. The difference between a frequency histogram and a density histogram is that while in a frequency histogram the heights of the bars add up to the total number of observations, in a density histogram the areas of the bars add up to 1. The area of each bar can be calculated as simply the height times the width of the bar. Using a density histogram allows us to properly overlay a normal distribution curve over the histogram since the curve is a normal probability density function that also has area under the curve of 1. Frequency and density histograms both display the same exact shape; they only differ in their y-axis. You can verify this by comparing the frequency histogram you constructed earlier and the density histogram created by the commands below.

qplot(x = hgt, data = fdims, geom = "blank") +
  geom_histogram(aes(y = ..density..)) +
  stat_function(fun = dnorm, args = c(mean = fhgtmean, sd = fhgtsd), col = "tomato")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

After initializing a blank plot with the first command, the ggplot2 package allows us to add additional layers. The first layer is a density histogram. The second layer is a statistical function – the density of the normal curve, dnorm. We specify that we want the curve to have the same mean and standard deviation as the column of female heights. The argument col simply sets the color for the line to be drawn. If we left it out, the line would be drawn in black.

  1. Based on the this plot, does it appear that the female heights follow a nearly normal distribution? What about the male heights? Explain in words as well as with a picture.

Evaluating the normal distribution

Eyeballing the shape of the histogram is one way to determine if the data appear to be nearly normally distributed, but it can be frustrating to decide just how close the histogram is to the curve. An alternative approach involves constructing a normal probability plot, also called a normal Q-Q plot for “quantile-quantile”. We may come back to q-q plots later in the course, but for now, we’ll know they exist if we are concerned about making sure the density is normal.

Normal probabilities

Okay, so if we believe that a variable is normally distributed, why should we care?

It turns out that statisticians know a lot about the normal distribution. Once we decide that a random variable is approximately normal, we can answer all sorts of questions about that variable related to probability. Take, for example, the question of, “What is the probability that a randomly chosen young adult female is taller than 6 feet (about 183 cm)?” (The study that published the data set we are working with is clear to point out that the sample was not random and therefore inference to a general population is not suggested. We do so here only as an exercise.)

If we assume that female heights are normally distributed (a very close approximation is also okay), we can find the relevant probability by calculating a Z score and consulting a Z table (also called a normal probability table). In R, this is done in one step with the function pnorm() or the ISCAM function iscamnormprob.

1 - pnorm(q = 183, mean = fhgtmean, sd = fhgtsd)
## [1] 0.00280394
iscamnormprob(183, mean = fhgtmean, sd = fhgtsd, direction = "above")

## probability: 0.002804

Note that the function pnorm() gives the area under the normal curve below a given value, q, with a given mean and standard deviation. Since we’re interested in the probability that someone is taller than 183 cm, we have to take one minus that probability.

Assuming a normal distribution has allowed us to calculate a theoretical probability. If we want to calculate the probability empirically, we simply need to determine how many observations fall above 183 then divide this number by the total sample size (the number of rows in the female data set).

fdims %>% 
  filter(hgt > 183) %>%
  summarise(percent = n() / nrow(fdims))
## # A tibble: 1 × 1
##   percent
##     <dbl>
## 1       0

Although the probabilities are not exactly the same, they are reasonably close. The closer that your distribution is to being normal, the more accurate the theoretical probabilities will be.

  1. Write out two probability questions that you would like to answer; one regarding female heights and one regarding female weights. Calculate those probabilities using both the theoretical normal distribution as well as the empirical distribution (four probabilities in all). Which variable, height or weight, had a closer agreement between the two methods?

Normal quantiles

Not only can we calculate normal probabilities at a particular cutoff, but we can also calculate the inverse question: the cutoff value associated with a particular percentile.

  1. Before moving on, assume you have a standard normal curve (N(0,1)) and ask yourself if you know the cutoff value associated with 0.025 probability in the upper tail? What about the cutoff value of 0.16 in the lower tail? (Hint: you should be able to get good estimates of both of those values without using R.)
iscaminvnorm(0.025, mean=0, sd=1, direction="above")
iscaminvnorm(0.16, mean=0, sd=1, direction="below")
# for questions about an ISCAM function type:
iscaminvnorm("?")

Just like the standard normal curve, we can estimate cutoffs for any normal distribution and any probability. Back to the body dimensions data set, let’s find cutoff values associated with particular probabilities.

  1. Using the data on body dimensions: find the theoretical cutoff value such that 20% of people are taller than it. Find the heights where theoretically the middle 50% of people range. Find height corresponding to the lower 10% of women, theoretically.

  2. Check your data to see that the empirical (data) proportions are roughly the same as the theoretical (normal) probabilities from the previous question.


To Turn In

  1. Plot the age variable.

  2. Does the age variable follow the empirical rule? (To answer this question will take a series of computations in R.) Explain your results.

  3. Consider the population of men (yes, there are female jockeys, but so few that we ask the question only of men). To be a horse jockey, you need to weigh between 108 and 118 lbs. What percent of the men in the dataset could be horse jockeys? If the data are representative of a normal population of weights, what theoretical percent of men in the population could be a horse jockeys? (Hint: weight is coded in kg, not lbs.)

  4. On the other side, what percent of men would be required to box at the heavyweight weight class (above 200 lbs)? Calculate the probability empirically and theoretically under a normal distribution centered from the data given.

This is a product of OpenIntro that is released under a Creative Commons Attribution-ShareAlike 3.0 Unported. This lab was adapted for OpenIntro by Andrew Bray and Mine Çetinkaya-Rundel from a lab written by Mark Hansen of UCLA Statistics.