Getting Started
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
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"…
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 of 1 means that Kobe made one shot and then missed the
following shot, meaning that there are 2 shots in a stream of 1.. A
streak of 0 means that there were consecutive missed shots, and the
streak starts after a non zero stream ends.
kobe_streak <- calc_streak(kobe_basket$shot)
ggplot(data = kobe_streak, aes(x = length)) +
geom_bar()

summary(kobe_streak)
## length
## Min. :0.0000
## 1st Qu.:0.0000
## Median :0.0000
## Mean :0.7632
## 3rd Qu.:1.0000
## Max. :4.0000
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.
The distribution on Kobe’s streaks are heavily skewed right, and his
typical streak length was 0, given the median of the data though the
average was 0.76. The longest streak was 4.
Simulations in R
coin_outcomes <- c("heads", "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" "tails" "tails" "tails" "heads" "heads" "heads" "tails" "heads"
## [10] "heads" "tails" "heads" "tails" "heads" "tails" "tails" "tails" "tails"
## [19] "heads" "heads" "tails" "heads" "heads" "heads" "heads" "tails" "heads"
## [28] "tails" "heads" "heads" "tails" "heads" "tails" "tails" "tails" "heads"
## [37] "tails" "tails" "tails" "tails" "tails" "heads" "tails" "tails" "heads"
## [46] "tails" "heads" "heads" "tails" "tails" "tails" "tails" "heads" "heads"
## [55] "tails" "heads" "tails" "tails" "heads" "heads" "heads" "heads" "tails"
## [64] "heads" "tails" "tails" "tails" "tails" "tails" "heads" "heads" "tails"
## [73] "heads" "heads" "heads" "tails" "heads" "tails" "tails" "heads" "heads"
## [82] "tails" "heads" "tails" "tails" "tails" "tails" "heads" "heads" "heads"
## [91] "heads" "heads" "tails" "tails" "tails" "heads" "tails" "heads" "heads"
## [100] "heads"
table(sim_fair_coin)
## sim_fair_coin
## heads tails
## 49 51
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE,
prob = c(0.2, 0.8))
sim_unfair_coin
## [1] "tails" "tails" "tails" "tails" "heads" "tails" "heads" "tails" "tails"
## [10] "tails" "tails" "heads" "heads" "tails" "tails" "heads" "tails" "tails"
## [19] "tails" "heads" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [28] "heads" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [37] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [46] "heads" "tails" "tails" "heads" "tails" "heads" "heads" "tails" "tails"
## [55] "tails" "tails" "heads" "tails" "tails" "tails" "tails" "tails" "heads"
## [64] "tails" "heads" "heads" "tails" "heads" "tails" "heads" "tails" "tails"
## [73] "tails" "tails" "tails" "tails" "heads" "heads" "tails" "tails" "heads"
## [82] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [91] "tails" "heads" "tails" "tails" "tails" "tails" "tails" "heads" "tails"
## [100] "tails"
table(sim_unfair_coin)
## sim_unfair_coin
## heads tails
## 22 78
set.seed(12345)
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.
The amount of heads with the unfair coin was 19