Warning: package 'tidyverse' was built under R version 4.5.2
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
Attaching package: 'purrrfect'
The following objects are masked from 'package:base':
replicate, tabulate
Warning: package 'ggh4x' was built under R version 4.5.2
head(sim <- parameters(
~n, ~mu, ~sigma,
c(4, 8, 15), c(-2, 0, 2), c(1, 2, 3)
) %>%
add_trials(10000) %>%
mutate(
samples = pmap(list(n, mu, sigma), rnorm),
x_bar = map_dbl(samples, mean),
s = map_dbl(samples, sd),
t_stat = (x_bar - mu) / (s / sqrt(n)),
dt_val = dt(t_stat, df = n - 1)
)
)
# A tibble: 6 × 9
n mu sigma .trial samples x_bar s t_stat dt_val
<dbl> <dbl> <dbl> <dbl> <list> <dbl> <dbl> <dbl> <dbl>
1 4 -2 1 1 <dbl [4]> -1.85 0.487 0.631 0.287
2 4 -2 1 2 <dbl [4]> -1.28 0.683 2.11 0.0597
3 4 -2 1 3 <dbl [4]> -2.71 1.09 -1.31 0.148
4 4 -2 1 4 <dbl [4]> -1.62 0.876 0.870 0.234
5 4 -2 1 5 <dbl [4]> -2.08 0.355 -0.435 0.325
6 4 -2 1 6 <dbl [4]> -2.11 1.75 -0.121 0.364
ggplot(sim, aes(x = t_stat)) +
geom_density(aes(color = "Simulated"), linewidth = 0.8) +
geom_line(aes(y = dt_val, color = "Analytic"), linewidth = 0.5) +
facet_nested(n ~ mu + sigma, labeller = label_both) +
scale_color_manual(values = c("Simulated" = "black", "Analytic" = "red")) +
coord_cartesian(xlim = c(-5, 5)) +
theme_minimal() +
labs(
x = "t-stat",
y = "Density",
color = "Legend"
)
# Simulated densities do follow the analytic t dist with n-1 df.