simulation

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

#| message: false
#| warning: false

library(ggplot2)
library(dplyr)

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
sample_sizes <- c(10, 100, 1000, 100000, 1000000)

for (n in sample_sizes) {
  
  sample_data <- sample(1:9, n, replace = TRUE)
  sample_mean <- mean(sample_data)
  
  p <- ggplot(data.frame(x = sample_data), aes(x = x)) +
    geom_histogram(bins = 9, fill = "#378ADD", color = "white") +
    geom_vline(xintercept = 5, color = "#D85A30", 
               linewidth = 1, linetype = "dashed") +
    scale_x_continuous(breaks = 1:9) +
    labs(
      title = paste("Sample size:", format(n, big.mark = ",")),
      subtitle = paste("Sample mean:", round(sample_mean, 3),
                       "| Difference from expected:", 
                       round(sample_mean - 5, 3)),
      x = "Outcome",
      y = "Count",
      caption = "Red dashed line = expected value (5)"
    ) +
    theme_minimal()
  
  print(p)
}

## Summary Table
#| echo: false

results <- data.frame(
  sample_size = sample_sizes
) %>%
  rowwise() %>%
  mutate(
    sample_mean = mean(sample(1:9, sample_size, replace = TRUE)),
    difference  = round(sample_mean - 5, 4)
  ) %>%
  ungroup()

knitr::kable(results, 
             col.names = c("Sample Size", "Sample Mean", "Difference from 5"),
             format.args = list(big.mark = ","))
Sample Size Sample Mean Difference from 5
1e+01 3.300000 -1.7000
1e+02 5.440000 0.4400
1e+03 4.938000 -0.0620
1e+05 4.999050 -0.0009
1e+06 4.999964 0.0000