MTH 361 A/B SP23 (University of Portland): Lab 2 R Studio Cloud
Content Source: OpenIntro Statistics (4th edition) by David Diez, Mine Cetinkaya-Rundel, and Christopher Barr.
In this lab, we will explore and visualize the data using the
tidyverse
suite of packages. The data can be found in the
companion package for OpenIntro labs, openintro.
Let’s load the packages.
library(tidyverse)
library(openintro)
To create your new lab report, in RStudio, go to New File -> R
Markdown… Then, choose From Template and then choose
Lab Report for OpenIntro Statistics Labs
from the list of
templates.
Basketball players who make several baskets in succession are described as having a hot hand. Fans and players have long believed in the hot hand phenomenon, which refutes the assumption that each shot is independent of the next. However, a 1985 paper by Gilovich, Vallone, and Tversky collected evidence that contradicted this belief and showed that successive shots are independent events. This paper started a great controversy that continues to this day, as you can see by Googling hot hand basketball.
We do not expect to resolve this controversy today. However, in this lab we’ll apply one approach to answering questions like this. The goals for this lab are to (1) think about the effects of independent and dependent events, (2) learn how to simulate shooting streaks in R, and (3) to compare a simulation to actual data in order to determine if the hot hand phenomenon appears to be real.
Your investigation will focus on the performance of one player: Kobe Bryant of the
Los Angeles Lakers. His performance against the Orlando Magic in the 2009 NBA Finals
earned him the title Most Valuable Player and many spectators
commented on how he appeared to show a hot hand. The data file we’ll use
is called kobe_basket
.
glimpse(kobe_basket)
This data frame contains 133 observations and 6 variables, where
every row records a shot taken by Kobe Bryant. The shot
variable in this dataset indicates whether the shot was a hit
(H
) or a miss (M
).
Just looking at the string of hits and misses, it can be difficult to gauge whether or not it seems like Kobe was shooting with a hot hand. One way we can approach this is by considering the belief that hot hand shooters tend to go on shooting streaks. For this lab, we define the length of a shooting streak to be the number of consecutive baskets made until a miss occurs.
For example, in Game 1 Kobe had the following sequence of hits and misses from his nine shot attempts in the first quarter:
\[ \textrm{H M | M | H H M | M | M | M} \]
You can verify this by viewing the first 9 rows of the data in the data viewer.
Within the nine shot attempts, there are six streaks, which are separated by a “|” above. Their lengths are one, zero, two, zero, zero, zero (in order of occurrence).
Counting streak lengths manually for all 133 shots would get tedious,
so we’ll use the custom function calc_streak
to calculate
them, and store the results in a data frame called
kobe_streak
as the length
variable.
<- calc_streak(kobe_basket$shot) kobe_streak
We can then take a look at the distribution of these streak lengths.
ggplot(data = kobe_streak, aes(x = length)) +
geom_bar()
We’ve shown that Kobe had some long shooting streaks, but are they long enough to support the belief that he had a hot hand? What can we compare them to?
To answer these questions, let’s return to the idea of independence. Two processes are independent if the outcome of one process doesn’t effect the outcome of the second. If each shot that a player takes is an independent process, having made or missed your first shot will not affect the probability that you will make or miss your second shot.
A shooter with a hot hand will have shots that are not independent of one another. Specifically, if the shooter makes his first shot, the hot hand model says he will have a higher probability of making his second shot.
Let’s suppose for a moment that the hot hand model is valid for Kobe. During his career, the percentage of time Kobe makes a basket (i.e. his shooting percentage) is about 45%, or in probability notation,
\[ P(\textrm{shot 1 = H}) = 0.45 \]
If he makes the first shot and has a hot hand (not independent shots), then the probability that he makes his second shot would go up to, let’s say, 60%,
\[ P(\textrm{shot 2 = H} \, | \, \textrm{shot 1 = H}) = 0.60 \]
As a result of these increased probabilities, you’d expect Kobe to have longer streaks. Compare this to the skeptical perspective where Kobe does not have a hot hand, where each shot is independent of the next. If he hit his first shot, the probability that he makes the second is still 0.45.
\[ P(\textrm{shot 2 = H} \, | \, \textrm{shot 1 = H}) = 0.45 \]
In other words, making the first shot did nothing to effect the probability that he’d make his second shot. If Kobe’s shots are independent, then he’d have the same probability of hitting every shot regardless of his past shots: 45%.
Now that we’ve phrased the situation in terms of independent shots, let’s return to the question: how do we tell if Kobe’s shooting streaks are long enough to indicate that he has a hot hand? We can compare his streak lengths to someone without a hot hand: an independent shooter.
While we don’t have any data from a shooter we know to have independent shots, that sort of data is very easy to simulate in R. In a simulation, you set the ground rules of a random process and then the computer uses random numbers to generate an outcome that adheres to those rules. As a simple example, you can simulate flipping a fair coin with the following.
<- c("heads", "tails")
coin_outcomes sample(coin_outcomes, size = 1, replace = TRUE)
The vector coin_outcomes
can be thought of as a hat with
two slips of paper in it: one slip says heads
and the other
says tails
. The function sample
draws one slip
from the hat and tells us if it was a head or a tail.
Run the second command listed above several times. Just like when flipping a coin, sometimes you’ll get a heads, sometimes you’ll get a tails, but in the long run, you’d expect to get roughly equal numbers of each.
If you wanted to simulate flipping a fair coin 100 times, you could
either run the function 100 times or, more simply, adjust the
size
argument, which governs how many samples to draw (the
replace = TRUE
argument indicates we put the slip of paper
back in the hat before drawing again). Save the resulting vector of
heads and tails in a new object called sim_fair_coin
.
<- sample(coin_outcomes, size = 100, replace = TRUE) sim_fair_coin
To view the results of this simulation, type the name of the object
and then use table
to count up the number of heads and
tails.
sim_fair_cointable(sim_fair_coin)
Since there are only two elements in coin_outcomes
, the
probability that we “flip” a coin and it lands heads is 0.5. Say we’re
trying to simulate an unfair coin that we know only lands heads 20% of
the time. We can adjust for this by adding an argument called
prob
, which provides a vector of two probability
weights.
<- sample(coin_outcomes, size = 100, replace = TRUE,
sim_unfair_coin prob = c(0.2, 0.8))
prob=c(0.2, 0.8)
indicates that for the two elements in
the outcomes
vector, we want to select the first one,
heads
, with probability 0.2 and the second one,
tails
with probability 0.8. Another way of thinking about
this is to think of the outcome space as a bag of 10 chips, where 2
chips are labeled “head” and 8 chips “tail”. Therefore at each draw, the
probability of drawing a chip that says “head”” is 20%, and “tail” is
80%.
A note on setting a seed: Setting a seed will cause R to select the same sample each time you knit your document. This will make sure your results don’t change each time you knit, and it will also ensure reproducibility of your work (by setting the same seed it will be possible to reproduce your results). You can set a seed like this:
set.seed(35797) # make sure to change the seed
The number above is completely arbitraty. If you need inspiration, you can use your ID, birthday, or just a random string of numbers. The important thing is that you use each seed only once in a document. Remember to do this before you sample in the exercise above.
In a sense, we’ve shrunken the size of the slip of paper that says
“heads”, making it less likely to be drawn, and we’ve increased the size
of the slip of paper saying “tails”, making it more likely to be drawn.
When you simulated the fair coin, both slips of paper were the same
size. This happens by default if you don’t provide a prob
argument; all elements in the outcomes
vector have an equal
probability of being drawn.
If you want to learn more about sample
or any other
function, recall that you can always check out its help file.
?sample
Simulating a basketball player who has independent shots uses the same mechanism that you used to simulate a coin flip. To simulate a single shot from an independent shooter with a shooting percentage of 50% you can type
<- c("H", "M")
shot_outcomes <- sample(shot_outcomes, size = 1, replace = TRUE) sim_basket
To make a valid comparison between Kobe and your simulated independent shooter, you need to align both their shooting percentage and the number of attempted shots.
sample
function so
that it reflects a shooting percentage of 45%? Make this adjustment,
then run a simulation to sample 133 shots. Assign the output of this
simulation to a new object called sim_basket
.Note that we’ve named the new vector sim_basket
, the
same name that we gave to the previous vector reflecting a shooting
percentage of 50%. In this situation, R overwrites the old object with
the new one, so always make sure that you don’t need the information in
an old vector before reassigning its name.
With the results of the simulation saved as sim_basket
,
you have the data necessary to compare Kobe to our independent
shooter.
Both data sets represent the results of 133 shot attempts, each with the same shooting percentage of 45%. We know that our simulated data is from a shooter that has independent shots. That is, we know the simulated shooter does not have a hot hand.
Using calc_streak
, compute the streak lengths of
sim_basket
, and save the results in a data frame called
sim_streak
.
Describe the distribution of streak lengths. What is the typical streak length for this simulated independent shooter with a 45% shooting percentage? How long is the player’s longest streak of baskets in 133 shots? Make sure to include a plot in your answer.
If you were to run the simulation of the independent shooter a second time, how would you expect its streak distribution to compare to the distribution from the question above? Exactly the same? Somewhat similar? Totally different? Explain your reasoning.
How does Kobe Bryant’s distribution of streak lengths compare to the distribution of streak lengths for the simulated shooter? Using this comparison, do you have evidence that the hot hand model fits Kobe’s shooting patterns? Explain.
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.
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 × 17
## restaur…¹ item calor…² cal_fat total…³ sat_fat trans…⁴ chole…⁵ sodium total…⁶
## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 Mcdonalds Arti… 380 60 7 2 0 95 1110 44
## 2 Mcdonalds Sing… 840 410 45 17 1.5 130 1580 62
## 3 Mcdonalds Doub… 1130 600 67 27 3 220 1920 63
## 4 Mcdonalds Gril… 750 280 31 10 0.5 155 1940 62
## 5 Mcdonalds Cris… 920 410 45 12 0.5 120 1980 81
## 6 Mcdonalds Big … 540 250 28 10 1 80 950 46
## # … with 7 more variables: fiber <dbl>, sugar <dbl>, protein <dbl>,
## # vit_a <dbl>, vit_c <dbl>, calcium <dbl>, salad <chr>, and abbreviated
## # variable names ¹restaurant, ²calories, ³total_fat, ⁴trans_fat,
## # ⁵cholesterol, ⁶total_carb
You’ll see that for every observation there are 17 measurements, many of which are nutritional facts.
You’ll be focusing on just three columns to get started: restaurant, calories, calories from fat.
Let’s first focus on just products from McDonalds and Dairy Queen.
<- fastfood %>%
mcdonalds filter(restaurant == "Mcdonalds")
<- fastfood %>%
dairy_queen filter(restaurant == "Dairy Queen")
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_histogram(aes(y = ..density..)) +
stat_function(fun = dnorm, args = c(mean = dqmean, sd = dqsd), col = "tomato")
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
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_line(stat = "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?
A useful way to address this question is to rephrase it as: what do
probability plots look like for data that I know came from a
normal distribution? We can answer this by simulating data from a normal
distribution using rnorm
.
<- rnorm(n = nrow(dairy_queen), mean = dqmean, sd = dqsd) sim_norm
The first argument indicates how many numbers you’d like to generate,
which we specify to be the same number of menu items in the
dairy_queen
data set using the nrow()
function. The last two arguments determine the mean and standard
deviation of the normal distribution from which the simulated sample
will be generated. You can take a look at the shape of our simulated
data set, sim_norm
, as well as its normal probability
plot.
sim_norm
. Do all of
the points fall on the line? How does this plot compare to the
probability plot for the real data? (Since sim_norm
is not
a dataframe, it can be put directly into the sample
argument and the data
argument can be dropped.)Even better than comparing the original plot to a single plot generated from a normal distribution is to compare it to many more plots using the following function. It shows the Q-Q plot corresponding to the original data in the top left corner, and the Q-Q plots of 8 different simulated normal data. It may be helpful to click the zoom button in the plot window.
qqnormsim(sample = cal_fat, data = dairy_queen)
Does the normal probability plot for the calories from fat look similar to the plots created for the simulated data? That is, do the plots provide evidence that the calories from fat are nearly normal?
Using the same technique, determine whether or not the calories from McDonald’s menu appear to come from a normal distribution.
Okay, so now you have a slew of tools to judge whether or not a variable is normally distributed. Why should you care?
It turns out that statisticians know a lot about the normal distribution. Once you decide that a random variable is approximately normal, you 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 Dairy Queen product has more than 600 calories from fat?”
If we assume that the calories from fat from Dairy Queen’s menu are
normally distributed (a very close approximation is also okay), we can
find this 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()
.
1 - pnorm(q = 600, mean = dqmean, sd = dqsd)
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 a
Dairy Queen item has more than 600 calories from fat, 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 600 then divide this number by the total sample size.
%>%
dairy_queen filter(cal_fat > 600) %>%
summarise(percent = n() / nrow(dairy_queen))
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.
Now let’s consider some of the other variables in the dataset. Out of all the different restaurants, which ones’ distribution is the closest to normal for sodium?
Note that some of the normal probability plots for sodium distributions seem to have a stepwise pattern. why do you think this might be the case?
As you can see, normal probability plots can be used both to
assess normality and visualize skewness. Make a normal probability plot
for the total carbohydrates from a restaurant of your choice. Based on
this normal probability plot, is this variable left skewed, symmetric,
or right skewed?
Use a histogram to confirm your findings.
This
work is licensed under a
Creative
Commons Attribution-ShareAlike 4.0 International License.