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)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.2 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.2 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
You can also embed plots, for example:
glimpse(kobe_basket)
## Rows: 133
## Columns: 6
## $ vs <fct> ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL…
## $ game <int> 1, 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, 3…
## $ time <fct> 9:47, 9:07, 8:11, 7:41, 7:03, 6:01, 4:07, 0:52, 0:00, 6:35…
## $ description <fct> Kobe Bryant makes 4-foot two point shot, Kobe Bryant misse…
## $ shot <chr> "H", "M", "M", "H", "H", "M", "M", "M", "M", "H", "H", "H"…
head(kobe_basket,10)
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?
A streak of one is when means he made a shot and then missed 1 after. A streak of zero means he didn’t make any shots in the series of shots he attempted.
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
kobe_streak <- calc_streak(kobe_basket$shot)
ggplot(data = kobe_streak, aes(x = length)) +
geom_bar()
## Exercise 2
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.
A large portion of Kobe’s streak was 0 to 1 and his longest streak was 4 baskets in a row.
coin_outcomes <- c("heads", "tails")
sample(coin_outcomes, size = 1, replace = TRUE)
## [1] "heads"
sim_fair_coin <- sample(coin_outcomes, size = 100, replace = TRUE)
sim_fair_coin
## [1] "tails" "tails" "heads" "tails" "tails" "heads" "tails" "tails" "heads"
## [10] "tails" "tails" "heads" "tails" "heads" "tails" "tails" "heads" "tails"
## [19] "heads" "heads" "tails" "heads" "heads" "heads" "tails" "heads" "heads"
## [28] "tails" "heads" "tails" "heads" "heads" "heads" "tails" "tails" "tails"
## [37] "heads" "heads" "heads" "heads" "tails" "heads" "tails" "tails" "tails"
## [46] "heads" "tails" "tails" "tails" "tails" "heads" "heads" "heads" "tails"
## [55] "heads" "tails" "heads" "heads" "tails" "tails" "heads" "tails" "heads"
## [64] "tails" "tails" "tails" "tails" "heads" "heads" "tails" "tails" "heads"
## [73] "heads" "heads" "tails" "heads" "tails" "tails" "heads" "heads" "tails"
## [82] "heads" "heads" "tails" "tails" "heads" "heads" "heads" "heads" "tails"
## [91] "heads" "tails" "heads" "tails" "tails" "tails" "tails" "tails" "tails"
## [100] "tails"
table(sim_fair_coin)
## sim_fair_coin
## heads tails
## 47 53
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. Read more about setting a seed below.
set.seed(0821997)
coin_experiment <- c("H","T")
sample(coin_experiment, size = 1, replace = TRUE)
## [1] "T"
unfair_coin <- sample(coin_experiment, size = 100, replace = TRUE,
prob = c(0.2, 0.8))
table(unfair_coin)
## unfair_coin
## H T
## 22 78
shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 1, replace = TRUE)
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.
shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))
sim_basket
## [1] "M" "H" "M" "H" "M" "M" "M" "M" "M" "M" "H" "M" "M" "H" "H" "H" "H" "M"
## [19] "H" "H" "H" "M" "H" "M" "H" "M" "M" "H" "M" "M" "M" "M" "H" "M" "M" "H"
## [37] "M" "H" "H" "M" "H" "M" "H" "H" "M" "M" "H" "H" "H" "M" "H" "M" "H" "H"
## [55] "H" "H" "M" "M" "H" "H" "M" "H" "M" "M" "M" "H" "M" "M" "M" "M" "M" "H"
## [73] "H" "H" "M" "M" "H" "M" "H" "M" "M" "M" "M" "H" "M" "M" "H" "M" "H" "H"
## [91] "H" "M" "M" "M" "M" "H" "H" "H" "M" "M" "H" "M" "M" "H" "M" "H" "H" "M"
## [109] "H" "M" "M" "H" "M" "M" "H" "H" "M" "H" "M" "M" "M" "H" "M" "H" "H" "H"
## [127] "M" "H" "M" "H" "M" "M" "M"
table(sim_basket)
## sim_basket
## H M
## 60 73
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.
ggplot(data = sim_streak, aes(x = length)) +
geom_bar()
In Kobe’s shooting pattern, we observe a right-skewed distribution of streak lengths. This suggests that he had a higher frequency of missing his shots, with streaks of misses being the most common, usually appearing around 47 to 49 times. On the other hand, his longest streak of successful shots was 6, but this happened relatively rarely, occurring only once.
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.
I anticipate that the distribution will closely resemble the observed pattern, although there exists a small likelihood of observing significantly different values. This is due to the fact that, even though the probability of making a shot is 45%, there remains the remote possibility, though improbable, of Kobe making an extensive series of consecutive shots at a rapid rate.
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.
Kobe’s streak length distribution closely resembles that of simulated shooters in terms of right-skewness and the number of misses. However, it’s crucial to highlight that Kobe had more frequent streaks in nearly every category, except for a 2-point streak. On the other hand, the simulated shooter had a greater number of streaks overall compared to Kobe. This observation may lean towards supporting the argument that the hot hand model doesn’t quite apply to Kobe’s shooting patterns.