Lab report

Load data:

# 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))
}

Set the seed:

# DO NOT ALTER CODE IN THIS CHUNK
set.seed(010)

Exercises:

Exercise 1: Create a vector shot_outcomes holding “H” and “M”, then use sample() to simulate 133 shots from a shooter who hits 50% of the time, saving the result as sim_fair. Count the hits and misses with table(). Report how many hits you got, and explain why replace = TRUE is the right choice for simulating shots.

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.

Exercise 2: Simulate 5,000 shots from a 45% shooter and save them as sim_long. Build the running proportion of hits, then plot it as a line with a dashed horizontal reference line at 0.45 using abline(h = 0.45). Describe how the line behaves over the first few hundred shots compared with the last few thousand, and say which idea from the recap this illustrates.

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.

Exercise 3: Compute Kobe’s proportion of hits in this series and save it as p_hit. Report the value, and state what it means as a long-run proportion.

p_hit <- mean(kobe_basket$shot == "H")
p_hit
## [1] 0.4360902

Written answer: in the long run Kobe hits the basket 43.6% of the time

Exercise 4: Compute Kobe’s streak lengths with calc_streak() and save them as kobe_streak. Make a titled, labelled bar plot of table(kobe_streak), and report the median and the longest streak. Describe the shape of the distribution, and say in your own words what a streak of length 0 tells you about a shot.

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")

median(kobe_streak)
## [1] 0
max(kobe_streak)
## [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.

Exercise 5: Simulate 133 shots from an independent shooter whose hit probability is p_hit, and save the result as sim_basket. Compute its streak lengths as sim_streak and make a titled, labelled bar plot of them. Report this simulated shooter’s median and longest 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")

median(sim_streak)
## [1] 0
max(sim_streak)
## [1] 5

Written answer: median: 0 | longest streak: 5

Exercise 6:Compare Kobe’s streak distribution from Exercise 4 with the simulated shooter’s from Exercise 5. Which has the longer typical streak, and which has the longer single longest streak? Based on this comparison alone, state whether you see evidence that the hot hand model fits Kobe’s shooting in the 2009 Finals, and explain your reasoning.

table(kobe_streak)
## kobe_streak
##  0  1  2  3  4 
## 39 24  6  6  1
table(sim_streak)
## 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.

Exercise 7: Build kobe_prev and kobe_next from kobe_basket$shot, form the two-way table kobe_tbl, and print its row proportions. Do the same for sim_basket. Report P(hit∣previous hit )for Kobe and compare it with his overall hit proportion. Then compare the size of Kobe’s gap with the simulated shooter’s gap, and say whether this lens agrees with your answer to Exercise 6.

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
mean(kobe_next == "H")
## [1] 0.4318182
sim_tbl <- table(sim_basket[1:(n - 1)], sim_basket[2:n])
prop.table(sim_tbl, 1)
##    
##             H         M
##   H 0.3272727 0.6727273
##   M 0.4805195 0.5194805
mean(sim_basket[2:n] == "H")
## [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.


On your own:

Load the Curry data:

curry_basket <- read.csv("curry_basket.csv")

1: [6 pts] Report how many shots the data set contains and compute Curry’s proportion of hits, saving it as p_curry. Report that proportion, compare it with Kobe’s p_hit from Exercise 3, and say which of the two was the more accurate shooter in these series.

nrow(curry_basket)
## [1] 78
p_curry <- mean(curry_basket$shot == "H")
p_curry
## [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)

2: [6 pts] Compute Curry’s streak lengths with calc_streak(), saving them as curry_streak, and make a bar plot of them. Report his median streak length and his longest streak, and describe the shape of the distribution.

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")

median(curry_streak)
## [1] 1
max(curry_streak)
## [1] 5

Written answer: Median: 1 | Longest Streak: 5 | The Distribution is a rightward skewed bar graph

3: [10 pts] Simulate an independent shooter matched to Curry – the same number of shots and the same hit probability – saving the shots as sim_curry and the streaks as sim_curry_streak. Make a bar plot of the simulated streaks and report the simulated median and longest streak. Compare the two distributions, and state whether this streak comparison gives you evidence that the hot hand model fits Curry.

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")

median(sim_curry_streak)
## [1] 1
max(sim_curry_streak)
## [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.

4: [10 pts] Build the previous-shot and next-shot columns for Curry, form the two-way table, and print its row proportions. Report P(hit∣previous hit) and Curry’s overall hit proportion, then do the same two things for sim_curry. Compare the size of Curry’s gap between those two numbers with the simulated shooter’s gap, and state whether this second lens gives you evidence of a hot hand.

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
mean(curry_next == "H")
## [1] 0.5584416
sim_tbl2 <- table(sim_curry[1:(nC - 1)], sim_curry[2:nC]) 
prop.table(sim_tbl2, 1)
##    
##             H         M
##   H 0.4736842 0.5263158
##   M 0.5384615 0.4615385
mean(sim_curry[2:nC] == "H")
## [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.

5: [8 pts] Pulling both lenses together, state your overall conclusion about whether the hot hand model fits Curry in this series, citing specific numbers from the two questions above. Then explain why an independent shooter with a higher shooting percentage produces longer streaks than a less accurate one, and why comparing Curry with a simulation is a plausibility check rather than a formal test.

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.


Teamwork report

Team member Attendance Author Contribution %
Matthew Nguyen Yes Yes 50%
Tuan Nguyen Yes No 50%
Total 100%