This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3 ✓ purrr 0.3.4
## ✓ tibble 3.0.6 ✓ dplyr 1.0.4
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(openintro)
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
str(kobe_basket)
## tibble [133 × 6] (S3: tbl_df/tbl/data.frame)
## $ vs : Factor w/ 1 level "ORL": 1 1 1 1 1 1 1 1 1 1 ...
## $ game : int [1:133] 1 1 1 1 1 1 1 1 1 1 ...
## $ quarter : Factor w/ 5 levels "1","1OT","2",..: 1 1 1 1 1 1 1 1 1 3 ...
## $ time : Factor w/ 116 levels "00:00.0","00:00.5",..: 114 109 102 100 96 85 64 21 11 91 ...
## $ description: Factor w/ 80 levels "Bryant 3pt Shot: Made (16 PTS) Assist: Bynum (1 AST) ",..: 40 78 75 27 44 78 52 62 79 45 ...
## $ shot : chr [1:133] "H" "M" "M" "H" ...
glimpse(kobe_basket)
## Rows: 133
## Columns: 6
## $ vs <fct> ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, ORL, OR…
## $ game <int> 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, …
## $ time <fct> 9:47, 9:07, 8:11, 7:41, 7:03, 6:01, 4:07, 0:52, 0:00, 6:3…
## $ description <fct> Kobe Bryant makes 4-foot two point shot, Kobe Bryant miss…
## $ shot <chr> "H", "M", "M", "H", "H", "M", "M", "M", "M", "H", "H", "H…
kobe_streak <- calc_streak(kobe_basket$shot)
ggplot(data = kobe_streak, aes(x = length)) +
geom_bar()
#The streak length of 1 means he got one basket and missed the next one. The streak length of 0, means he missed two baskets on a row. #Describe the distribution of Kobe’s streak lengths from the 2009 NBA finals. What was his typical streak length? Kobe’s distribution is skewed to the right. Kobe’s typical strek was 0 or 1.
#How long was his longest streak of baskets? Kobe’s longest strek was 4.
coin_outcomes <- c("heads", "tails")
sample(coin_outcomes, size = 1, replace = TRUE)
## [1] "tails"
sim_fair_coin <- sample(coin_outcomes, size = 100, replace = TRUE)
sim_fair_coin
## [1] "tails" "tails" "tails" "heads" "tails" "tails" "heads" "tails" "heads"
## [10] "heads" "heads" "tails" "tails" "tails" "tails" "heads" "heads" "heads"
## [19] "tails" "tails" "heads" "heads" "tails" "tails" "tails" "heads" "tails"
## [28] "tails" "tails" "heads" "heads" "heads" "tails" "heads" "tails" "tails"
## [37] "tails" "heads" "tails" "tails" "heads" "tails" "tails" "tails" "heads"
## [46] "heads" "heads" "tails" "tails" "tails" "tails" "tails" "heads" "tails"
## [55] "tails" "tails" "heads" "heads" "heads" "heads" "tails" "heads" "heads"
## [64] "heads" "heads" "tails" "tails" "heads" "tails" "tails" "heads" "tails"
## [73] "tails" "heads" "tails" "heads" "heads" "heads" "tails" "tails" "tails"
## [82] "heads" "tails" "tails" "tails" "heads" "tails" "heads" "tails" "heads"
## [91] "heads" "tails" "heads" "heads" "heads" "tails" "heads" "heads" "heads"
## [100] "heads"
table(sim_fair_coin)
## sim_fair_coin
## heads tails
## 47 53
set.seed(12345)
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE, prob = c(0.2, 0.8))
sim_unfair_coin
## [1] "tails" "heads" "tails" "heads" "tails" "tails" "tails" "tails" "tails"
## [10] "heads" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [19] "tails" "heads" "tails" "tails" "heads" "tails" "tails" "tails" "tails"
## [28] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [37] "heads" "heads" "tails" "tails" "tails" "tails" "heads" "tails" "tails"
## [46] "tails" "tails" "tails" "tails" "tails" "heads" "heads" "tails" "tails"
## [55] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "heads"
## [64] "tails" "heads" "tails" "heads" "tails" "tails" "heads" "tails" "tails"
## [73] "tails" "tails" "tails" "tails" "heads" "tails" "tails" "tails" "heads"
## [82] "tails" "tails" "tails" "tails" "tails" "heads" "tails" "heads" "tails"
## [91] "heads" "heads" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [100] "tails"
table(sim_unfair_coin)
## sim_unfair_coin
## heads tails
## 20 80
#In your simulation of flipping the unfair coin 100 times, how many flips came up heads? In the simulation, I got 20 heads and 80 tails.
?sample
shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 1, replace = TRUE)
sim_basket
## [1] "H"
#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.
set.seed(12346)
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))
sim_basket
## [1] "H" "H" "M" "M" "H" "H" "M" "H" "M" "H" "H" "H" "M" "M" "M" "M" "H" "M"
## [19] "H" "M" "M" "H" "H" "M" "M" "M" "M" "M" "H" "M" "M" "H" "M" "M" "H" "M"
## [37] "M" "M" "H" "H" "M" "M" "M" "H" "M" "M" "M" "M" "M" "M" "M" "M" "H" "M"
## [55] "H" "M" "H" "M" "H" "M" "H" "M" "H" "M" "M" "H" "M" "H" "M" "H" "H" "M"
## [73] "H" "H" "M" "M" "H" "H" "M" "H" "M" "M" "M" "H" "M" "H" "M" "H" "H" "M"
## [91] "M" "H" "H" "H" "M" "H" "H" "H" "M" "M" "H" "M" "M" "H" "H" "M" "H" "M"
## [109] "M" "H" "M" "H" "H" "M" "H" "H" "M" "M" "M" "H" "M" "H" "M" "M" "M" "M"
## [127] "H" "H" "M" "M" "H" "M" "M"
table(sim_basket)
## sim_basket
## H M
## 57 76
#Using calc_streak, compute the streak lengths of sim_basket, and save the results in a data frame called sim_streak.
sim_streak <- calc_streak(sim_basket)
#Describe the distribution of streak lengths. #The distribution is skewed to the right.
#What is the typical streak length for this simulated independent shooter with a 45% shooting percentage? #The typical streak lenght is between 0 to 1.
#How long is the player’s longest streak of baskets in 133 shots? Make sure to include a plot in your answer. #The longest streak is 3.
ggplot(data = sim_streak, aes(x = length)) +
geom_bar()
#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. #I would expect a similar distribution because the probability still at 45%. The numbers of hits and missing wouldn’t deviate that much.
#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. #Both distributions look similar although Kobe’s longest streak was 4. I don’t believe Kobe’s streak pattern fit the hot hand model because both distributions look similar.