Let’s shoot some hoops!

Welcome to the second lab! In this lab, you’ll learn all about probability, dependent and independent events, and simulating event sequences in R.

Throughout this lab, we’ll investigate the much debated phenomenon of hot hands. When a basketball player makes several baskets in succession, they are describe as having a “hot hand”. Fans and players have long believed in this phenomenon, which refutes the assumption that each shot is independent of the next. However, a 1985 paper by Gilovich, Vallone, and Tversky found evidence that contradicted this belief and showed that successive shots are independent events.

We do not expect to resolve this controversy today. However, in this lab we’ll apply one approach to answering questions like this. 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.

Getting started

Our investigation will focus on the performance of one player: Kobe Bryant of the Los Angeles Lakers.

His performance against the Orlando Magic in the 2009 NBA finals earned him the title “Most Valuable Player” and many spectators commented on how he appeared to show a hot hand. Let’s load some data from those games and look at the first several rows.

# Load the data frame
load(url('http://s3.amazonaws.com/assets.datacamp.com/course/dasi/kobe.RData'))

# Inspect your data
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
tail(kobe)
##      vs game quarter    time                        description basket
## 128 ORL    3       4    3:57  Bryant Jump Shot: Made (28 PTS)        H
## 129 ORL    3       4    3:33        Bryant Layup Shot: Missed        M
## 130 ORL    3       4    2:02          Bryant 3pt Shot: Missed        M
## 131 ORL    3       4 00:23.9          Bryant 3pt Shot: Missed        M
## 132 ORL    3       4 00:06.9          Bryant 3pt Shot: Missed        M
## 133 ORL    3       4 00:00.5 Bryant Layup Shot: Made (31 PTS)        H

Kobe’s track record

In the kobe data frame, every row records a shot taken by Kobe Bryant. If he hit the shot (made a basket), a hit, H, is recorded in the column named basket, otherwise a miss, M, is recorded.

Just looking at the string of hits and misses, it can be difficult to gauge whether or not it seems like Kobe was shooting with a hot hand. One way we can approach this is by considering the belief that hot hand shooters tend to go on shooting streaks. For this lab, we define the length of a shooting streak to be the number of consecutive baskets made until a miss occurs.

For example, in Game 1 Kobe had the following sequence of hits and misses from his nine shot attempts in the first quarter: H M | M | H H M | M | M | M.

# Print the variable names of the data frame.
names(kobe)
## [1] "vs"          "game"        "quarter"     "time"        "description"
## [6] "basket"
# Print the first 9 values of the 'basket' variable
kobe$basket[1:9]
## [1] "H" "M" "M" "H" "H" "M" "M" "M" "M"

That’s correct! You can see that within the nine shot attempts, there are six streaks, which are separated by a ‘|’ above. Their lengths are one, zero, two, zero, zero, zero.

Definition: the length of a shooting streak is the number of consecutive baskets (hits) until a miss occurs.

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

A hit followed by a miss.

What about a streak length of 0? How many hits and misses are in a streak of 0?

A miss followed by a miss.

A first analysis

To start, let’s look at the distribution of the shooting streaks. Along with the data frame, we also loaded the custom function calc_streak into the workspace.

We’ll use this function to calculate the lengths of all shooting streaks and then look at the distribution with a barplot. Note that a bar plot is preferable to a histogram here since our variable is discrete – counts – instead of continuous.

calc_streak = function(x) {
  y <- rep(0, length(x))
  y[x == "H"] <- 1
  y <- c(0, y, 0)
  wz <- which(y == 0)
  streak <- diff(wz) - 1
  return(streak)
}
kobe_streak = calc_streak(kobe$basket)
barplot(table(kobe_streak))

plot of chunk unnamed-chunk-4

Which of the following is false about the distribution of Kobe’s streak lengths from the 2009 NBA finals?

Hint: You may also want to use other visualizations and summaries of kobe streak in order to answer this question.

boxplot(kobe_streak)

plot of chunk unnamed-chunk-5

IQR(kobe_streak)
## [1] 1
summary(kobe_streak)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   0.000   0.000   0.763   1.000   4.000

The distribution of Kobe’s streaks is unimodal and left skewed.

We’ve shown that Kobe had some long shooting streaks, but are they long enough to support the belief that he had hot hands? What can we compare them to?

To answer these questions, let’s return to the idea of independence. Two processes are independent if the outcome of one process doesn’t effect the outcome of the second. If each shot that a player takes is an independent process, having made or missed your first shot will not affect the probability that you will make or miss your second shot.

A shooter with a hot hand will have shots that are not independent of one another. Specifically, if the shooter makes his first shot, the hot hand model says he will have a higher probability of making his second shot. Let’s suppose for a moment that the hot hand model is valid for Kobe. During his career, the percentage of time Kobe makes a basket (i.e. his shooting percentage) is about 45%, or in probability notation,

\(P(\text{shot}_1 = H) = 0.45\)

If Kobe has a hot hand (not independent shots), then the probability that he makes his second shot would go up given that he made the first shot:

\(P(\text{shot}_2=H | \text{shot}_1=H) > 0.45\) .

TRUE

Exactly, if Kobe has a hot hand, the probability that he makes his second shot would be higher, for example 0.60. As a result of these increased probabilities, you’d expect Kobe to have longer streaks. Compare this to the skeptical perspective where Kobe does not have a hot hand, where each shot is independent of the next. If he hits his first shot, the probability that he makes the second is still 0.45:

\(P(\text{shot}_2=H | \text{shot}_1=H) = 0.45\)

In other words, making the first shot did nothing to effect the probability that he’d make his second shot. If Kobe’s shots are independent, then he’d have the same probability of hitting every shot regardless of his past shots: 45%.

Now that we’ve phrased the situation in terms of independent shots, let’s return to the question: how do we tell if Kobe’s shooting streaks are long enough to indicate that he has hot hands? We can compare his streak lengths to someone without hot hands: an independent shooter. Starting with the next exercise, you’ll learn how to simulate such an independent shooter in R.

If Kobe’s shooting streaks diverge significantly from an independent shooter’s streaks, we can conclude…

We can conclude that Kobe likely has a hot hand.

Simulations in R

So the key is to compare Kobe’s data with that of a shooter who we know to have independent shots. While we don’t have any such data, that is very easy to simulate in R. In a simulation, you set the ground rules of a random process and then the computer uses random numbers to generate an outcome that adheres to those rules. As a simple example, you can simulate flipping a fair coin with the following commands. outcomes = c(“heads”, “tails”) sample(outcomes, size=1, replace=TRUE) The vector outcomes can be thought of as a hat with two slips of paper in it: one slip says “heads” and the other says “tails”. The function sample draws one slip from the hat and tells us if it was a head or a tail.

outcomes = c("heads", "tails")

sample(outcomes, size = 1, replace = TRUE)
## [1] "heads"
sample(outcomes, size = 1, replace = TRUE)
## [1] "tails"
sample(outcomes, size = 1, replace = TRUE)
## [1] "tails"

Flipping 100 coins

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. Since there are only two elements in outcomes, the probability that we “flip” a coin and it lands heads is 0.5. Suppose again that you have a hat with two slips of paper in it: one slip says “heads” and the other says “tails”. Therefore at each draw, the probability of drawing a chip that says “head” is 50%, and “tail” is 50%. The replace = TRUE argument indicates we put the chip back in the hat before drawing again, therefore always keeping the 50/50 odds.

outcomes = c("heads", "tails")
sim_fair_coin = sample(outcomes, size=100, replace=T)
sim_fair_coin
##   [1] "heads" "tails" "tails" "heads" "tails" "tails" "tails" "tails"
##   [9] "heads" "tails" "heads" "heads" "heads" "heads" "heads" "tails"
##  [17] "heads" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
##  [25] "heads" "tails" "tails" "heads" "tails" "heads" "heads" "tails"
##  [33] "tails" "heads" "tails" "tails" "heads" "tails" "tails" "heads"
##  [41] "heads" "heads" "tails" "tails" "tails" "heads" "tails" "heads"
##  [49] "heads" "tails" "tails" "heads" "heads" "tails" "tails" "heads"
##  [57] "tails" "tails" "tails" "heads" "heads" "heads" "tails" "heads"
##  [65] "heads" "tails" "tails" "heads" "heads" "tails" "tails" "tails"
##  [73] "heads" "heads" "tails" "heads" "heads" "tails" "tails" "heads"
##  [81] "tails" "heads" "heads" "tails" "heads" "heads" "tails" "tails"
##  [89] "heads" "tails" "tails" "tails" "tails" "tails" "tails" "heads"
##  [97] "tails" "tails" "heads" "heads"
table(sim_fair_coin)
## sim_fair_coin
## heads tails 
##    44    56

Flipping an unfair coin

Until now you’ve been running simulations where each outcome had an equal probability. However, the sample function also allows you to set the probabilities. You can set these probabilities by adding an argument called prob to the sample function. This argument needs a vector of probability weights, one for each possible outcome. An example of such a vector for three possible outcomes is c(0.1, 0.6, 0.3). Note that for the fair coin the probability weight vector is c(0.5, 0.5). The default of the sample function (when no prob is given) is for all outcomes to have equal probability.

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

Simulating the Independent Shooter

Simulating a basketball player who has independent shots uses the same mechanism that we used to simulate a coin flip. To simulate a single shot from an independent shooter with a shooting percentage of 50% we type, 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 percentages and their numbers of attempted shots.

outcomes = c("H", "M")
sim_basket = sample(outcomes, size=133, replace=T, prob=c(0.45, 0.55))

sim_basket
##   [1] "M" "M" "M" "H" "H" "M" "M" "M" "M" "H" "H" "H" "H" "M" "M" "M" "H"
##  [18] "H" "H" "M" "M" "M" "H" "H" "H" "M" "H" "M" "M" "H" "M" "H" "M" "M"
##  [35] "H" "H" "M" "M" "M" "H" "M" "H" "M" "H" "M" "M" "M" "M" "M" "M" "M"
##  [52] "H" "H" "M" "H" "H" "H" "H" "H" "M" "M" "H" "H" "H" "M" "H" "H" "M"
##  [69] "M" "H" "M" "H" "M" "H" "M" "H" "M" "M" "H" "M" "H" "M" "H" "M" "M"
##  [86] "H" "M" "M" "H" "H" "H" "H" "H" "M" "M" "M" "H" "H" "H" "M" "H" "M"
## [103] "H" "M" "M" "M" "H" "M" "H" "M" "H" "M" "M" "H" "M" "H" "M" "H" "H"
## [120] "M" "M" "M" "M" "M" "M" "H" "M" "M" "M" "M" "M" "M" "H"
table(sim_basket)
## sim_basket
##  H  M 
## 59 74

Kobe vs. the Independent Shooter

With the results of the simulation saved as sim_basket, you have the data you need to compare Kobe to your independent shooter. We can look at Kobe’s data alongside our simulated data: kobe$basket vs. sim_basket.

Both data sets represent the results of 133 shot attempts, each with the same shooting percentage of 45%. We know that our simulated data is from a shooter that has independent shots. That is, we know the simulated shooter does not have a hot hand.

kobe_streak = calc_streak(kobe$basket)
sim_streak = calc_streak(sim_basket)

# Compute summaries:
summary(kobe_streak)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   0.000   0.000   0.763   1.000   4.000
summary(sim_streak)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   0.000   0.000   0.787   1.000   5.000
# Make bar plots:
kobe_table = table(kobe_streak)
sim_table = table(sim_streak)

barplot(kobe_table)

plot of chunk unnamed-chunk-10

barplot(sim_table)

plot of chunk unnamed-chunk-10

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? > A somewhat similar distribution

How does Kobe Bryant’s distribution of streak lengths compare to the distribution of streak lengths for the simulated shooter?

Protip: Again, you can draw barplots of both Kobe and the simulated shooter. Barplots are a great help for making visual comparisons between data like these.

kobe_table = table(kobe_streak)
sim_table = table(sim_streak)

barplot(kobe_table)

plot of chunk unnamed-chunk-11

barplot(sim_table)

plot of chunk unnamed-chunk-11

Since Kobe’s streak length distribution looks very similar to the independent shooter’s simulated steak length distribution, we can conclude that Kobe Bryant likely does not have a ”hot hand”.

TRUE