Hot Hands

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.

load("C:/Users/joshua.bentley/Dropbox/cuny_msds/rwd/Lab2/more/kobe.RData")
head(kobe)
##    vs game quarter time
## 1 ORL    1       1 9:47
## 2 ORL    1       1 9:07
## 3 ORL    1       1 8:11
## 4 ORL    1       1 7:41
## 5 ORL    1       1 7:03
## 6 ORL    1       1 6:01
##                                               description basket
## 1                 Kobe Bryant makes 4-foot two point shot      H
## 2                               Kobe Bryant misses jumper      M
## 3                        Kobe Bryant misses 7-foot jumper      M
## 4 Kobe Bryant makes 16-foot jumper (Derek Fisher assists)      H
## 5                         Kobe Bryant makes driving layup      H
## 6                               Kobe Bryant misses jumper      M
kobe$basket[1:9]
## [1] "H" "M" "M" "H" "H" "M" "M" "M" "M"

1. What does a streak length of 1 mean, i.e. how many hits and misses are in a streak of 1? What about a streak length of 0?

A streak of 1 means that there was 1 basket made but the next one missed. A streak of 0 means that no baskets were made. A streak of 2 means that 2 baskets were made before one was missed.
kobe_streak <- calc_streak(kobe$basket)
barplot(table(kobe_streak))

2. Describe the distribution of Kobe’s streak lengths from the 2009 NBA finals. What was his typical streak length? How long was his longest streak of baskets?

The distribution is right-skewed--he had very few actual streaks (more than one basket in a row), most often just hitting once and then missing again, and none more than 4 in a row.

Simulations in R

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.

sim_fair_coin <- sample(outcomes, size = 100, replace = TRUE)

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.

sim_fair_coin
##   [1] "heads" "heads" "tails" "tails" "tails" "heads" "tails" "heads"
##   [9] "tails" "tails" "tails" "heads" "heads" "tails" "tails" "heads"
##  [17] "heads" "tails" "heads" "heads" "heads" "tails" "tails" "tails"
##  [25] "tails" "heads" "tails" "heads" "tails" "heads" "heads" "tails"
##  [33] "tails" "tails" "heads" "heads" "heads" "heads" "tails" "heads"
##  [41] "heads" "tails" "heads" "heads" "heads" "tails" "heads" "heads"
##  [49] "heads" "tails" "heads" "tails" "heads" "tails" "heads" "heads"
##  [57] "tails" "heads" "tails" "heads" "heads" "heads" "tails" "tails"
##  [65] "tails" "heads" "heads" "heads" "tails" "tails" "heads" "tails"
##  [73] "tails" "heads" "heads" "tails" "tails" "tails" "heads" "tails"
##  [81] "tails" "tails" "tails" "tails" "tails" "heads" "tails" "heads"
##  [89] "tails" "tails" "tails" "heads" "tails" "tails" "tails" "heads"
##  [97] "tails" "heads" "tails" "tails"
table(sim_fair_coin)
## sim_fair_coin
## heads tails 
##    47    53

I ran the above code 50 times and it always came back with 49 heads and 51 tails so I don’t know about Kobe, but I know that this formula doesn’t seem kosher!

I ran it on another machine at it came out 47 heads and 53 tails every time.

sim_unfair_coin <- sample(outcomes, size = 100, replace = TRUE, prob = c(0.2, 0.8))
sim_unfair_coin
##   [1] "tails" "heads" "tails" "tails" "tails" "tails" "heads" "tails"
##   [9] "tails" "heads" "tails" "tails" "tails" "tails" "tails" "tails"
##  [17] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "heads"
##  [25] "tails" "tails" "tails" "heads" "tails" "heads" "tails" "tails"
##  [33] "tails" "tails" "tails" "tails" "heads" "heads" "tails" "tails"
##  [41] "tails" "tails" "heads" "tails" "tails" "heads" "heads" "heads"
##  [49] "tails" "tails" "tails" "heads" "tails" "tails" "heads" "tails"
##  [57] "tails" "tails" "tails" "heads" "tails" "tails" "tails" "tails"
##  [65] "tails" "heads" "tails" "tails" "tails" "tails" "tails" "tails"
##  [73] "tails" "tails" "tails" "heads" "tails" "tails" "tails" "tails"
##  [81] "tails" "tails" "heads" "heads" "tails" "tails" "tails" "tails"
##  [89] "tails" "tails" "tails" "tails" "tails" "heads" "tails" "tails"
##  [97] "tails" "tails" "tails" "tails"
table(sim_unfair_coin)
## sim_unfair_coin
## heads tails 
##    20    80

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%.

3. In your simulation of flipping the unfair coin 100 times, how many flips came up heads?

21/79, 16/84, 15/85, 18/82, 19/81

Simulating the Independent Shooter

outcomes <- c("H", "M")
sim_basket <- sample(outcomes, size = 1, replace = TRUE)

To make a valid comparison between Kobe and our simulated independent shooter, we need to align both their shooting percentage and the number of attempted shots.

4. What change needs to be made to the 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.

It should be: sample(outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))
outcomes <- c("H", "M")
sim_basket <- sample(outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))
table(sim_basket)
## sim_basket
##  H  M 
## 51 82
kobe$basket
##   [1] "H" "M" "M" "H" "H" "M" "M" "M" "M" "H" "H" "H" "M" "H" "H" "M" "M"
##  [18] "H" "H" "H" "M" "M" "H" "M" "H" "H" "H" "M" "M" "M" "M" "M" "M" "H"
##  [35] "M" "H" "M" "M" "H" "H" "H" "H" "M" "H" "M" "M" "H" "M" "M" "H" "M"
##  [52] "M" "H" "M" "H" "H" "M" "M" "H" "M" "H" "H" "M" "H" "M" "M" "M" "H"
##  [69] "M" "M" "M" "M" "H" "M" "H" "M" "M" "H" "M" "M" "H" "H" "M" "M" "M"
##  [86] "M" "H" "H" "H" "M" "M" "H" "M" "M" "H" "M" "H" "H" "M" "H" "M" "M"
## [103] "H" "M" "M" "M" "H" "M" "H" "H" "H" "M" "H" "H" "H" "M" "H" "M" "H"
## [120] "M" "M" "M" "M" "M" "M" "H" "M" "H" "M" "M" "M" "M" "H"
sim_basket
##   [1] "M" "H" "M" "M" "M" "H" "M" "M" "H" "H" "M" "M" "H" "H" "M" "M" "H"
##  [18] "H" "H" "H" "H" "M" "H" "M" "M" "M" "H" "H" "H" "M" "M" "M" "M" "H"
##  [35] "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "M" "H" "M" "H" "H"
##  [52] "M" "H" "M" "M" "H" "M" "M" "H" "M" "M" "H" "M" "M" "M" "H" "H" "M"
##  [69] "H" "M" "M" "M" "M" "M" "H" "H" "M" "H" "M" "M" "H" "H" "M" "M" "M"
##  [86] "M" "H" "M" "H" "M" "M" "H" "H" "M" "H" "M" "H" "M" "M" "M" "H" "M"
## [103] "M" "M" "M" "H" "M" "H" "M" "H" "H" "M" "M" "H" "H" "M" "M" "M" "H"
## [120] "H" "M" "H" "M" "M" "H" "H" "M" "H" "M" "M" "M" "M" "H"
table(sim_basket)
## sim_basket
##  H  M 
## 51 82

On your own

Comparing Kobe Bryant to the Independent Shooter

Using calc_streak, compute the streak lengths of sim_basket.

calc_streak(sim_basket)
##  [1] 0 1 0 0 1 0 2 0 2 0 5 1 0 0 3 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 2 1 0
## [36] 1 0 1 0 1 0 0 2 1 0 0 0 0 2 1 0 2 0 0 0 1 1 0 2 1 1 0 0 1 0 0 0 1 1 2
## [71] 0 2 0 0 2 1 0 2 1 0 0 0 1
table(calc_streak(sim_basket))
## 
##  0  1  2  3  5 
## 49 21 11  1  1
kframe <- data.frame(table(kobe_streak))
simframe <- data.frame(table(calc_streak(sim_basket)))

colnames(kframe) <- c("streak", "kobe")
colnames(simframe) <- c("streak", "sim")

streakframe <- merge(simframe, kframe, all = TRUE)
streakframe$streak <- (as.numeric(streakframe$streak)-1)

streakchange <- streakframe
streakchange$diff <- (streakchange$kobe - streakchange$sim)

streakchange
##   streak sim kobe diff
## 1      0  49   39  -10
## 2      1  21   24    3
## 3      2  11    6   -5
## 4      3   1    6    5
## 5      4   1   NA   NA
## 6      5  NA    1   NA
streakplotdata <- melt(streakframe, id.vars='streak')

colnames(streakplotdata) <- c("streak", "player","observations" )
streakplotdata$streak <- as.factor(streakplotdata$streak)


streakplot <- ggplot(streakplotdata, aes(x=streak, y=observations, fill=player)) +
    geom_bar(stat='identity', position='dodge') + xlab("Streak Number") + labs(title="Hot Hands Comparison: Kobe vs. Simulated Player")

streakplot                                              
## Warning: Removed 2 rows containing missing values (geom_bar).

Describe the distribution of streak lengths. What is the typical streak length for this simulated independent shooter with a 45% shooting percentage?-

## The mean streak length for a simulated independent shooter is 0.6144578
## The mean streak length for a Kobe was 0.7631579

How long is the player’s longest streak of baskets in 133 shots?

## The max streak length for a simulated independent shooter is 5

If you were to run the simulation of the independent shooter a second time how would you expect its streak distribution to compare to the distribution from the question above? Exactly the same? Somewhat similar? Totally different? Explain your reasoning.

It should be somewhat similar as it's based on the same set of odds 45% / 55% and the law of large numbers says that the results should follow a similar curve. Also, the simulation is rerun every time I knit this document and they are always similar.

How does Kobe Bryant’s distribution of streak lengths compare to the distribution of streak lengths for the simulated shooter? Using this comparison, do you have evidence that the hot hand model fits Kobe’s shooting patterns? Explain.

The patterns are similar. Kobe makes more shots in general but there's no overwhelming evidence of the hot hand theory in play here.