library(tidyverse)
library(openintro)Probability R-Lab
Import Tidyverse and Open Intro packages
Load the data into environment
data("kobe_basket")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?
# Answer
# A streak length of one means that there was one shot made and one successive miss, a streak of zero means there was just a miss
# Example: streak of one -- "H", "M"
# Example: streak of zero - "M"Importing chunk from Open Intro
# Define variable : kobe_streak
kobe_streak <- calc_streak(kobe_basket$shot)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.
# Answer :
# The longest streak in this dataset is a 4 basket streak, the plot of streaks is right skewed
# Create a histogram / barchart showing the frequencies of streak lengths
ggplot(data = kobe_streak, aes(x = length)) +
geom_bar()Simulating a coin flip in R
coin_outcomes <- c("heads", "tails")
sample(coin_outcomes, size = 1, replace = TRUE)[1] "heads"
Simulating 100 coin flips
sim_fair_coin <- sample(coin_outcomes, size = 100, replace = TRUE)View the results of the fair coin simulation
sim_fair_coin [1] "heads" "tails" "heads" "tails" "tails" "heads" "tails" "tails" "heads"
[10] "tails" "heads" "heads" "heads" "tails" "tails" "tails" "heads" "heads"
[19] "heads" "tails" "heads" "heads" "heads" "tails" "heads" "tails" "tails"
[28] "tails" "heads" "heads" "heads" "tails" "tails" "tails" "heads" "tails"
[37] "heads" "tails" "heads" "tails" "tails" "heads" "heads" "heads" "heads"
[46] "heads" "tails" "heads" "tails" "heads" "heads" "tails" "tails" "tails"
[55] "tails" "tails" "heads" "tails" "tails" "heads" "heads" "heads" "tails"
[64] "heads" "tails" "tails" "heads" "heads" "heads" "tails" "tails" "tails"
[73] "heads" "tails" "tails" "tails" "heads" "tails" "heads" "heads" "heads"
[82] "tails" "tails" "heads" "heads" "tails" "tails" "tails" "heads" "tails"
[91] "tails" "heads" "tails" "heads" "heads" "tails" "heads" "tails" "tails"
[100] "tails"
table(sim_fair_coin)sim_fair_coin
heads tails
48 52
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.
Setting the seed
set.seed(35797) # make sure to change the seed, I just used the same seed that is in openintroSimulate unfair coin, where P(“heads”) = 0.2 and P(“tails) = 0.8 ((ANSWER TO EXERCISE 3 IN HERE))
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE,
prob = c(0.2, 0.8))
sim_unfair_coin [1] "tails" "tails" "tails" "tails" "tails" "heads" "tails" "tails" "tails"
[10] "tails" "tails" "heads" "heads" "tails" "heads" "heads" "heads" "tails"
[19] "tails" "tails" "tails" "tails" "heads" "tails" "tails" "tails" "tails"
[28] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "heads"
[37] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "heads"
[46] "heads" "tails" "tails" "tails" "heads" "heads" "tails" "tails" "heads"
[55] "heads" "tails" "tails" "tails" "tails" "tails" "tails" "heads" "tails"
[64] "heads" "tails" "tails" "heads" "tails" "tails" "tails" "tails" "heads"
[73] "heads" "tails" "tails" "tails" "tails" "tails" "heads" "heads" "tails"
[82] "heads" "tails" "tails" "heads" "tails" "tails" "tails" "tails" "tails"
[91] "tails" "heads" "heads" "tails" "tails" "tails" "heads" "tails" "tails"
[100] "tails"
table(sim_unfair_coin)sim_unfair_coin
heads tails
26 74
## ANSWER is heads occurred 26 times and tails occurred 74 times