k_red <- 3 # Number of red jellybeans
k_green <- 2 # Number of green jellybeans
N_red <- 70 # Total red jellybeans
N_green <- 30 # Total green jellybeans
n_draw <- 5 # Number of jellybeans drawn
exact_prob <- dhyper(k_red, N_red, N_green, n_draw)
# 4 Simulate the situation:
n_simulations <- 10000
jellybean_colors <- c(rep("red", 70), rep("green", 30))
simulated_draws <- replicate(n_simulations, sample(jellybean_colors, n_draw))
################START CODE HERE################
num_red_in_draws <- ??? # hint: use apply(), function(draw)
#################END CODE HERE#################
# 5 Estimate the probability from the simulation:
estimated_prob <- sum(num_red_in_draws == k_red) / n_simulations
# 6 Compare the exact and estimated probabilities:
# - Print out the exact and estimated probabilities.
# - Discuss the differences, if any.
comparison_table <- data.frame(
Probabilities = c("Exact", "Estimated"),
Values = c(exact_prob, estimated_prob)
)
print(comparison_table)
# 7: Create a histogram of the simulated data:
hist(num_red_in_draws, main="Simulated Jellybean Draws", xlab="Number of Red Jellybeans", col="purple", border="black", breaks=seq(min(num_red_in_draws), max(num_red_in_draws), 1))