7.7

Author

courtney casey

pop 1.

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.1     ✔ tibble    3.3.1
✔ lubridate 1.9.4     ✔ tidyr     1.3.2
✔ purrr     1.2.1     
── 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(purrrfect)

Attaching package: 'purrrfect'

The following objects are masked from 'package:base':

    replicate, tabulate
n_grid <- c(5, 10, 20, 40, 80, 160)
N <- 10000

mu <- 4
sigma <- sqrt(16/3)

sim_unif <- parameters(~n, n_grid) %>%
  add_trials(N) %>%
  mutate(
    ybar = map_dbl(n, \(nn) mean(runif(nn, 0, 8))),
    se = sigma / sqrt(n)
  )

ggplot(sim_unif, aes(x = ybar)) +
  geom_density() +
  stat_function(
    fun = dnorm,
    args = list(mean = mu, sd = sigma / sqrt(5)),
    linewidth = 0.8
  ) +
  facet_wrap(~n, scales = "free") +
  labs(title = "UNIF(0,8): Sampling distribution of Ȳ") +
  theme_classic()

ggplot(sim_unif, aes(x = ybar)) +
  stat_ecdf() +
  stat_function(fun = pnorm, args = list(mean = mu, sd = sigma / sqrt(5))) +
  facet_wrap(~n, scales = "free") +
  labs(title = "UNIF(0,8): ECDF vs Normal CDF") +
  theme_classic()

pop 2.

library(tidyverse)
library(purrrfect)

n_grid <- c(5, 10, 20, 40, 80, 160)
N <- 10000

mu <- 1
sigma <- sqrt(0.5)

sim_gamma <- parameters(~n, n_grid) %>%
  add_trials(N) %>%
  mutate(
    ybar = map_dbl(n, \(nn) mean(rgamma(nn, shape = 2, rate = 2))),
    se = sigma / sqrt(n)
  )
ggplot(sim_gamma, aes(x = ybar)) +
  geom_density() +
  stat_function(
    fun = dnorm,
    args = list(mean = mu, sd = sigma / sqrt(5)),
    linewidth = 0.8
  ) +
  facet_wrap(~n, scales = "free") +
  labs(title = "Gamma(2,2): Sampling distribution of Ȳ") +
  theme_classic()

ggplot(sim_gamma, aes(x = ybar)) +
  stat_ecdf() +
  stat_function(fun = pnorm, args = list(mean = mu, sd = sigma / sqrt(5))) +
  facet_wrap(~n, scales = "free") +
  labs(title = "Gamma(2,2): ECDF vs Normal CDF") +
  theme_classic()

pop 3.

library(tidyverse)
library(purrrfect)


n_grid <- c(5, 10, 20, 40, 80, 160)
N <- 10000

mu <- 4
sigma <- sqrt(4)

sim_poi <- parameters(~n, n_grid) %>%
  add_trials(N) %>%
  mutate(
    ybar = map_dbl(n, \(nn) mean(rpois(nn, lambda = 4))),
    se = sigma / sqrt(n)
  )

ggplot(sim_poi, aes(x = ybar)) +
  geom_density() +
  stat_function(
    fun = dnorm,
    args = list(mean = mu, sd = sigma / sqrt(5)),
    linewidth = 0.8
  ) +
  facet_wrap(~n, scales = "free") +
  labs(title = "Poisson(4): Sampling distribution of Ȳ") +
  theme_classic()

F
[1] FALSE
ggplot(sim_poi, aes(x = ybar)) +
  stat_ecdf() +
  stat_function(fun = pnorm, args = list(mean = mu, sd = sigma / sqrt(5))) +
  facet_wrap(~n, scales = "free") +
  labs(title = "Poisson(4): ECDF vs Normal CDF") +
  theme_classic()

the uniform distribution get’s “normal” fastest