library(tidyverse)
library(openintro)
glimpse (kobe_basket) Rows: 133 Columns: 6 $ vs ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL,… $ game 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,… $ quarter 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3,… $ time 9:47, 9:07, 8:11, 7:41, 7:03, 6:01, 4:07, 0:52, 0:00, 6:35,… $ description Kobe Bryant makes 4-foot two point shot, Kobe Bryant misses… $ shot “H”, “M”, “M”, “H”, “H”, “M”, “M”, “M”, “M”, “H”, “H”, “H”,…
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?
A streak length of 1 means that he had one hit and zero misses. A streak length of zero would be one miss and no hits.
## function (x)
## {
## if (!is.atomic(x))
## x <- x[, 1]
## if (any(!x %in% c("H", "M")))
## stop("Input should only contain hits (\"H\") and misses (\"M\")")
## y <- rep(0, length(x))
## y[x == "H"] <- 1
## y <- c(0, y, 0)
## wz <- which(y == 0)
## streak <- diff(wz) - 1
## return(data.frame(length = streak))
## }
## <bytecode: 0x7fe8bd1323a0>
## <environment: namespace:openintro>
kobe_streak <- calc_streak(kobe_basket$shot)
ggplot(kobe_streak)

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? Make sure to include the accompanying plot in your answer.
Using the plot above, we can see that his most common streak was 0 hits. His longest streak was 4 hits in a row, however that did not happen too often. Of the streaks that he did have, it was typically just 1 hit.
kobe_streak <- calc_streak(kobe_basket) … coin_outcomes <- c(“heads”,“tails”) sample(coin_outcomes, size = 1, replace = TRUE) sim_fair_coin <- sample(coin_outcomes, size = 100, replace = TRUE)
sim_fair_coin table(sim_fair_coin) sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE, prob = c(0.2, 0.8))
Exercise 3
In your simulation of flipping the unfair coin 100 times, how many flips came up heads? Include the code for sampling the unfair coin in your response. Since the markdown file will run the code, and generate a new sample each time you Knit it, you should also “set a seed” before you sample. Read more about setting a seed below. table(sim_unfair_coin) set.seed(0710) Heads came up 19 times, tails came up 81 times, almost exactly what we expected (20 heads and 80 tails would have been the exact expectation, with a sample size larger than 100 we would probably see numbers closer to 20%/80%)
shot_outcomes <- c("0.45", "0.55")
sim_basket <- sample(shot_outcomes, size = 1, replace = TRUE)
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(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55)) table(sim_basket)
outcomes <- c("H", "M")
sim_basket <- sample(outcomes, size = 133,replace = TRUE,prob=c(0.45,0.55))
table(sim_basket)
## sim_basket
## H M
## 63 70
Exercise 5
## [1] "M" "H" "M" "M" "H" "H" "H" "H" "M" "M" "H" "H" "M" "H" "M" "M" "M" "M"
## [19] "H" "M" "H" "H" "M" "M" "H" "M" "M" "M" "H" "H" "M" "H" "M" "M" "H" "H"
## [37] "M" "M" "H" "M" "H" "M" "H" "H" "H" "H" "H" "H" "M" "H" "H" "M" "H" "H"
## [55] "M" "H" "M" "M" "H" "M" "M" "M" "M" "M" "H" "M" "H" "H" "M" "H" "H" "H"
## [73] "M" "M" "M" "M" "M" "M" "H" "H" "M" "M" "H" "M" "H" "H" "M" "H" "M" "M"
## [91] "M" "M" "H" "H" "M" "M" "H" "H" "H" "H" "M" "M" "M" "H" "H" "M" "M" "M"
## [109] "H" "H" "M" "H" "M" "H" "H" "H" "M" "H" "M" "M" "H" "H" "H" "H" "M" "H"
## [127] "M" "M" "M" "M" "M" "M" "M"
sim_streak <- calc_streak(sim_basket)
Exercise 6
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? Make sure to include a plot in your answer.
qplot(data = sim_streak, x = length, geom = “histogram”, binwidth = 1)
The longest streak is 7.5, the range goes from 0 to this 7.5. The most common streak is 0.
Exercise 7
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.
Both streak distributions should be similar since the amount of hits in a streak is not dependent on the past hit or miss that occured.
Exercise 8
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.
The simulated shooter and Kobe Bryant are pretty similar in that the primary streak length is 0, and larger streaks become significantly less common. I think it’s hard to say whether the hot hand model fits Kobe’s shooting pattern since the graphs look generally similar, mostly 0s for the streaks and skewed to the right (towards that 0 mark).
LS0tCnRpdGxlOiAiTGFiIDM6IFByb2JhYmlsaXR5IgphdXRob3I6ICJLaXJzdGVuIEdvbGRuZXIiCm91dHB1dDogb3BlbmludHJvOjpsYWJfcmVwb3J0Ci0tLQoKYGBge3IgbG9hZC1wYWNrYWdlcywgbWVzc2FnZT1GQUxTRX0KbGlicmFyeSh0aWR5dmVyc2UpCmxpYnJhcnkob3BlbmludHJvKQpgYGAKCmdsaW1wc2UgKGtvYmVfYmFza2V0KQpSb3dzOiAxMzMKQ29sdW1uczogNgokIHZzICAgICAgICAgIDxmY3Q+IE9STCwgT1JMLCBPUkwsIE9STCwgT1JMLCBPUkwsIE9STCwgT1JMLCBPUkwsIE9STCwgT1JMLCBPUkws4oCmCiQgZ2FtZSAgICAgICAgPGludD4gMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSwgMSzigKYKJCBxdWFydGVyICAgICA8ZmN0PiAxLCAxLCAxLCAxLCAxLCAxLCAxLCAxLCAxLCAyLCAyLCAyLCAyLCAyLCAyLCAyLCAyLCAyLCAzLCAzLOKApgokIHRpbWUgICAgICAgIDxmY3Q+IDk6NDcsIDk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