A note on workspaces: Before we get started with the lab, let’s take a moment to review our R Markdown workflow and remind ourselves about workspaces in R. The workspaces of the console and the workspaces of your R Markdown document are not the same. Therefore, if you define a variable only in the Console and then try to use that variable in your R Markdown document, you’ll get an error. This might seem frustrating at first, but it is actually a feature that helps you in the long run. In order to ensure that your report is fully reproducible, everything that is used in the report must be defined in the report, and not somewhere else.

It is your responsibility, and an important learning goal of this course, that you master the skills for creating fully reproducible data analysis reports. Below are some tips for achieving this goal:


Your reproducible lab report: Before you get started, download the R Markdown template for this lab. Remember all of your code and answers go in this document:

download.file("https://dyurovsky.github.io/psyc20100/post/rmd/lab3.Rmd", 
              destfile = "lab3.Rmd")

Hot Hands

Basketball players who make several baskets in succession are described as having hot hands. Fans and players have long believed in the hot hands phenomenon, which refutes the assumption that each shot is independent of the next. However, a 1985 paper by Gilovich, Vallone, and Tversky collected evidence that contradicted this belief and showed that successive shots are independent events. This paper started a great controversy that continues to this day, as you can see by Googling hot hands basketball.

We do not expect to resolve this controversy today. However, in this lab we’ll apply the Null Hypothesis testing framework to help you see how to approach 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 Null Hypothesis distributions in R, and (3) to compare a simulation to actual data in order to determine if the hot hands phenomenon appears to be real.

Getting Started

Our investigation will focus on the performance of one player: Stephen Curry of the Gold State Warriors. Curry has been the NBA Most Valuable Player for both of the last two seasons–last year by unanimous vote. Maybe that’s because of hot hands? We’ll be looking at some data on Curry that I pulled from NBA Stats.

Let’s read in the data and see what we have.

curry_data <- read.csv("https://dyurovsky.github.io/psyc20100/data/lab3/curry_data.csv")

Let’s take a look at this new data set: curry_data. Try using View(curry_data) in your console (or clicking on the data frame in the Environment tab). This data frame has information about every shot that Steph Curry took during the 2014-2015 regular season. There are 1341 observations and 14 variables, where every row records a single shot taken. The SHOT_MADE_FLAG variable in this dataset indicates whether the shot scored (1) or was a miss (0).

As always, let’s do a little bit of exploratory data analysis to make sure we understand our data. Let’s do two things. First, let’s compute Curry’s average shooting success. Second, let’s use the group_by and summarise functions from dplyr to look at his shooting success from game to game.

mean_shooting_prob <- mean(curry_data$SHOT_MADE_FLAG)

by_game_data <- curry_data %>%
  group_by(GAME_ID) %>%
  summarise(shooting_prob = mean(SHOT_MADE_FLAG))

qplot(data = by_game_data, x = shooting_prob, bins = 15) +
  geom_vline(xintercept = mean_shooting_prob, color = "gold", size = 2)

  1. Describe the distribution that we’re seeing. What proportion of his shots does Curry make on average, and how does this vary from game to game?

Now let’s use this data to ask whether Curry’s shots show evidence of hot hands. One way we can approach this question is to look at whether Curry is more likely to make his next shot if he has just made his previous shot.

  1. Come up with a Null Hypothesis and Alternative Hypothesis that we can use to ask whether Curry’s shots provide evidence for the hot hands phenomenon.

We can use the lag function from dplyr to make our life a lot easier. This will let us compare each row in the dataframe with the row that came just before it. Because the data are already arranged chronologically, each shot in our data frame comes right after the shot it follwed.

Let’s try this out.

lag_data <- curry_data %>%
  group_by(GAME_ID) %>%
  mutate(lag_shot = lag(SHOT_MADE_FLAG))

I’ll select just the three relevant variables to make looking at the data easier, and then print out the first 10 rows using kable (to make them a little bit prettier).

lag_data %>%
  select(GAME_ID, SHOT_MADE_FLAG, lag_shot) %>%
  head(., n = 10) %>%
  kable
GAME_ID SHOT_MADE_FLAG lag_shot
21400014 1 NA
21400014 0 1
21400014 1 0
21400014 1 1
21400014 0 1
21400014 0 0
21400014 0 0
21400014 0 0
21400014 0 0
21400014 1 0

You can see that the lag_shot column contains exactly the value that is in SHOT_MADE_FLAG in the previous row.

  1. Why did I group_by game first before calling the lag function? Hint: The NA you see in the first row means that there is no value that came before.

We can now use the new lag_shot variable to group shots by whether they followed a successful or missed shot. And thus we can test our Alternative Hypothesis. Let’s do one more quick exploratory data analysis to see what the difference in Curry’s shooting percentage is like following successful vs. unsuccessful shots.

eda_hothands <- lag_data %>%
  group_by(lag_shot) %>%
  summarise(shooting_prob = mean(SHOT_MADE_FLAG))
  1. What do you see when you run this code? Does it look like we see evidence for the hot hands phenomenon? Also, what does the value for shooting_prob for NA tell us?

Compared to what?

To answer this questions, let’s return to the idea of independence. Two processes are independent if the outcome of one process doesn’t affect 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 hands hypothesis says he will have a higher probability of making his second shot.

Simulations in R

Now we’re ready to test our hypothesis formally. What we need to do is generate the Null Distribution for what kind of differences in shots following successful vs. unsuccessful shots we should see if there is no hot hands phenomenon–if Curry shoots identically following successful vs. unsucessful shots. We’ll do this through sampling just like we did in lecture. Except in lecture, our data were arrays and this time our data is in a data frame. So we will use a different but very related function: sample_frac. This function will produce a random sample of a specified fraction of the rows in a data frame. When we call sample_frac(1), it will sample 100% of the rows. But, critically, it will sample them in a random order instead of the order they are currently in.

So what we want to do, is keep everything about the shooting data identical – e.g. how many shots Curry took, how many of them were successful, etc. The only thing we want to do is shuffle the order of the shots so that we destroy any dependence on previous shots.

simulate_null <- function() {
  random_data <- curry_data %>%
    group_by(GAME_ID) %>%
    sample_frac(1) %>%
    mutate(lag_shot = lag(SHOT_MADE_FLAG)) %>%
    filter(!is.na(lag_shot)) %>% #throw out the first shot of each game
    group_by(lag_shot) %>%
    summarise(shooting_prob = mean(SHOT_MADE_FLAG))

  return(random_data[random_data$lag_shot == 1, "shooting_prob"] - 
           random_data[random_data$lag_shot == 0, "shooting_prob"])
}
  1. Based on the code we looked at during the last two lectures, generate a Null distribution using this sampling function, and compare it to the empirical difference we saw in Curry’s shots. Do we see any evidence of hot hands? Or maybe cold hands?

If you want to learn more about sample_frac or any other function, recall that you can always check out its help file.

?sample_frac

On your own

  • Where does the empirical difference between shots following success and failures fall on the null distribution (what percentile?) I