library(tidyverse) library(openintro)
data(kobe_basket) glimpse(kobe_basket)
calc_streak <- function(shots) { streaks <- rle(shots) streaks_df <- data.frame(length = streaks\(lengths[streaks\)values == “H”]) return(streaks_df) }
kobe_streak <- calc_streak(kobe_basket$shot)
ggplot(data = kobe_streak, aes(x = length)) + geom_bar() + labs(title = “Distribution of Kobe’s Shooting Streak Lengths”, x = “Streak Length”, y = “Count”)
summary(kobe_streak$length)
set.seed(35797) # Ensure reproducibility
shot_outcomes <- c(“H”, “M”) sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))
sim_streak <- calc_streak(sim_basket)
ggplot(data = sim_streak, aes(x = length)) + geom_bar() + labs(title = “Distribution of Simulated Shooter’s Streak Lengths”, x = “Streak Length”, y = “Count”)
summary(sim_streak$length)