suppressWarnings(
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
Simulation
set.seed(42)
coin <- c('H', 'T')
heads_counts <- numeric(1000) # Initialize a vector to store the number of heads in each experiment
for (i in 1:1000) {
toss <- sample(coin, size = 100, replace = TRUE)
heads_counts[i] <- sum(toss == 'H') # Count the number of heads and store it
}
head_counts_df <- data.frame(toss = 1:1000, Heads_Count = heads_counts)
head(head_counts_df, 10)
## toss Heads_Count
## 1 1 44
## 2 2 45
## 3 3 55
## 4 4 52
## 5 5 45
## 6 6 55
## 7 7 49
## 8 8 49
## 9 9 50
## 10 10 55
head_count_35_to_65 <- head_counts_df %>%
filter(Heads_Count >= 35 & Heads_Count <= 65) %>%
group_by(Heads_Count) %>%
mutate(Frequency = n()) %>%
distinct(Heads_Count, Frequency)
head(head_count_35_to_65, 10)
## # A tibble: 10 × 2
## # Groups: Heads_Count [10]
## Heads_Count Frequency
## <dbl> <int>
## 1 44 39
## 2 45 43
## 3 55 56
## 4 52 76
## 5 49 79
## 6 50 96
## 7 46 49
## 8 54 57
## 9 57 32
## 10 42 20
Plot
head_count_35_to_65 %>%
ggplot(aes(x = Heads_Count, y = Frequency)) +
geom_bar(stat = 'identity') +
labs(title = "Head Count Frequency (35 - 65)",
x = "Head Count",
y = "Frequency")
Conclusion: The bar graph looks like it can be fit with a normal distribution curve with a mean at 50.