Warning: package 'tidyverse' was built under R version 4.4.3
Warning: package 'ggplot2' was built under R version 4.4.3
Warning: package 'tidyr' was built under R version 4.4.3
Warning: package 'readr' was built under R version 4.4.3
Warning: package 'purrr' was built under R version 4.4.3
Warning: package 'dplyr' was built under R version 4.4.3
Warning: package 'stringr' was built under R version 4.4.3
Warning: package 'forcats' was built under R version 4.4.3
Warning: package 'lubridate' was built under R version 4.4.3
── 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.2.1
✔ lubridate 1.9.4 ✔ tidyr 1.3.1
✔ purrr 1.0.4
── 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
N <- 10000
clt_Unif_sims <-( parameters( ~n, c(5,10,20,40,80,160))
%>% add_trials(N)
%>% mutate(Y_sample = map(n, ~ runif(.x,0,8)))
%>% mutate(Ybar = map_dbl(Y_sample, mean))
%>% mutate(fU = dnorm(Ybar, mean = 4, sd = sqrt((8-0)^2/12) / sqrt(n)),
FU = pnorm(Ybar, mean = 4, sd = sqrt((8-0)^2/12)/sqrt(n)),
Fhat = cume_dist(Ybar),
.by = n
)
)
names(clt_Unif_sims)
[1] "n" ".trial" "Y_sample" "Ybar" "fU" "FU" "Fhat"
ggplot(data = clt_Unif_sims, aes(x = Ybar))+
geom_histogram(aes(y = after_stat(density)),
binwidth = .2, fill = 'goldenrod')+
geom_line(aes(y = fU), col = 'cornflowerblue')+
facet_wrap(~n, labeller = label_both, scales = 'free_y')+
labs(x = expression(bar(Y)))+
theme_classic()
ggplot(data = clt_Unif_sims, aes(x = Ybar))+
geom_step(aes( y = FU, col = 'Analytic Normal CDF'))+
geom_step(aes(y = Fhat, col = 'Empirical CDF'))+
facet_wrap(~n, labeller = label_both, scales = 'free')+
labs(color = '',
x = expression(bar(Y)),
y = 'CDF')+
theme_classic()
You can add options to executable code like this
library(tidyverse)
library(purrrfect)
N <- 10000
clt_gamma_sims <-( parameters( ~n, c(5,10,20,40,80,160))
%>% add_trials(N)
%>% mutate(Y_sample = map(n, ~ rgamma(.x,shape = 2, scale = 2)))
%>% mutate(Ybar = map_dbl(Y_sample, mean))
%>% mutate(fU = dnorm(Ybar, mean = 4, sd = sqrt(2*2^2) / sqrt(n)),
FU = pnorm(Ybar, mean = 4, sd = sqrt(2*2^2/sqrt(n))),
Fhat = cume_dist(Ybar),
.by = n
)
)
ggplot(data = clt_gamma_sims, aes(x = Ybar))+
geom_histogram(aes(y = after_stat(density)),
binwidth = .2, fill = 'goldenrod')+
geom_line(aes(y = fU), col = 'cornflowerblue')+
facet_wrap(~n, labeller = label_both, scales = 'free_y')+
labs(x = expression(bar(Y)))+
theme_classic()
ggplot(data = clt_gamma_sims, aes(x = Ybar))+
geom_step(aes( y = FU, col = 'Analytic Normal CDF'))+
geom_step(aes(y = Fhat, col = 'Empirical CDF'))+
facet_wrap(~n, labeller = label_both, scales = 'free')+
labs(color = '',
x = expression(bar(Y)),
y = 'CDF')+
theme_classic()
library(tidyverse)
library(purrrfect)
N <- 10000
clt_exp_sims <-( parameters( ~n, c(5,10,20,40,80,160))
%>% add_trials(N)
%>% mutate(Y_sample = map(n, ~ rexp(.x,rate = 1/4)))
%>% mutate(Ybar = map_dbl(Y_sample, mean))
%>% mutate(fU = dnorm(Ybar, mean = 4, sd = 4 / sqrt(n)),
FU = pnorm(Ybar, mean = 4, sd = 4/sqrt(n)),
Fhat = cume_dist(Ybar),
.by = n
)
)
ggplot(data = clt_exp_sims, aes(x = Ybar))+
geom_histogram(aes(y = after_stat(density)),
binwidth = .2, fill = 'goldenrod')+
geom_line(aes(y = fU), col = 'cornflowerblue')+
facet_wrap(~n, labeller = label_both, scales = 'free_y')+
labs(x = expression(bar(Y)))+
theme_classic()
ggplot(data = clt_exp_sims, aes(x = Ybar))+
geom_step(aes( y = FU, col = 'Analytic Normal CDF'))+
geom_step(aes(y = Fhat, col = 'Empirical CDF'))+
facet_wrap(~n, labeller = label_both, scales = 'free')+
labs(color = '',
x = expression(bar(Y)),
y = 'CDF')+
theme_classic()
- Uniform normality kicks in the fastest for all 3 of the distributions.
- N = 80 is the n for which Y bar appears normal regardless of the population.