Hot hand

Author

Yeabsira Alemu

library (tidyverse)
Warning: package 'ggplot2' was built under R version 4.3.3
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── 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)
Warning: package 'openintro' was built under R version 4.3.3
Loading required package: airports
Warning: package 'airports' was built under R version 4.3.3
Loading required package: cherryblossom
Warning: package 'cherryblossom' was built under R version 4.3.3
Loading required package: usdata
Warning: package 'usdata' was built under R version 4.3.3
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"…
##Exercise 1 
kobe_streak<- calc_streak(kobe_basket$shot)
##Streak length 1 means one hit followed by a miss 
##Streak lenght 0 means there is a miss with no hits before it 
ggplot(data = data.frame(length = kobe_streak), aes(x = length)) +
  geom_bar(fill = "purple", color = "orange") +
  ggtitle("Kobe Bryant's Shooting Streak Lengths") +
  theme_minimal()

#Exercise 2
summary(kobe_streak)
     length      
 Min.   :0.0000  
 1st Qu.:0.0000  
 Median :0.0000  
 Mean   :0.7632  
 3rd Qu.:1.0000  
 Max.   :4.0000  
max(kobe_streak)
[1] 4
#Exercise 3 
shot_outcomes <- c("H", "M")

# 45% shooting 
set.seed(7243)
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))

sim_streak <- calc_streak(sim_basket)

# Comparison between sim and kobe
kobe_summary <- data.frame(Player = "Kobe Bryant", Mean_Streak = mean(kobe_streak), Max_Streak = max(kobe_streak))
Warning in mean.default(kobe_streak): argument is not numeric or logical:
returning NA
sim_summary <- data.frame(Player = "Simulated Shooter", Mean_Streak = mean(sim_streak), Max_Streak = max(sim_streak))
Warning in mean.default(sim_streak): argument is not numeric or logical:
returning NA
comparison <- rbind(kobe_summary, sim_summary)
print(comparison)
             Player Mean_Streak Max_Streak
1       Kobe Bryant          NA          4
2 Simulated Shooter          NA          5
#Exercise 4
# Additional Analysis
set.seed(7234)
sim_streaks <- replicate(1000, {
  sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))
  calc_streak(sim_basket)
})

sim_streak_lengths <- unlist(sim_streaks)
mean_streak_lengths <- mean(sim_streak_lengths)
max_streak_lengths <- max(sim_streak_lengths)

cat("Mean Streak Lengths across 1000 simulations:", mean_streak_lengths, "\n")
Mean Streak Lengths across 1000 simulations: 0.8047624 
cat("Max Streak Lengths across 1000 simulations:", max_streak_lengths, "\n")
Max Streak Lengths across 1000 simulations: 15 
#Excercise 5 
set.seed(7243)
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))

# Calculate streak lengths for the simulated shooter
sim_streak <- calc_streak(sim_basket)

# Plot the distribution of the simulated shooter's streak lengths with custom colors
ggplot(data = data.frame(length = sim_streak), aes(x = length)) +
  geom_bar(fill = "darkorange", color = "black") +
  ggtitle("Simulated Independent Shooter's Streak Lengths (45% Shooting Percentage)") +
  theme_minimal()

# Describe the distribution of the simulated shooter's streak lengths
summary_sim <- summary(sim_streak)
max_sim <- max(sim_streak)

# Output the summary statistics for the simulated shooter's streaks
cat("Simulated Shooter's Streak Lengths Summary:\n")
Simulated Shooter's Streak Lengths Summary:
print(summary_sim)
     length      
 Min.   :0.0000  
 1st Qu.:0.0000  
 Median :0.0000  
 Mean   :0.6341  
 3rd Qu.:1.0000  
 Max.   :5.0000  
cat("Longest Streak:", max_sim, "\n")
Longest Streak: 5 
#Exercise 6 
#sim2 
set.seed(12345)  # Use a different seed
sim_basket_2 <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))

sim_streak_2 <- calc_streak(sim_basket_2)

# Plot the distribution of the new simulation's streak lengths
ggplot(data = data.frame(length = sim_streak_2), aes(x = length)) +
  geom_bar(fill = "purple", color = "black") +
  ggtitle("New Simulated Independent Shooter's Streak Lengths (45% Shooting Percentage)") +
  theme_minimal()

# Describe the distribution of the new simulation's streak lengths
summary_sim_2 <- summary(sim_streak_2)
max_sim_2 <- max(sim_streak_2)

# Output the summary statistics for the new simulation's streaks
cat("New Simulated Shooter's Streak Lengths Summary:\n")
New Simulated Shooter's Streak Lengths Summary:
print(summary_sim_2)
     length     
 Min.   :0.000  
 1st Qu.:0.000  
 Median :0.000  
 Mean   :0.942  
 3rd Qu.:1.000  
 Max.   :4.000  
cat("Longest Streak:", max_sim_2, "\n")
Longest Streak: 4 
#The streak distribution would be somewhat similar but not exactly the same. This is because the simulation involves random sampling, which introduces variability. However, the overall pattern and typical streak lengths should be.
#Exercise 7 
sim1_mean<- mean(sim_streak, na.rm = TRUE)
Warning in mean.default(sim_streak, na.rm = TRUE): argument is not numeric or
logical: returning NA
# Compare the two simulated shooter's streak lengths
sim_summary_1 <- data.frame(Player = "First Simulated Shooter", Mean_Streak = mean(sim_streak, na.rm = TRUE), Max_Streak = max(sim_streak, na.rm = TRUE))
Warning in mean.default(sim_streak, na.rm = TRUE): argument is not numeric or
logical: returning NA
sim_summary_2 <- data.frame(Player = "Second Simulated Shooter", Mean_Streak = mean(sim_streak_2, na.rm = TRUE), Max_Streak = max(sim_streak_2, na.rm = TRUE))
Warning in mean.default(sim_streak_2, na.rm = TRUE): argument is not numeric or
logical: returning NA
comparison_sim <- rbind(sim_summary_1, sim_summary_2)
print(comparison_sim)
                    Player Mean_Streak Max_Streak
1  First Simulated Shooter          NA          5
2 Second Simulated Shooter          NA          4