library(tidyverse)
## ── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.2 ✓ purrr 0.3.4
## ✓ tibble 3.0.3 ✓ dplyr 1.0.2
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.5.0
## ── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
# import data
kobe_basket
## # A tibble: 133 x 6
## vs game quarter time description shot
## <fct> <int> <fct> <fct> <fct> <chr>
## 1 ORL 1 1 9:47 Kobe Bryant makes 4-foot two point shot H
## 2 ORL 1 1 9:07 Kobe Bryant misses jumper M
## 3 ORL 1 1 8:11 Kobe Bryant misses 7-foot jumper M
## 4 ORL 1 1 7:41 Kobe Bryant makes 16-foot jumper (Derek Fish… H
## 5 ORL 1 1 7:03 Kobe Bryant makes driving layup H
## 6 ORL 1 1 6:01 Kobe Bryant misses jumper M
## 7 ORL 1 1 4:07 Kobe Bryant misses 12-foot jumper M
## 8 ORL 1 1 0:52 Kobe Bryant misses 19-foot jumper M
## 9 ORL 1 1 0:00 Kobe Bryant misses layup M
## 10 ORL 1 2 6:35 Kobe Bryant makes jumper H
## # … with 123 more rows
glimpse(kobe_basket)
## Rows: 133
## Columns: 6
## $ vs <fct> ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, OR…
## $ game <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ quarter <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, …
## $ time <fct> 9:47, 9:07, 8:11, 7:41, 7:03, 6:01, 4:07, 0:52, 0:00, 6:3…
## $ description <fct> Kobe Bryant makes 4-foot two point shot, Kobe Bryant miss…
## $ shot <chr> "H", "M", "M", "H", "H", "M", "M", "M", "M", "H", "H", "H…
#Examine first few rows
head(kobe_basket)
## # A tibble: 6 x 6
## vs game quarter time description shot
## <fct> <int> <fct> <fct> <fct> <chr>
## 1 ORL 1 1 9:47 Kobe Bryant makes 4-foot two point shot H
## 2 ORL 1 1 9:07 Kobe Bryant misses jumper M
## 3 ORL 1 1 8:11 Kobe Bryant misses 7-foot jumper M
## 4 ORL 1 1 7:41 Kobe Bryant makes 16-foot jumper (Derek Fishe… H
## 5 ORL 1 1 7:03 Kobe Bryant makes driving layup H
## 6 ORL 1 1 6:01 Kobe Bryant misses jumper M
What does a streak length of 1 mean, i.e. how many hits and misses are in a streak of 1? What about a streak length of 0?
# Use custom function to calculate streak length and store in new variable
kobe_streak <- calc_streak(kobe_basket$shot)
The length of a shooting streak is the number of consecutive baskets made until a miss occurs. A streak length of 1 means that Bryant has made one basket, then he misses the next shot. A streak length of 0 means that Bryant has missed the shot.
Describe the distribution of Kobe’s streak lengths from the 2009 NBA finals. What was his typical streak length? How long was his longest streak of baskets? Make sure to include the accompanying plot in your answer.
# Find average length of Kobe's streak
mean(kobe_streak$length)
## [1] 0.7631579
# Find max length of Kobe's streaks
max(kobe_streak$length)
## [1] 4
Kobe’s typical streak length was between 0 and 1. His longest streak of baskets was 4.
# Plot distribution of streak length
ggplot(data = kobe_streak, aes(x = length)) +
geom_bar()+
labs(title = "Kobe Bryant Shooting Streaks")
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.
coin_outcomes <- c("heads", "tails")
sample(coin_outcomes, size = 1, replace = TRUE)
## [1] "tails"
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.
sim_fair_coin <- sample(coin_outcomes, size = 100, replace = TRUE)
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_coin
## [1] "heads" "heads" "heads" "heads" "heads" "tails" "heads" "tails" "heads"
## [10] "tails" "tails" "tails" "tails" "heads" "heads" "tails" "tails" "tails"
## [19] "heads" "heads" "tails" "heads" "heads" "tails" "heads" "heads" "tails"
## [28] "heads" "heads" "tails" "tails" "heads" "heads" "heads" "tails" "tails"
## [37] "tails" "tails" "heads" "heads" "tails" "tails" "heads" "tails" "tails"
## [46] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "heads" "heads"
## [55] "heads" "tails" "heads" "tails" "heads" "heads" "tails" "heads" "tails"
## [64] "tails" "tails" "tails" "tails" "heads" "tails" "tails" "heads" "heads"
## [73] "tails" "tails" "heads" "heads" "tails" "tails" "heads" "tails" "heads"
## [82] "heads" "heads" "heads" "tails" "tails" "tails" "tails" "tails" "heads"
## [91] "tails" "tails" "tails" "tails" "heads" "tails" "heads" "tails" "tails"
## [100] "tails"
table(sim_fair_coin)
## sim_fair_coin
## heads tails
## 43 57
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.
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE,
prob = c(0.2, 0.8))
In your simulation of flipping the unfair coin 100 times, how many flips came up heads? Include the code for sampling the unfair coin in your response. Since the markdown file will run the code, and generate a new sample each time you Knit it, you should also “set a seed” before you sample.
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).
set.seed(4)
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE,
prob = c(0.2, 0.8))
sim_unfair_coin
## [1] "tails" "tails" "tails" "tails" "heads" "tails" "tails" "heads" "heads"
## [10] "tails" "tails" "tails" "tails" "heads" "tails" "tails" "heads" "tails"
## [19] "heads" "tails" "tails" "heads" "tails" "tails" "tails" "heads" "tails"
## [28] "heads" "tails" "tails" "tails" "tails" "heads" "tails" "tails" "heads"
## [37] "tails" "tails" "tails" "tails" "heads" "tails" "tails" "tails" "heads"
## [46] "tails" "heads" "heads" "tails" "tails" "tails" "tails" "heads" "heads"
## [55] "heads" "tails" "tails" "tails" "heads" "tails" "tails" "tails" "heads"
## [64] "heads" "tails" "tails" "tails" "tails" "heads" "heads" "tails" "tails"
## [73] "tails" "tails" "heads" "tails" "tails" "tails" "tails" "tails" "heads"
## [82] "tails" "tails" "heads" "heads" "tails" "tails" "heads" "tails" "heads"
## [91] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [100] "tails"
table(sim_unfair_coin)
## sim_unfair_coin
## heads tails
## 29 71
In a simulation of flipping an unfair coin, in 100 flips, only 29 came up heads.
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
shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 1, replace = TRUE)
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.
What change needs to be made to the 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.
The sample function needs to be changes to reflect a probability of 45% for “H”. We also need to change the size to 133 to sample 133 shots.
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE,
prob = c(.45, .55))
Using calc_streak, compute the streak lengths of sim_basket, and save the results in a data frame called sim_streak.
sim_streak <- calc_streak(sim_basket)
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.
mean(sim_streak$length)
## [1] 0.7402597
max(sim_streak$length)
## [1] 5
The simulated independent shooter’s typical streak length is between 0 and 1. The longest streak length for this player is 5 baskets in a row.
# Plot distribution of streak length
ggplot(data = sim_streak, aes(x = length)) +
geom_bar()+
labs(title = "Simulated Player Shooting Streaks")
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.
If I ran the simulation a second time, I would expect the distribution to be somewhat similar. The law of large numbers says that as the number of chances increases, the distribution tends toward the probability. At just 130 chances, I don’t expect the numbers to be exactly the same as the probability, but I don’t expect they would be totally different either.
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE,
prob = c(.45, .55))
sim_streak <- calc_streak(sim_basket)
# Plot distribution of streak length
ggplot(data = sim_streak, aes(x = length)) +
geom_bar()+
labs(title = "Simulated Player Shooting Streaks--Rerun")
The graph shows that my assumption was correct!
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.
Bryant’s distribution of streak length was slightly different from that of the simulated shooter. His longest streak was four, but in two runnings of the simulated player’s streaks, their highest number was 5. That said, the difference is not that significant. His distribution looks similar, so the hot hand model could fit Kobe’s shooting patterns.