library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ ggplot2 3.3.5 ✓ purrr 0.3.4
## ✓ tibble 3.1.4 ✓ dplyr 1.0.7
## ✓ tidyr 1.1.3 ✓ stringr 1.4.0
## ✓ readr 2.0.1 ✓ forcats 0.5.1
## ── 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
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)
## 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"…
-A streak of 1 means that he makes one shot and misses the next. -A streak length of 0 would mean that there are no shots made and 1 missed.
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.
kobe_streak <- calc_streak(kobe_basket$shot)
Look at distribution of streak lengths:
ggplot(data = kobe_streak, aes(x = length)) +
geom_bar()
-The distribution for Kobe’s streaks is skewed to the right -His typical streak length was 0 -His longest streak length was 4. Plot is above
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.
coin_outcomes <- c("heads", "tails")
sample(coin_outcomes, size = 1, replace = TRUE)
## [1] "tails"
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.
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] "tails" "heads" "tails" "heads" "heads" "heads" "tails" "tails" "heads"
## [10] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "heads"
## [19] "heads" "tails" "heads" "tails" "tails" "tails" "tails" "tails" "heads"
## [28] "heads" "tails" "tails" "heads" "heads" "heads" "heads" "heads" "heads"
## [37] "heads" "heads" "tails" "tails" "heads" "heads" "heads" "heads" "tails"
## [46] "tails" "tails" "heads" "tails" "tails" "heads" "heads" "heads" "heads"
## [55] "heads" "tails" "tails" "tails" "heads" "tails" "heads" "tails" "heads"
## [64] "tails" "tails" "heads" "heads" "tails" "heads" "heads" "tails" "heads"
## [73] "heads" "heads" "tails" "heads" "tails" "tails" "tails" "tails" "tails"
## [82] "heads" "tails" "heads" "heads" "heads" "tails" "tails" "heads" "heads"
## [91] "heads" "tails" "heads" "heads" "tails" "tails" "heads" "heads" "tails"
## [100] "tails"
table(sim_fair_coin)
## sim_fair_coin
## heads tails
## 50 50
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.
set.seed(02251)
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE,
prob = c(0.2, 0.8))
table(sim_unfair_coin)
## sim_unfair_coin
## heads tails
## 28 72
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%.
-28 heads in my simulation. Code for simulation is above.
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.
In order to reflect a shooring percentage of 45%, we would need to add the probability that H is .45 and M is .55.
set.seed(02252)
outcomes <- c("H", "M")
sim_basket <- sample(outcomes, size = 133, replace = TRUE,
prob = c(.45, .55))
table(sim_basket)
## sim_basket
## H M
## 46 87
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.
set.seed(02253)
sim_streak <- calc_streak(sim_basket)
sim_streak
## length
## 1 0
## 2 0
## 3 1
## 4 0
## 5 0
## 6 0
## 7 0
## 8 1
## 9 2
## 10 0
## 11 0
## 12 0
## 13 2
## 14 2
## 15 0
## 16 0
## 17 0
## 18 0
## 19 0
## 20 1
## 21 0
## 22 0
## 23 0
## 24 0
## 25 0
## 26 1
## 27 0
## 28 0
## 29 0
## 30 1
## 31 0
## 32 2
## 33 0
## 34 0
## 35 0
## 36 0
## 37 3
## 38 0
## 39 0
## 40 1
## 41 0
## 42 1
## 43 0
## 44 0
## 45 0
## 46 0
## 47 0
## 48 0
## 49 0
## 50 0
## 51 0
## 52 1
## 53 1
## 54 0
## 55 0
## 56 0
## 57 0
## 58 0
## 59 0
## 60 0
## 61 8
## 62 1
## 63 0
## 64 0
## 65 2
## 66 2
## 67 0
## 68 0
## 69 0
## 70 0
## 71 2
## 72 0
## 73 0
## 74 4
## 75 0
## 76 0
## 77 1
## 78 0
## 79 0
## 80 2
## 81 0
## 82 0
## 83 2
## 84 0
## 85 0
## 86 0
## 87 0
## 88 2
ggplot(data = sim_streak, aes(x = length)) +
geom_bar()
-The distribution of streak lengths is skewed to the right. -The typical streak for the independent shooter with a 45% shooting percentage is 0. -The longest streak in 133 shots was 8
The distribution would be similar because the shots are still independent of one another. The probability of making the shot wouldn’t change no matter how many times you run the simulation.
Both distribution streaks are similar for the simulated independent shooter, and for Kobe. Both distributions have 0 as the typical streak and they are both skewed to the right. The similarities in the streaks mean that there is not enough evidence to say that Kobe’s streaks can be explained by the hot hand theory.