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.
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.
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
.
## 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"…
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).
I wanted to view the table and get the first 9 rows of data to interpret the streaks. I realized that I had to convert it into a data frame first before I could analyze it. The five columns represent the streaks, and the rows indicate how many hits occurred before a miss. For example, streak 1 has 24 hits, while streak 0 has 39.
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)
# converted the vector into a dateframe
kobe_streak_df <- as.data.frame(table(kobe_streak))
colnames(kobe_streak_df) <- c("length", "count")
str(kobe_streak_df)
## 'data.frame': 5 obs. of 2 variables:
## $ length: Factor w/ 5 levels "0","1","2","3",..: 1 2 3 4 5
## $ count : int 39 24 6 6 1
## count
## length 1 6 24 39
## 0 0 0 0 1
## 1 0 0 1 0
## 2 0 1 0 0
## 3 0 1 0 0
## 4 1 0 0 0
## length count
## 1 0 39
## 2 1 24
## 3 2 6
## 4 3 6
## 5 4 1
We can then take a look at the distribution of these streak lengths.
The distribution of Kobe Bryant’s shooting streaks in the 2009 NBA Finals shows that his most common streak length was 1, followed by 2 to visualize this,I plotted length by frequency, the distribution also showed that streak higher than 5(plot length was converted to a numeric value, switching the original streak from 0 to 1)
library(ggplot2)
library(ggplot2)
ggplot(data = kobe_streak_df, aes(x = as.numeric(length), y = count)) +
geom_bar(stat = "identity", fill = "purple", alpha = 0.7) +
labs(title = "Distribution of Kobe's Streak Lengths (2009 NBA Finals)",
x = "Streak Length (Consecutive Hits)",
y = "Frequency") +
theme_minimal()
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 probabilites, 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.
## [1] "heads"
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
.
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.
## [1] "tails" "heads" "tails" "heads" "tails" "heads" "heads" "tails" "tails"
## [10] "heads" "tails" "tails" "heads" "heads" "heads" "tails" "tails" "tails"
## [19] "tails" "tails" "tails" "heads" "tails" "heads" "heads" "heads" "heads"
## [28] "tails" "heads" "tails" "heads" "tails" "heads" "tails" "tails" "heads"
## [37] "heads" "heads" "tails" "heads" "heads" "heads" "heads" "tails" "tails"
## [46] "tails" "tails" "tails" "heads" "heads" "tails" "heads" "tails" "tails"
## [55] "heads" "tails" "heads" "heads" "tails" "heads" "tails" "tails" "heads"
## [64] "tails" "tails" "heads" "heads" "tails" "heads" "tails" "heads" "heads"
## [73] "heads" "tails" "tails" "heads" "tails" "heads" "tails" "tails" "tails"
## [82] "heads" "heads" "tails" "heads" "heads" "heads" "heads" "tails" "tails"
## [91] "heads" "heads" "heads" "tails" "tails" "heads" "heads" "heads" "tails"
## [100] "tails"
## sim_fair_coin
## heads tails
## 51 49
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.
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%.
Ok, so I ran two simulations. The second was the unfair simulation, where I set the seed to my birthday and changed the probability to 55% heads and 45% tails. I suspected that the unfair simulation would produce more heads than the first simulation. I also found that the higher the fixed probability, the closer the results were to the expected value.
# Set seed for reproducibility
set.seed(052086)
# Define the possible outcomes
outcomes <- c("Heads", "Tails")
# First simulation: 30% chance of heads
flips <- sample(outcomes, size = 100, replace = TRUE, prob = c(0.3, 0.7))
num_heads <- sum(flips == "Heads")
# Second simulation (Unfair): 55% chance of heads
sim_unfair_coin <- sample(outcomes, size = 100, replace = TRUE, prob = c(0.55, 0.45))
num_heads_unfair <- sum(sim_unfair_coin == "Heads")
# Print the results
num_heads
## [1] 36
## [1] 49
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:
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.
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
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
.The simulation was adjusted to 133 shots with a 45% hit and 55% miss probability to match the actual attempts. The results show that streaks of 1-2 hits were the most common, while longer streaks (4-5 hits) were rare. This pattern aligns with an independent shooter, where short streaks occur frequently, and long streaks are uncommon due to randomness.
# Set seed
set.seed(052086)
# Define shot outcomes
shot_outcomes <- c("H", "M")
# Simulate 133 shots with 45% chance of hitting ("H") and 55% chance of missing ("M")
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))
# View first few shots to confirm
head(sim_basket)
## [1] "H" "H" "M" "M" "M" "M"
# Count the number of "H" (Hits)
num_hits <- sum(sim_basket == "H")
# Count the number of "M" (Misses)
num_misses <- sum(sim_basket == "M")
# Print results
num_hits
## [1] 65
## [1] 68
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.
calc_streak
, compute the streak lengths of
sim_basket
, and save the results in a data frame called
sim_streak
.The data was then formatted into a table, with columns renamed for clarity, making it easier to analyze the frequency of different streak lengths
## length
## 0 1 2 3 4 5
## 35 17 8 6 1 2
# Compute streak lengths using calc_streak and save as a data frame
sim_streak <- as.data.frame(table(calc_streak(sim_basket)))
# Rename columns
colnames(sim_streak) <- c("Streak Length", "Frequency")
# Print
head(sim_streak)
## Streak Length Frequency
## 1 0 35
## 2 1 17
## 3 2 8
## 4 3 6
## 5 4 1
## 6 5 2
The typical streak length was 1, while the longest streak was 4 consecutive makes. Short streaks (1-2 hits) were the most common, while longer streaks were rare. The bar chart confirmed this pattern, showing a decreasing frequency of longer streaks, consistent with a random shooting process.
# Load required library
library(ggplot2)
# Set seed for reproducibility
set.seed(52086)
# Simulate 133 shots with 45% chance of making a shot
second_sim_basket <- sample(c("H", "M"), size = 133, replace = TRUE, prob = c(0.45, 0.55))
# Compute streak lengths
second_sim_streaks <- calc_streak(second_sim_basket)
# Ensure streak data is numeric and remove NAs
second_sim_streaks <- as.numeric(second_sim_streaks[!is.na(second_sim_streaks)])
# Convert streak data into a data frame
second_streak_df <- as.data.frame(table(second_sim_streaks))
colnames(second_streak_df) <- c("Streak Length", "Frequency")
# Convert Streak Length column to numeric
second_streak_df$`Streak Length` <- as.numeric(as.character(second_streak_df$`Streak Length`))
# Plot streak distribution
ggplot(second_streak_df, aes(x = `Streak Length`, y = Frequency)) +
geom_bar(stat = "identity", fill = "gold", alpha = 0.8) +
labs(title = "Simulated Shooter's Streak Lengths (45% Shooting)",
x = "Streak Length (Consecutive Hits)",
y = "Frequency") +
theme_minimal()
# Print findings with numeric safety check
cat("Typical streak length:", median(second_sim_streaks, na.rm = TRUE), "\n")
## Typical streak length: 0
## Longest streak: 5
After running the simulation again, the streak distribution was somewhat similar but not exactly the same. Since the shooter’s shots were independent and randomly generated, the overall pattern of short streaks with a few longer ones—remained consistent. However, individual streak lengths varied slightly due to randomness.
# Load necessary packages
library(ggplot2)
library(dplyr)
# Set seed for reproducibility
set.seed(52086)
# Function to simulate shot streaks
simulate_shooter <- function(num_shots, hit_prob) {
shots <- sample(c("H", "M"), size = num_shots, replace = TRUE, prob = c(hit_prob, 1 - hit_prob))
streaks <- c()
current_streak <- 0
for (shot in shots) {
if (shot == "H") {
current_streak <- current_streak + 1
} else {
if (current_streak > 0) {
streaks <- c(streaks, current_streak) # Save streak length
}
current_streak <- 0 # Reset streak
}
}
if (current_streak > 0) {
streaks <- c(streaks, current_streak)
}
return(streaks)
}
# Define simulation parameters
num_shots <- 133
hit_prob <- 0.45 # 45% chance of making a shot
# First simulation
first_sim_streaks <- simulate_shooter(num_shots, hit_prob)
first_streak_df <- as.data.frame(table(first_sim_streaks), stringsAsFactors = FALSE)
colnames(first_streak_df) <- c("Streak Length", "Frequency")
first_streak_df$Simulation <- "First Simulation"
# Second simulation
second_sim_streaks <- simulate_shooter(num_shots, hit_prob)
second_streak_df <- as.data.frame(table(second_sim_streaks), stringsAsFactors = FALSE)
colnames(second_streak_df) <- c("Streak Length", "Frequency")
second_streak_df$Simulation <- "Second Simulation"
# Combine both simulations for comparison
combined_streak_df <- bind_rows(first_streak_df, second_streak_df)
# Ensure `Streak Length` is numeric
combined_streak_df$`Streak Length` <- as.numeric(as.character(combined_streak_df$`Streak Length`))
# Plot comparison
ggplot(combined_streak_df, aes(x = `Streak Length`, y = Frequency, fill = Simulation)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.7), alpha = 0.8) +
scale_fill_manual(values = c("First Simulation" = "gold", "Second Simulation" = "purple")) +
labs(title = "Comparison of Two Simulated Shooter's Streak Lengths (45% Shooting)",
x = "Streak Length (Consecutive Hits)",
y = "Frequency",
fill = "Simulation") +
theme_minimal()
Looking at the plot, Kobe’s streaks were longer and more frequent compared to the simulated shooter. The simulated shooter, who had the same 45% shooting percentage, had fewer long streaks and mostly short ones (1-2 hits in a row). Since Kobe had more long streaks than expected, this suggests he might have had a hot hand, meaning his shots weren’t completely independent—when he got on a roll, he stayed hot longer than the random model predicted.
# Load necessary libraries
library(ggplot2)
library(dplyr)
library(tidyr)
# Ensure Kobe's and simulated streaks exist
if (!exists("kobe_streak")) kobe_streak <- calc_streak(kobe_basket$shot)
if (!exists("sim_streak")) {
sim_basket <- sample(c("H", "M"), size = 133, replace = TRUE, prob = c(0.45, 0.55))
sim_streak <- calc_streak(sim_basket)
}
# Create clean data frames
kobe_streak_df <- as.data.frame(table(kobe_streak))
sim_streak_df <- as.data.frame(table(sim_streak))
# Rename columns for consistency
colnames(kobe_streak_df) <- c("Streak_Length", "Frequency_Kobe")
colnames(sim_streak_df) <- c("Streak_Length", "Frequency_Simulated")
# Remove any NA columns in sim_streak_df
sim_streak_df <- sim_streak_df[, !is.na(colnames(sim_streak_df))]
# Convert all values to numeric
kobe_streak_df <- kobe_streak_df %>% mutate(Streak_Length = as.numeric(as.character(Streak_Length)),
Frequency_Kobe = as.numeric(Frequency_Kobe))
sim_streak_df <- sim_streak_df %>% mutate(Streak_Length = as.numeric(as.character(Streak_Length)),
Frequency_Simulated = as.numeric(Frequency_Simulated))
# Merge properly without duplicate or missing values
full_streak_df <- full_join(kobe_streak_df, sim_streak_df, by = "Streak_Length") %>%
replace(is.na(.), 0) # Replace NA with 0s
# Convert to long format for ggplot
streak_frequency_long <- full_streak_df %>%
pivot_longer(cols = c(Frequency_Kobe, Frequency_Simulated),
names_to = "Shooter", values_to = "Frequency")
# Final Plot
ggplot(streak_frequency_long, aes(x = Streak_Length, y = Frequency, fill = Shooter)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.7)) +
scale_fill_manual(values = c("Frequency_Kobe" = "#552583", "Frequency_Simulated" = "#FDB927"),
labels = c("Kobe Bryant", "Simulated Shooter")) +
labs(title = "Comparison of Kobe Bryant's Streaks vs. Simulated Shooter",
x = "Streak Length",
y = "Frequency",
fill = "Legend") +
theme_minimal()