In this lab, you’ll investigate the probability distribution that is most central to statistics: the normal distribution. If you are confident that your 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.
In this lab, we will explore and visualize the data using the tidyverse suite of packages as well as the openintro package.
Let’s load the packages.
library(tidyverse)
library(openintro)
This week you’ll be working with fast food data. This data set contains data on 515 menu items from some of the most popular fast food restaurants worldwide. Let’s take a quick peek at the first few rows of the data.
Either you can use glimpse
like before, or head
to do this.
library(tidyverse)
library(openintro)
head(fastfood)
## # A tibble: 6 x 17
## restaurant item calories cal_fat total_fat sat_fat trans_fat cholesterol
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Mcdonalds Artisan G~ 380 60 7 2 0 95
## 2 Mcdonalds Single Ba~ 840 410 45 17 1.5 130
## 3 Mcdonalds Double Ba~ 1130 600 67 27 3 220
## 4 Mcdonalds Grilled B~ 750 280 31 10 0.5 155
## 5 Mcdonalds Crispy Ba~ 920 410 45 12 0.5 120
## 6 Mcdonalds Big Mac 540 250 28 10 1 80
## # ... with 9 more variables: sodium <dbl>, total_carb <dbl>, fiber <dbl>,
## # sugar <dbl>, protein <dbl>, vit_a <dbl>, vit_c <dbl>, calcium <dbl>,
## # salad <chr>
You’ll see that for every observation there are 17 measurements, many of which are nutritional facts.
You’ll be focusing on just two columns to get started:calories, calories from fat.
Let’s first focus on just products from Dairy Queen.
<- fastfood %>%
dairy_queen filter(restaurant == "Dairy Queen")
%>%
dairy_queenggplot(aes(x = cal_fat))+
geom_histogram()
%>%
dairy_queenggplot(aes(x = cal_fat))+
geom_density()
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, you 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. You’ll be focusing on calories from fat from Dairy Queen products, so let’s store them as a separate object and then calculate some statistics that will be referenced later.
<- mean(dairy_queen$cal_fat)
dqmean <- sd(dairy_queen$cal_fat) dqsd
Next, you 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.
ggplot(data = dairy_queen, aes(x = cal_fat)) +
geom_blank() +
geom_density(aes(y = ..density..)) +
stat_function(fun = dnorm, args = c(mean = dqmean, sd = dqsd), col = "red", lwd = 2)
After initializing a blank plot with geom_blank()
, the ggplot2
package (within the tidyverse
) 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 calories from fat. 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.
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”.
ggplot(data = dairy_queen, aes(sample = cal_fat)) +
geom_qq_line(aes(col = "red"), show.legend = F)+
geom_qq()
This time, you can use the geom_line()
layer, while specifying that you will be creating a Q-Q plot with the stat
argument. It’s important to note that here, instead of using x
instead aes()
, you need to use sample
.
The x-axis values correspond to the quantiles of a theoretically normal curve with mean 0 and standard deviation 1 (i.e., the standard normal distribution). The y-axis values correspond to the quantiles of the original unstandardized sample data. However, even if we were to standardize the sample data values, the Q-Q plot would look identical. A data set that is nearly normal will result in a probability plot where the points closely follow a diagonal line. Any deviations from normality leads to deviations of these points from that line.
The plot for Dairy Queen’s calories from fat shows points that tend to follow the line but with some errant points towards the upper tail. You’re left with the same problem that we encountered with the histogram above: how close is close enough?
Properties of the Normal Distribution: We have the following properties associated with the normal distribution. Consider \(X\sim N\left(\mu, \sigma\right)\).
The area beneath the entire distribution is 1 (since this is equivalent to the probability that \(X\) takes on any of its possible values).
\(\displaystyle{\mathbb{P}\left[X\leq \mu\right] = \mathbb{P}\left[X\geq \mu\right] = 0.5}\) (the area underneath a full half of the distribution is 0.5)
The distribution is symmetric. In symbols, \(\mathbb{P}\left[X\leq \mu - k\right] = \mathbb{P}\left[X \geq \mu + k\right]\) for any \(k\).
\(\displaystyle{\mathbb{P}\left[X = k\right] = 0}\) (the probability that \(X\) takes on any prescribed value exactly is \(0\))
We can use a similar technique for Normal Distribution from Binomial Distribution of pbinom()
to acquire the sum of probability of events for a certain number of successes and less than those for a given n. For normal distribution, we will make use of the function pnorm()
.
Let’s find the area under the curve for the given plot.
So to find this area, we will use the following R code:
pnorm(1.5, mean = 0, sd = 1)
## [1] 0.9331928
Now, let’s use this function to find the following areas:
Let’s consider this situation. The average daily high temperature in June in LA is 77\(^{\circ}\)F with a standard deviation of 5\(^{\circ}\)F. Suppose that the temperatures in June closely follow a normal distribution.
So, we can use a value, mean and sd to find the area under the curve. But we can use area under the curve, mean and sd to find the exact value as well. To do so we will make use of qnorm(p, mean, sd)
Let’s say for instance, "x*" is 1.96 on the standard normal curve. Then the area under the curve would be
pnorm(1.96, mean = 0, sd = 1)
## [1] 0.9750021
Now, if we use this number in qnorm()
then we will get "x*" in return. That is,
qnorm(0.9750, mean = 0, sd = 1)
## [1] 1.959964
We tend to round the Z-score to two decimal places. So the outcome would be 1.96.
Using this principle let’s answer the following exercises using the example from earlier.