Before starting, a couple of packages need to be installed,
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ ggplot2 4.0.3 ✔ tibble 3.3.1
## ✔ lubridate 1.9.5 ✔ tidyr 1.3.2
## ✔ purrr 1.2.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
install.packages('tinytex')
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
tinytex::install_tinytex(force = TRUE)
## tlmgr option sys_bin ~/bin
## tlmgr option repository 'https://tlnet.yihui.org'
## tlmgr update --list
install.packages(c('openintro','devtools','tidyverse', 'ggplot2',
'psych','reshape2','knitr','markdown','shiny','R.rsp',
'fivethirtyeight'))
## Installing packages into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
install.packages('remotes')
## Installing package into '/cloud/lib/x86_64-pc-linux-gnu-library/4.6'
## (as 'lib' is unspecified)
remotes::install_github("jbryer/DATA606")
## Skipping install of 'DATA606' from a github remote, the SHA1 (919d6624) has not changed since last install.
## Use `force = TRUE` to force installation
remotes::install_github("jbryer/VisualStats")
## Skipping install of 'VisualStats' from a github remote, the SHA1 (81352eb1) has not changed since last install.
## Use `force = TRUE` to force installation
library(openintro)
library(tidyverse)
library(ggplot2)
library(DATA606)
## Loading required package: shiny
## Loading required package: markdown
##
## Welcome to CUNY DATA606 Statistics and Probability for Data Analytics
## This package is designed to support this course. The text book used
## is OpenIntro Statistics, 4th Edition. You can read this by typing
## vignette('os4') or visit www.OpenIntro.org.
##
## The getLabs() function will return a list of the labs available.
##
## The demo(package='DATA606') will list the demos that are available.
##
## Attaching package: 'DATA606'
##
## The following objects are masked from 'package:openintro':
##
## calc_streak, famuss, nhanes.samp.adult.500, present, qqnormsim
##
## The following object is masked from 'package:utils':
##
## demo
In the Lab instructions it says the length of a shooting streak is the number of consecutive hits made until a miss occurs. This means a miss ends a streak. Thus, a single miss is a streak, just as a two hits and a miss is a streak. Consequently, a sreak made of a single miss is said to be of length zero, as there are no hits. A streak made of a hit and a miss is of length one because there is only one hit. The miss that ends the streak is included in the streak. Thus, a hit followed by two misses results in a streak of length one (a hit and a miss) followed by a streak of length zero (a single miss).
The most common streak (almost 40 of them) have zero length. There were no hits. However, it has to be remembered that five consecutive misses are considered five different streaks (as explained above a miss is the end of a streak and belongs to the streak the miss ends or closes). Then there are almost 25 streaks of length one (a hit followed by a miss). These streaks are the majority, about two thirds, of the streaks that include hits. There are less than 10 streaks of length two and three. The longest streak is of length four. It happened only once.
kobe_streak <- calc_streak(kobe_basket$shot)
kobe_streak <- as.data.frame(kobe_streak)
kobe_streak <- kobe_streak |>
rename(length = kobe_streak)
ggplot(data = kobe_streak, aes(x = length))+
geom_bar()
As indicated in the instructions, the seed for random numbers is needed.
set.seed(11358)
The simulation of the unfair results in 27 Heads and 73 Tails
coin_outcomes <- c("heads", "tails")
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE,
prob = c(0.2, 0.8))
sim_unfair_coin
## [1] "heads" "tails" "heads" "heads" "tails" "heads" "tails" "tails" "tails"
## [10] "heads" "tails" "tails" "tails" "heads" "tails" "tails" "tails" "tails"
## [19] "heads" "tails" "tails" "heads" "heads" "heads" "tails" "heads" "tails"
## [28] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [37] "tails" "heads" "tails" "tails" "tails" "heads" "tails" "tails" "heads"
## [46] "heads" "tails" "tails" "heads" "tails" "tails" "tails" "tails" "tails"
## [55] "tails" "heads" "tails" "tails" "tails" "tails" "heads" "tails" "heads"
## [64] "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [73] "heads" "heads" "tails" "tails" "tails" "tails" "tails" "tails" "tails"
## [82] "tails" "tails" "tails" "heads" "heads" "tails" "tails" "heads" "tails"
## [91] "tails" "tails" "tails" "tails" "heads" "heads" "tails" "heads" "tails"
## [100] "tails"
table(sim_unfair_coin)
## sim_unfair_coin
## heads tails
## 27 73
###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.
The sim_basket object is generated for 133 shots, with slightly lower probability of hitting than missing (45 to 55 percent). Thus, in this simulation there are 53 hits against 80 misses.
shot_outcomes <- c("H", "M")
sim_basket <- sample(shot_outcomes, size = 133, replace = TRUE, prob = c(0.45, 0.55))
sim_basket
## [1] "H" "M" "H" "M" "H" "M" "M" "H" "H" "M" "M" "M" "H" "M" "H" "M" "M" "H"
## [19] "M" "H" "H" "M" "M" "H" "H" "M" "M" "M" "H" "M" "H" "H" "M" "M" "H" "M"
## [37] "H" "H" "M" "M" "H" "H" "H" "H" "H" "M" "H" "M" "H" "H" "H" "M" "H" "H"
## [55] "H" "M" "M" "M" "M" "M" "H" "H" "M" "M" "M" "H" "H" "H" "M" "M" "M" "H"
## [73] "H" "M" "M" "H" "H" "M" "H" "M" "H" "M" "H" "M" "M" "H" "M" "M" "H" "M"
## [91] "M" "M" "M" "M" "M" "M" "M" "M" "H" "H" "M" "M" "H" "M" "H" "M" "H" "M"
## [109] "H" "H" "M" "M" "M" "M" "M" "M" "M" "H" "M" "M" "H" "H" "H" "M" "M" "M"
## [127] "M" "M" "M" "M" "M" "H" "H"
table(sim_basket)
## sim_basket
## H M
## 57 76
sim_streak <- calc_streak(sim_basket)
sim_streak <- as.data.frame(sim_streak)
sim_streak
## sim_streak
## 1 1
## 2 1
## 3 1
## 4 0
## 5 2
## 6 0
## 7 0
## 8 1
## 9 1
## 10 0
## 11 1
## 12 2
## 13 0
## 14 2
## 15 0
## 16 0
## 17 1
## 18 2
## 19 0
## 20 1
## 21 2
## 22 0
## 23 5
## 24 1
## 25 3
## 26 3
## 27 0
## 28 0
## 29 0
## 30 0
## 31 2
## 32 0
## 33 0
## 34 3
## 35 0
## 36 0
## 37 2
## 38 0
## 39 2
## 40 1
## 41 1
## 42 1
## 43 0
## 44 1
## 45 0
## 46 1
## 47 0
## 48 0
## 49 0
## 50 0
## 51 0
## 52 0
## 53 0
## 54 0
## 55 2
## 56 0
## 57 1
## 58 1
## 59 1
## 60 2
## 61 0
## 62 0
## 63 0
## 64 0
## 65 0
## 66 0
## 67 1
## 68 0
## 69 3
## 70 0
## 71 0
## 72 0
## 73 0
## 74 0
## 75 0
## 76 0
## 77 2
ggplot(data = sim_streak, aes(x = length)) + geom_bar()
## Don't know how to automatically pick scale for object of type <function>.
## Defaulting to continuous.
## Error in `geom_bar()`:
## ! Problem while computing aesthetics.
## ℹ Error occurred in the 1st layer.
## Caused by error:
## ! Aesthetics are not valid data columns.
## ✖ The following aesthetics are invalid:
## • `x = length`
## ℹ Did you mistype the name of a data column or forget to add `after_stat()`?
The most common streak (about 40 of them) have zero length. There were no hits. Then there are about 20 streaks of length one. These streaks are the majority, about 60% of the streaks that include hits. There are less than 10 streaks of length two. There are 3 streaks of length three and another 3 of length four. The longest streak is of length five. It happened only once.
Given we are dealing with a random distribution of independent events, a second simulation would not be exactly the same (even if theoretically it could be possible, it is extremely unlikely).
Also, theoretically, it could be very different. Again this is unlikely.
Thus I would expect a second simulation to be similar. In particular I would expect it to roughly reproduce what was built in the different probabilities: to be close to the ratio of misses and hits experienced by Kobe in the 133 shots in the first game of the 2009 NAB finals.
###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 distributions look very similar to me. In other words, the professional basketball player (Kobe) and the random simulation of independent events (adjusting the observed ratio of misses to hits in the 133 shot by Kobe) look similar. Thus, there seems to be no evidence to support the hot hand model.