library(tidyverse)
library(openintro)kobe_basket## # A tibble: 133 × 6
## vs game quarter time description shot
## <fct> <int> <fct> <fct> <fct> <chr>
## 1 ORL 1 1 9:47 Kobe Bryant makes 4-foot two point shot H
## 2 ORL 1 1 9:07 Kobe Bryant misses jumper M
## 3 ORL 1 1 8:11 Kobe Bryant misses 7-foot jumper M
## 4 ORL 1 1 7:41 Kobe Bryant makes 16-foot jumper (Derek Fish… H
## 5 ORL 1 1 7:03 Kobe Bryant makes driving layup H
## 6 ORL 1 1 6:01 Kobe Bryant misses jumper M
## 7 ORL 1 1 4:07 Kobe Bryant misses 12-foot jumper M
## 8 ORL 1 1 0:52 Kobe Bryant misses 19-foot jumper M
## 9 ORL 1 1 0:00 Kobe Bryant misses layup M
## 10 ORL 1 2 6:35 Kobe Bryant makes jumper H
## # … with 123 more rows
glimpse(kobe_basket)## Rows: 133
## Columns: 6
## $ vs <fct> ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL…
## $ game <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1…
## $ quarter <fct> 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3…
## $ time <fct> 9:47, 9:07, 8:11, 7:41, 7:03, 6:01, 4:07, 0:52, 0:00, 6:35…
## $ description <fct> Kobe Bryant makes 4-foot two point shot, Kobe Bryant misse…
## $ shot <chr> "H", "M", "M", "H", "H", "M", "M", "M", "M", "H", "H", "H"…
A streak is defined as the number of consecutive baskets until a streak occurs. So, in a streak of 1 there is one basket before he misses. If the streak is 0, he only missed. If the streak is 2, he made 2 baskets before he misses. Each streak, regardless of length, has only 1 miss.
kobe_streak <- calc_streak(kobe_basket$shot)ggplot(data = kobe_streak, aes(x = length)) +
geom_bar()Based on the plot above, the majority of Kobe’s streaks were of zero baskets- this is his “typical” streak length. The next longest was a streak of 1. As the number of baskets increases, the frequency/count of the streaks decreases. This is a right-skewed distribution, as the majority of the data is on the left and the tail goes toward the right. His longest streak of baskets was 4, though that only happened once, maybe twice.
coin_outcomes <- c("heads", "tails")
sample(coin_outcomes, size = 1, replace = TRUE)## [1] "heads"
sample(coin_outcomes, size = 1, replace = TRUE)## [1] "heads"
sample(coin_outcomes, size = 1, replace = TRUE)## [1] "tails"
sample(coin_outcomes, size = 1, replace = TRUE)## [1] "tails"
sample(coin_outcomes, size = 1, replace = TRUE)## [1] "heads"
sim_fair_coin <- sample(coin_outcomes, size = 100, replace = TRUE)sim_fair_coin## [1] "heads" "heads" "heads" "tails" "heads" "heads" "tails" "tails" "tails"
## [10] "tails" "tails" "heads" "heads" "heads" "heads" "tails" "heads" "heads"
## [19] "tails" "tails" "heads" "heads" "tails" "heads" "tails" "heads" "heads"
## [28] "heads" "heads" "heads" "heads" "heads" "heads" "tails" "heads" "tails"
## [37] "tails" "tails" "heads" "tails" "heads" "heads" "heads" "heads" "heads"
## [46] "heads" "tails" "tails" "heads" "tails" "tails" "heads" "tails" "heads"
## [55] "heads" "tails" "tails" "tails" "tails" "tails" "tails" "heads" "heads"
## [64] "heads" "heads" "tails" "tails" "tails" "tails" "heads" "tails" "heads"
## [73] "tails" "heads" "tails" "tails" "tails" "heads" "tails" "tails" "tails"
## [82] "tails" "heads" "heads" "heads" "tails" "heads" "tails" "heads" "heads"
## [91] "tails" "tails" "heads" "tails" "tails" "heads" "heads" "heads" "heads"
## [100] "heads"
table(sim_fair_coin)## sim_fair_coin
## heads tails
## 54 46
set.seed(1116)
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE,
prob = c(0.2, 0.8))
table(sim_unfair_coin)## sim_unfair_coin
## heads tails
## 18 82
When the unfair coin was flipped 100 times using code sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE, prob = c(0.2, 0.8)), and table(sim_unfair_coin), heads came up 18 times.
shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 1, replace = TRUE)I changed the size outcome to 133, and replace 0.2 and 0.8 with 0.45 and 0.55, respectively.
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))
table (sim_basket)## sim_basket
## H M
## 61 72