PS_7_2

Question 7

library(tidyverse)
Warning: package 'ggplot2' was built under R version 4.5.2
── 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   4.0.1     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── 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
sim_data <- parameters(~n, c(4, 8, 12, 16)) %>%
  add_trials(10000) %>%
  mutate(
    y_sample = pmap(list(n), function(n) sort(runif(n))),
    y_1 = map_dbl(y_sample, 1),
    y_3 = map_dbl(y_sample, 3),
    y_n = map_dbl(y_sample, tail, 1),
    f_1 = dbeta(y_1, 1, n),
    f_3 = dbeta(y_3, 3, n - 2),
    f_n = dbeta(y_n, n, 1)
  )

ggplot(sim_data, aes(x = y_1)) +
  geom_histogram(aes(y = after_stat(density)), fill = 'goldenrod', bins = 40) +
  geom_line(aes(y = f_1), col = 'cornflowerblue', size = 1) +
  facet_wrap(~n, scales = "free_y", labeller = label_both) +
  theme_classic() +
  labs(title = "Y(1) Order Statistic")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

ggplot(sim_data, aes(x = y_3)) +
  geom_histogram(aes(y = after_stat(density)), fill = 'goldenrod', bins = 40) +
  geom_line(aes(y = f_3), col = 'cornflowerblue', size = 1) +
  facet_wrap(~n, scales = "free_y", labeller = label_both) +
  theme_classic() +
  labs(title = "Y(3) Order Statistic")

ggplot(sim_data, aes(x = y_n)) +
  geom_histogram(aes(y = after_stat(density)), fill = 'goldenrod', bins = 40) +
  geom_line(aes(y = f_n), col = 'cornflowerblue', size = 1) +
  facet_wrap(~n, scales = "free_y", labeller = label_both) +
  theme_classic() +
  labs(title = "Y(n) Order Statistic")

Again larger n, leads to less variability.