library(openintro)
## Please visit openintro.org for free statistics materials
## 
## Attaching package: 'openintro'
## The following objects are masked from 'package:datasets':
## 
##     cars, trees

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

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} \]

kobe$basket[1:9]
## [1] "H" "M" "M" "H" "H" "M" "M" "M" "M"

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

streak length of 1 mean First a hit and then a miss. Then a streak length of 0 means 0 hits and 1 miss.

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.

kobe_streak <- calc_streak(kobe$basket)
barplot(table(kobe_streak))

summary(kobe_streak)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.0000  0.0000  0.0000  0.7632  1.0000  4.0000

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

According to the graph, there is a right-skewed distribution for Kobe’s streak length with a median of 0. Typical streak length is 0 and longest streak is 4.

Simulation in R

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

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

22 flips came out as heads

Simulating the Independent Shooter

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

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

sim_basket <- sample(outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55) )
table(sim_basket)
## sim_basket
##  H  M 
## 67 66
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"

On your own

Comparing Kobe Bryant to the Independent Shooter

Using calc_streak, compute the streak lengths of sim_basket.

# Describe the distribution of streak lengths. What is the typical streak length for this simulated independent shooter with a 45% shooting percentage? How long is the player’s longest streak of baskets in 133 shots?

sim_streak <- calc_streak (sim_basket)
table(sim_streak)
## sim_streak
##  0  1  2  3  4  6 
## 30 21  7  6  2  1
barplot(table(sim_streak))

Left skewed distribution with 4 as the longest streak.

# 2. 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.

sim_basket <- sample(outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55) )
table(sim_basket)
## sim_basket
##  H  M 
## 64 69

results of the second simulation is somewhat similar. Because simulation ran under same criteria as before with 133 shots and being placed back for sampling.

# 3.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

barplot(table(kobe_streak))

table(kobe_streak)
## kobe_streak
##  0  1  2  3  4 
## 39 24  6  6  1
barplot(table(sim_streak))

table(sim_streak)
## sim_streak
##  0  1  2  3  4  6 
## 30 21  7  6  2  1

Both graphs have a left skewed distribution eventhough their numbers are different. each shot is independant.