# clear the existing environment
rm(list = ls())
# load data (local, bare filename -- no URLs)
kobe_basket <- read.csv("kobe_basket.csv")
# streak helper defined in the lab walkthrough -- do not alter
calc_streak <- function(x) {
if (!is.atomic(x)) {
x <- x[, 1]
}
if (any(!x %in% c("H", "M"))) {
stop('Input should only contain hits ("H") and misses ("M")')
}
y <- rep(0, length(x))
y[x == "H"] <- 1
y <- c(0, y, 0)
wz <- which(y == 0)
streak <- diff(wz) - 1
return(data.frame(length = streak))
}shot_outcomes <- c("H", "M")
sim_fair <- sample(shot_outcomes, size = 133, replace = TRUE)
table(sim_fair)## sim_fair
## H M
## 70 63
Written answer: replace = true is the right choice for simulating shot because if it was false the probability of hitting the same outcome each time would decrease the probability of whatever it lands on for the next which could not be the case.
sim_long <- sample(shot_outcomes, size = 5000, replace = TRUE,
prob = c(0.45, 0.55))
run_prop <- cumsum(sim_long == "H") / (1:5000)
plot(run_prop, type = "l", col = "#317EAC", lwd = 2, ylim = c(0, 1),
main = "Running proportion of hits, 45% shooter",
xlab = "Number of shots", ylab = "Proportion of hits so far")
abline(h = 0.45, col = "#D55E00", lwd = 2, lty = 2)Written answer: The line fluctuates in the first hundred shots compared to a thousand because over the first hundred there will be some cases where the 45% shooter makes it a couple times that increases it above the 45% line and vice-versa. After a thousands of attempts it becomes more consistent and closer the projected 45% shown on the graph. This is an example of law of large numbers.
## [1] 0.4360902
Written answer: in the long run Kobe hits the basket 43.6% of the time
kobe_streak <- calc_streak(kobe_basket$shot)$length
barplot(table(kobe_streak), col = "#317EAC",
main = "Kobe Bryant's streak lengths, 2009 NBA Finals",
xlab = "Streak length", ylab = "Number of streaks")## [1] 0
## [1] 4
Written answer: Median: 0 | Longest Streak: 4. The distribution is skewed to the right and a streak length of 0 is that when Kobe makes a shot the next shot he shoots is a miss meaning that he doesn’t have a streak.
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE,
prob = c(p_hit, 1 - p_hit))
sim_streak <- calc_streak(sim_basket)$length
barplot(table(sim_streak), col = "#D55E00",
main = "Simulated independent shooter's streak lengths",
xlab = "Streak length", ylab = "Number of streaks")## [1] 0
## [1] 5
Written answer: median: 0 | longest streak: 5
## kobe_streak
## 0 1 2 3 4
## 39 24 6 6 1
## sim_streak
## 0 1 2 3 5
## 42 26 6 4 1
Written answer: Kobe had a longer typical streak while the simulated player had the longest streak(5). There is no clear evidence that the hot hand model fits kobe’s shooting in the 2009 Finals. The siulated shooter had alonger single streak than Kobe. This Suggests that streaks can occur naturally due to random chance, even without hot hand.
n <- nrow(kobe_basket)
kobe_prev <- kobe_basket$shot[1:(n - 1)]
kobe_next <- kobe_basket$shot[2:n]
kobe_tbl <- table(kobe_prev, kobe_next)
prop.table(kobe_tbl, 1)## kobe_next
## kobe_prev H M
## H 0.3684211 0.6315789
## M 0.4800000 0.5200000
## [1] 0.4318182
##
## H M
## H 0.3272727 0.6727273
## M 0.4805195 0.5194805
## [1] 0.4166667
Written answer: This lens shows that both shooter a less likely to make a shot after the prevous one. This agrees with Exercise 6 as there is no clear evidence of a hot hand in jobe’s shooting from this comparison alone, since his results do not how an increased probability of making the next shot after a hit.
## [1] 78
## [1] 0.5641026
Written answer: Number of Shot: 78 | Curry’s proportion of hits: 56.41%. Curry has a higher proportion than Kobe during the 2017 finale run (0.5641 > 0.4360)
curry_streak <- calc_streak(curry_basket$shot)$length
barplot(table(curry_streak), col ="gold",
main = "Stephen Wardell Curry's streak lengths, 2017 Finals",
xlab = "Streak length", ylab = "Number of Streaks")## [1] 1
## [1] 5
Written answer: Median: 1 | Longest Streak: 5 | The Distribution is a rightward skewed bar graph
sim_curry <- sample(c("H", "M"),
size = nrow(curry_basket), replace = TRUE,
prob = c(p_curry, 1 - p_curry))
sim_curry_streak <- calc_streak(sim_curry)$length
barplot(table(sim_curry_streak),
main = "Simulated Curry Streak Distribution",
xlab = "Streak Length", ylab = "Frequency", col = "gold")## [1] 1
## [1] 5
Written answer: Median Streak: 1 | Longest Streak: 5. Both had the same typical streak length, but the simulated shooter had a longer maximum streak. Therefore, there is no evidence from this streak comparison that the hot hand model fits Curry, since his streaks were not longer than those produced by an independent shooter.
nC <- nrow(curry_basket)
curry_prev <- curry_basket$shot[1:(nC - 1)]
curry_next <- curry_basket$shot[2:nC]
curry_tbl <- table(curry_prev, curry_next)
prop.table(curry_tbl, 1)## curry_next
## curry_prev H M
## H 0.5348837 0.4651163
## M 0.5882353 0.4117647
## [1] 0.5584416
##
## H M
## H 0.4736842 0.5263158
## M 0.5384615 0.4615385
## [1] 0.5064935
Written answer: Curry’s probability of making a shot after a previous hit was 53.49%, compared to his overall hit proportion of 55.84%. The simulated shooter’s conditional hit probability was 47.37%, compared to an overall hit proportion of 50.65%. Curry’s gap was slightly smaller in magnitude than the simulated shooter’s gap, so this second lens does not provide evidence of a hot hand for Curry.
Written answer: Curry’s longest streak was 5, and the simulated shooter’s longest streak was also 5. Both had a median streak of 1. Curry’s probability of making a shot after a previous hit was 53.49%, compared to his overall hit proportion of 55.84%. The simulated shooter’s conditional hit probability was 47.37%, while its overall hit proportion was 50.65%. These results do not provide strong evidence of a hot hand for Curry. An independent shooter with a higher shooting percentage can have longer streaks because making more shots increases the chance of consecutive hits. Comparing Curry to a simulation is a plausibility check rather than a formal test because random chance can produce long streaks even when the shooting probability stays constant.
| Team member | Attendance | Author | Contribution % |
|---|---|---|---|
| Matthew Nguyen | Yes | Yes | 50% |
| Tuan Nguyen | Yes | No | 50% |
| Total | 100% |