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
(sim_results <- parameters (
~ n, ~ sigma, ~ mu,
c (4 , 8 , 15 ),
c (1 , 2 , 3 ),
0
) %>%
add_trials (10000 ) %>%
mutate (
ysample = pmap (list (n, mu, sigma), \(nn, m, s) rnorm (nn, m, s)),
s2 = map_dbl (ysample, var),
stat = (n - 1 ) * s2 / sigma^ 2
))
# A tibble: 90,000 × 7
n sigma mu .trial ysample s2 stat
<dbl> <dbl> <dbl> <dbl> <list> <dbl> <dbl>
1 4 1 0 1 <dbl [4]> 0.876 2.63
2 4 1 0 2 <dbl [4]> 1.85 5.56
3 4 1 0 3 <dbl [4]> 1.40 4.20
4 4 1 0 4 <dbl [4]> 3.53 10.6
5 4 1 0 5 <dbl [4]> 1.81 5.42
6 4 1 0 6 <dbl [4]> 0.209 0.626
7 4 1 0 7 <dbl [4]> 0.482 1.45
8 4 1 0 8 <dbl [4]> 0.439 1.32
9 4 1 0 9 <dbl [4]> 0.766 2.30
10 4 1 0 10 <dbl [4]> 0.883 2.65
# ℹ 89,990 more rows
# overlay
ggplot (sim_results, aes (x = stat)) +
geom_density (aes (y = after_stat (density)), fill = "blue" , alpha = 0.3 ) +
geom_line (aes (y = dchisq (stat, df = n - 1 )), color = "black" , linewidth = 1 ) +
facet_nested (sigma ~ n, labeller = label_both, scales = "free_y" ) +
theme_classic () +
labs (x = expression ((n-1 )* S^ 2 / sigma^ 2 ), y = "Density" )
# 4B verification
sim_results %>%
reframe (
mean_sim = mean (s2),
var_sim = var (s2),
mean_true = sigma^ 2 ,
var_true = 2 * sigma^ 4 / (n - 1 ),
.by = c (n, sigma)
)
# A tibble: 90,000 × 6
n sigma mean_sim var_sim mean_true var_true
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 4 1 1.00 0.677 1 0.667
2 4 1 1.00 0.677 1 0.667
3 4 1 1.00 0.677 1 0.667
4 4 1 1.00 0.677 1 0.667
5 4 1 1.00 0.677 1 0.667
6 4 1 1.00 0.677 1 0.667
7 4 1 1.00 0.677 1 0.667
8 4 1 1.00 0.677 1 0.667
9 4 1 1.00 0.677 1 0.667
10 4 1 1.00 0.677 1 0.667
# ℹ 89,990 more rows