Probability Lab

Author

Cedric

Probability Lab

Loading Libraries

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.4.4     ✔ 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

Loading the Data set

The data frame contains 133 observations and 6 variables, each indicating a shot taken by Kobe Bryant.

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"…

Exercise 1 :

kobe_streak <- calc_streak(kobe_basket$shot)
ggplot(data = kobe_streak, aes(x = length)) +
  geom_bar()

Exercise 2

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" "heads" "heads" "tails" "tails" "heads" "heads" "tails" "heads"
 [10] "tails" "heads" "heads" "heads" "tails" "tails" "heads" "heads" "tails"
 [19] "heads" "tails" "tails" "tails" "tails" "heads" "heads" "tails" "heads"
 [28] "tails" "heads" "heads" "heads" "heads" "heads" "heads" "tails" "tails"
 [37] "heads" "tails" "heads" "heads" "tails" "heads" "tails" "heads" "tails"
 [46] "tails" "heads" "heads" "tails" "tails" "tails" "tails" "heads" "heads"
 [55] "heads" "heads" "heads" "heads" "tails" "heads" "heads" "heads" "tails"
 [64] "heads" "heads" "heads" "heads" "heads" "tails" "tails" "heads" "heads"
 [73] "heads" "heads" "tails" "tails" "heads" "tails" "tails" "heads" "heads"
 [82] "heads" "tails" "heads" "tails" "heads" "tails" "heads" "tails" "heads"
 [91] "heads" "tails" "tails" "tails" "tails" "heads" "tails" "tails" "tails"
[100] "heads"
table(sim_fair_coin)
sim_fair_coin
heads tails 
   57    43 
sim_unfair_coin <- sample(coin_outcomes, size = 100, replace = TRUE, 
                          prob = c(0.2, 0.8))

Exercise 3

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

Exercise 4

# Adjusting the shooting percentage to 45%
prob_success <- 0.45

# Running a simulation to sample 133 shots
set.seed(123) # Setting seed for reproducibility
sim_basket <- rbinom(n = 133, size = 1, prob = prob_success)

# Output the simulation
sim_basket
  [1] 0 1 0 1 1 0 0 1 1 0 1 0 1 1 0 1 0 0 0 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 0 0 1
 [38] 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 0 1 1 0 1 0 0 0 1 0 1 1 1 0 1 1 1 0
 [75] 0 0 0 1 0 0 0 1 0 1 0 0 1 1 1 0 0 1 0 1 0 0 1 0 0 0 1 0 0 1 0 1 1 1 0 0 1
[112] 0 0 1 1 0 0 1 1 0 1 0 0 0 0 1 0 0 0 1 1 1 1