library(tidyverse)
library(openintro)
Kobe’s performance in the 2009 NBA Finals (5 games)
data("kobe_basket")
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"…
#unique(kobe_basket$game)
head(kobe_basket$shot, 9)
## [1] "H" "M" "M" "H" "H" "M" "M" "M" "M"
kobe_streak <- calc_streak(kobe_basket$shot)
str(kobe_streak)
## 'data.frame': 76 obs. of 1 variable:
## $ length: num 1 0 2 0 0 0 3 2 0 3 ...
ggplot(kobe_streak) +
geom_bar(aes(length))

Fair coin simulations
set.seed(6)
coin_outcomes <- c("heads", "tails")
sim_fair_coin <- sample(coin_outcomes, size = 100, replace = TRUE)
table(sim_fair_coin)
## sim_fair_coin
## heads tails
## 46 54
Unfair coin simulations
set.seed(12)
coin_outcomes <- c("heads", "tails")
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
## 17 83
Simulating the independent shooter with P(shot1 = H) = 0.45: P(shot2
= H | shot1 = H) = 0.45
set.seed(1)
shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE,
prob = c(0.45, 0.55))
sim_streak <- calc_streak(sim_basket)
str(sim_streak)
## 'data.frame': 75 obs. of 1 variable:
## $ length: num 0 0 2 4 0 0 1 1 2 2 ...
ggplot(sim_streak) +
geom_bar(aes(length))
