Source files: https://github.com/djlofland/DATA606_F2019/tree/master/Lab3

`## ── Attaching packages ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.2.1 ──`

```
## ✔ ggplot2 3.2.1 ✔ purrr 0.3.2
## ✔ tibble 2.1.3 ✔ dplyr 0.8.3
## ✔ tidyr 0.8.3 ✔ stringr 1.4.0
## ✔ readr 1.3.1 ✔ forcats 0.4.0
```

```
## ── Conflicts ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
```

```
##
## Attaching package: 'lubridate'
```

```
## The following object is masked from 'package:base':
##
## date
```

Basketball players who make several baskets in succession are described as having a *hot hand*. Fans and players have long believed in the hot hand 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 (http://psych.cornell.edu/sites/default/files/Gilo.Vallone.Tversky.pdf). This paper started a great controversy that continues to this day, as you can see by Googling *hot hand basketball*.

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.

Click on File -> New -> R Script. This will open a blank document above the console. As you go along you can copy and paste your code here and save it. This is a good way to keep track of your code and be able to reuse it later. To run your code from this document you can either copy and paste it into the console, highlight the code and hit the Run button, or highlight the code and hit command+enter or a mac or control+enter on a PC.

You’ll also want to save this script (code document). To do so click on the disk icon. The first time you hit save, RStudio will ask for a file name; you can name it anything you like. Once you hit save you’ll see the file appear under the Files tab in the lower right panel. You can reopen this file anytime by simply clicking on it.

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.

```
## 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
## 7 ORL 1 1 4:07
## 8 ORL 1 1 0:52
## 9 ORL 1 1 0:00
## 10 ORL 1 2 6:35
## 11 ORL 1 2 5:58
## 12 ORL 1 2 5:22
## 13 ORL 1 2 4:37
## 14 ORL 1 2 3:30
## 15 ORL 1 2 2:55
## 16 ORL 1 2 1:55
## 17 ORL 1 2 0:38
## 18 ORL 1 2 0:04
## 19 ORL 1 3 11:44
## 20 ORL 1 3 11:15
## 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
## 7 Kobe Bryant misses 12-foot jumper M
## 8 Kobe Bryant misses 19-foot jumper M
## 9 Kobe Bryant misses layup M
## 10 Kobe Bryant makes jumper H
## 11 Kobe Bryant makes 20-foot jumper H
## 12 Kobe Bryant makes 14-foot jumper H
## 13 Kobe Bryant misses driving layup M
## 14 Kobe Bryant makes 9-foot two point shot H
## 15 Kobe Bryant makes 14-foot running jumper H
## 16 Kobe Bryant misses 19-foot jumper M
## 17 Kobe Bryant misses 27-foot three point jumper M
## 18 Kobe Bryant makes driving layup H
## 19 Kobe Bryant makes layup H
## 20 Kobe Bryant makes 11-foot two point shot H
```

In this 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:

\[ \textrm{H M | M | H H M | M | M | M} \]

To verify this use the following command:

`## [1] "H" "M" "M" "H" "H" "M" "M" "M" "M"`

```
kobe <- kobe %>% group_by(game, quarter) %>% mutate(time_lead = lead(time, n=1, default=NA))
kobe$time <- ms(kobe$time)
kobe$time_lead <- ms(kobe$time_lead)
```

```
## Warning in .parse_hms(..., order = "MS", quiet = quiet): Some strings
## failed to parse, or all strings are NAs
```

```
## Note: method with signature 'Period#ANY' chosen for function '-',
## target signature 'Period#Period'.
## "ANY#Period" would also be valid
```

```
## # A tibble: 133 x 8
## # Groups: game, quarter [22]
## vs game quarter time description basket time_lead delta
## <fct> <int> <fct> <Perio> <fct> <chr> <Period> <dbl>
## 1 ORL 1 1 9M 47S Kobe Bryant makes 4-… H 9M 7S 40
## 2 ORL 1 1 9M 7S Kobe Bryant misses j… M 8M 11S 56
## 3 ORL 1 1 8M 11S Kobe Bryant misses 7… M 7M 41S 30
## 4 ORL 1 1 7M 41S Kobe Bryant makes 16… H 7M 3S 38
## 5 ORL 1 1 7M 3S Kobe Bryant makes dr… H 6M 1S 62
## 6 ORL 1 1 6M 1S Kobe Bryant misses j… M 4M 7S 114
## 7 ORL 1 1 4M 7S Kobe Bryant misses 1… M 52S 195
## 8 ORL 1 1 52S Kobe Bryant misses 1… M 0S 52
## 9 ORL 1 1 0S Kobe Bryant misses l… M NA NA
## 10 ORL 1 2 6M 35S Kobe Bryant makes ju… H 5M 58S 37
## # … with 123 more rows
```

Within the nine shot attempts, there are six streaks, which are separated by a “|” above. Their lengths are one, zero, two, zero, zero, zero (in order of occurrence).

- 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?

It looks like a “streak” is defined as attempted

shots made within ~1 minute of each other, whether the shot missed or hit. I added a new columnkobe$deltawhich contain the time difference between the current shot and the next shot attempt.

The custom function `calc_streak`

, which was loaded in with the data, may be used to calculate the lengths of all shooting streaks and then look at the distribution.

NOTE: My plots don’t match the calc_streak() output exactly, so I’m guessing there are other rules for what a “streak” is that I don’t fully understand.

Note that instead of making a histogram, we chose to make a bar plot from a table of the streak data. A bar plot is preferable here since our variable is discrete – counts – instead of continuous.

- 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?

This looks like a typical

exponential or logrithmic dropoff/decaycurve. His longest streak was4 shots.

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(\textrm{shot 1 = H}) = 0.45 \]

If he makes the first shot and has a hot hand (*not* independent shots), then the probability that he makes his second shot would go up to, let’s say, 60%,

\[ P(\textrm{shot 2 = H} \, | \, \textrm{shot 1 = H}) = 0.60 \]

As a result of these increased probabilites, 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 hit his first shot, the probability that he makes the second is still 0.45.

\[ P(\textrm{shot 2 = H} \, | \, \textrm{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.

While we don’t have any data from a shooter we know to have independent shots, that sort of data 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.

`## [1] "heads"`

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.

Run the second command listed above several times. Just like when flipping a coin, sometimes you’ll get a heads, sometimes you’ll get a tails, but in the long run, you’d expect to get roughly equal numbers of each.

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`

.

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.

```
## [1] "tails" "tails" "tails" "heads" "heads" "tails" "heads" "heads"
## [9] "tails" "heads" "heads" "heads" "tails" "tails" "tails" "heads"
## [17] "heads" "tails" "tails" "heads" "tails" "tails" "heads" "tails"
## [25] "tails" "heads" "heads" "heads" "tails" "heads" "tails" "heads"
## [33] "tails" "heads" "tails" "heads" "tails" "heads" "tails" "heads"
## [41] "heads" "heads" "tails" "tails" "tails" "heads" "tails" "tails"
## [49] "tails" "heads" "heads" "heads" "heads" "tails" "heads" "tails"
## [57] "heads" "heads" "tails" "tails" "tails" "tails" "heads" "tails"
## [65] "heads" "tails" "tails" "heads" "heads" "tails" "tails" "heads"
## [73] "heads" "heads" "heads" "tails" "heads" "tails" "heads" "tails"
## [81] "tails" "tails" "tails" "tails" "heads" "heads" "heads" "tails"
## [89] "tails" "tails" "heads" "tails" "tails" "heads" "tails" "heads"
## [97] "tails" "tails" "tails" "tails"
```

```
## sim_fair_coin
## heads tails
## 46 54
```

Since there are only two elements in `outcomes`

, the probability that we “flip” a coin and it lands heads is 0.5. Say we’re trying to simulate an unfair coin that we know only lands heads 20% of the time. We can adjust for this by adding an argument called `prob`

, which provides a vector of two probability weights.

```
sim_unfair_coin <- sample(outcomes, size = 100, replace = TRUE, prob = c(0.2, 0.8))
table(sim_unfair_coin)
```

```
## sim_unfair_coin
## heads tails
## 22 78
```

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

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

I ran it a number of times and saw:

13, 20,1 8, 22, 26

In a sense, we’ve shrunken the size of the slip of paper that says “heads”, making it less likely to be drawn and we’ve increased the size of the slip of paper saying “tails”, making it more likely to be drawn. When we simulated the fair coin, both slips of paper were the same size. This happens by default if you don’t provide a `prob`

argument; all elements in the `outcomes`

vector have an equal probability of being drawn.

If you want to learn more about `sample`

or any other function, recall that you can always check out its help file.

```
## Help on topic 'sample' was found in the following packages:
##
## Package Library
## dplyr /Users/dlofland/Library/R/3.6/library
## base /Library/Frameworks/R.framework/Resources/library
##
##
## Using the first match ...
```

Simulating a basketball player who has independent shots uses the same mechanism that we use to simulate a coin flip. To simulate a single shot from an independent shooter with a shooting percentage of 50% we type,

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.

- 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`

.

Note that we’ve named the new vector `sim_basket`

, the same name that we gave to the previous vector reflecting a shooting percentage of 50%. In this situation, R overwrites the old object with the new one, so always make sure that you don’t need the information in an old vector before reassigning its name.

With the results of the simulation saved as `sim_basket`

, we have the data necessary to compare Kobe to our independent shooter. We can look at Kobe’s data alongside our simulated data.

```
## [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"
```

```
## [1] "M" "M" "M" "H" "M" "M" "M" "H" "M" "M" "H" "H" "M" "M" "H" "M" "M"
## [18] "H" "H" "M" "M" "H" "M" "H" "H" "H" "H" "M" "M" "H" "M" "M" "H" "H"
## [35] "H" "H" "M" "H" "M" "M" "M" "H" "M" "M" "M" "M" "H" "M" "H" "H" "H"
## [52] "H" "H" "H" "H" "H" "H" "M" "M" "H" "H" "M" "M" "M" "H" "H" "H" "H"
## [69] "M" "H" "H" "M" "M" "H" "M" "H" "H" "H" "H" "H" "H" "M" "H" "M" "H"
## [86] "H" "H" "M" "M" "M" "M" "H" "M" "M" "M" "H" "H" "H" "M" "H" "H" "H"
## [103] "M" "M" "H" "M" "H" "M" "M" "M" "M" "M" "M" "H" "H" "H" "H" "M" "M"
## [120] "M" "M" "H" "H" "H" "H" "H" "H" "M" "H" "H" "H" "M" "H"
```

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.

Using `calc_streak`

, compute the streak lengths of `sim_basket`

.