For each combination of n∈{4,8,15}, μ∈{-2,0,2}, and σ∈{1,2,3}, simulate 10,000 realizations of (Y ̅-μ)/(S/√n) . Verify that this has a t distribution with n-1 degrees of freedom by plotting the simulated densities superimposed by the analytic densities. Submit a link to your rendered solutions published on rpubs.
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.5.2 ✔ 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
N <-10000t_sample <-parameters( ~n, ~mu, ~sigma,c(4, 8, 15), c(-2, 0, 2), c(1, 2, 3)) %>%add_trials(N) %>%mutate( y_sample =pmap(list(n, mu, sigma), \(nn, mm, ss) rnorm(nn, mm, ss)) ) %>%mutate(Ybar =map_dbl(y_sample, mean), S =map_dbl(y_sample, sd), Tval = (Ybar - mu) / (S /sqrt(n))) %>%mutate(f_t =pmap_dbl(list(Tval, n), \(x, nn) dt(x, df = nn -1)) )
ggplot(data = t_sample, aes(x = Tval)) +geom_histogram(aes(y =after_stat(density)),fill ="goldenrod",binwidth= .1 ,alpha =0.6 ) +geom_line(aes(y = f_t), col ="cornflowerblue") +facet_grid( n ~ sigma ~ mu , labeller = label_both, scales ="free_y" ) +coord_cartesian(xlim =c(-10, 10)) +theme_classic(base_size =16) +labs(x =expression((bar(Y) - mu)/(S/sqrt(n))), y ="Density",title ="Simulated and Analytic Densities of the t Statistic" )