library(tidyverse)
library(openintro)
glimpse(kobe_basket)  #glimpse function allows me to look at data set 
## 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"~
##shows a general idea of what the data looks like H is for hit meaning the shot went in and M is for miss

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?

A streak length of 1 means that Kobe made 1 shot then missed the next shot attempt. A streak length of 0 means that the shot attempt before was a miss and the shot after it was a miss again.

In this part of code streak is calculated by looking at the $shot column

kobe_streak <- calc_streak(kobe_basket$shot)
ggplot(data =kobe_streak, aes (x=length)) +geom_bar() + scale_y_continuous(breaks = c(0, 5, 10, 15, 20, 25, 30, 35, 40))

##Graph showing the distribution of streak lengths
##aes is for labeling the x axis 
##geom_bar() is to show the bars of the graph

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.

The distribution is skewed right where most of the streak length was 0. His typical streak length was 0. His longest streak of baskets was 4 shot in a row.

//Simulations in R of flipping a coin

coin_outcomes <- c("heads", "tails")
sample(coin_outcomes, size= 1, replace =TRUE)
## [1] "tails"
## simulates flipping a coin on R
sim_fair_coin <- sample(coin_outcomes, size = 100, replace = TRUE)
##this chunk of code simulates flipping a coin 100 times
sim_fair_coin
##   [1] "heads" "tails" "tails" "heads" "heads" "tails" "tails" "heads" "heads"
##  [10] "tails" "tails" "heads" "heads" "tails" "heads" "tails" "tails" "tails"
##  [19] "heads" "heads" "heads" "tails" "tails" "heads" "heads" "tails" "heads"
##  [28] "tails" "heads" "heads" "heads" "heads" "tails" "tails" "tails" "tails"
##  [37] "heads" "tails" "tails" "heads" "heads" "tails" "heads" "tails" "heads"
##  [46] "heads" "tails" "heads" "tails" "heads" "heads" "tails" "tails" "tails"
##  [55] "heads" "tails" "heads" "tails" "tails" "tails" "heads" "tails" "tails"
##  [64] "tails" "heads" "heads" "heads" "heads" "tails" "heads" "heads" "tails"
##  [73] "heads" "tails" "tails" "tails" "heads" "tails" "heads" "heads" "tails"
##  [82] "heads" "heads" "tails" "tails" "tails" "tails" "heads" "tails" "heads"
##  [91] "tails" "tails" "heads" "tails" "heads" "heads" "heads" "heads" "tails"
## [100] "tails"
table(sim_fair_coin)
## sim_fair_coin
## heads tails 
##    49    51
## this runs the code for flipping then shows it on the table
set.seed(1738)
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 
##    23    77
##in this coin flip there is favored 80% to the tail

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.

Only 23 heads came up in the unfair coin flip.

shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 1, replace = TRUE)

##Separates the hits and misses making it like heads or tails

Exercise 4

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.

prob = c(.45, .55) would need to be added to the line to reflect shooting percentage and size would equal 133

sim_basket<- sample(shot_outcomes, size = 133, replace = TRUE ,prob = c(.45, .55) )

Exercise 5

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)

Exercise 6

Describe the distribution of streak lengths. What is the typical streak length for this simulated independent shooter with a 45% shooting percentage? How long is the player’s longest streak of baskets in 133 shots? Make sure to include a plot in your answer.

The typical streak length is still 0 for the independent shooter and the longest streak of baskets is 6.

ggplot(data =sim_streak, aes (x=length)) +geom_bar()

Exercise 7

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.

There will be slight differences between the distributions but the general shape of the curve will basically be the same. The probabilities wouldn’t change so the distribution wouldn’t change that much either. The next run of code could have the highest streak be 4 but the general shape would still be the same.

Exercise 8

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.

The distribution streak length of the simulated shooter has a larger range on the x axis and a longer length of streak. The general shape of the two graphs are very similar with most of the length being at 0. Kobe’s shooting patterns don’t fit the hot hand model because the hot hand model says that the events which are the shots are going to be dependent of each and if you have one you should have a higher chance to make the next but in this case the data doesn’t show that.