download.file("http://www.openintro.org/stat/data/kobe.RData",destfile ="kobe.RData")
load("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

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

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

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

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

Answer: from the graph of the barplot, it showed that the typical streak length was 0 where as the longest streak was 4

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

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

Answer: 46 flips came up heads

Ex4: 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

outcomes <- c("H", "M")
sim_basket <- sample(outcomes,size=133,replace = TRUE,prob = c(0.45,0.55))
sim_basket
##   [1] "M" "H" "M" "M" "H" "M" "H" "H" "H" "M" "H" "M" "M" "H" "M" "H" "M"
##  [18] "M" "M" "H" "H" "H" "M" "M" "H" "M" "H" "M" "M" "H" "M" "H" "M" "M"
##  [35] "M" "H" "H" "H" "M" "M" "H" "M" "H" "M" "M" "H" "M" "H" "H" "M" "M"
##  [52] "H" "M" "H" "M" "H" "M" "H" "H" "M" "M" "H" "M" "M" "M" "H" "H" "M"
##  [69] "H" "M" "H" "H" "M" "M" "M" "M" "M" "H" "H" "M" "H" "M" "H" "H" "M"
##  [86] "M" "M" "H" "M" "H" "H" "M" "M" "M" "M" "M" "M" "M" "H" "M" "M" "H"
## [103] "M" "H" "H" "H" "M" "H" "M" "M" "H" "M" "H" "M" "H" "H" "M" "M" "H"
## [120] "H" "M" "M" "M" "H" "H" "M" "M" "H" "M" "M" "H" "H" "M"
table(sim_basket)
## sim_basket
##  H  M 
## 59 74

On Your Own

1, 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_shooter_Independant <- calc_streak(sim_basket)
sim_shooter_Independant
##  [1] 0 1 0 1 3 1 0 1 1 0 0 3 0 1 1 0 1 1 0 0 3 0 1 1 0 1 2 0 1 1 1 2 0 1 0
## [36] 0 2 1 2 0 0 0 0 2 1 2 0 0 1 2 0 0 0 0 0 0 1 0 1 3 1 0 1 1 2 0 2 0 0 2
## [71] 0 1 0 2 0
barplot(table(sim_shooter_Independant))

#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. #Answer: It’s somewhat similar since the distribution is a unimodule distribution and skewing towards left. #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_streak2009))

barplot(table(sim_shooter_Independant))

#Answer: