7.5

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

library(tidyverse)
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
library(purrrfect)

Attaching package: 'purrrfect'

The following objects are masked from 'package:base':

    replicate, tabulate
N <- 10000

(t_sim <- parameters(~n, ~mu, ~sigma,
                     c(4,8,15),
                     c(-1,0,2),
                     c(1,2,3))

%>% add_trials(N)
%>% mutate(ysample = pmap(list(n,mu,sigma),
                          \(nn,m,s) rnorm(nn, mean = m, sd = s)))
%>% mutate(ybar = map_dbl(ysample, mean))
%>% mutate(s = map_dbl(ysample, sd))
%>% mutate (T = (ybar - mu) / (s / sqrt(n)))
)
# A tibble: 270,000 × 8
       n    mu sigma .trial ysample     ybar     s        T
   <dbl> <dbl> <dbl>  <dbl> <list>     <dbl> <dbl>    <dbl>
 1     4    -1     1      1 <dbl [4]> -1.26  0.893 -0.588  
 2     4    -1     1      2 <dbl [4]> -0.998 0.629  0.00664
 3     4    -1     1      3 <dbl [4]> -1.62  1.37  -0.909  
 4     4    -1     1      4 <dbl [4]> -1.12  0.841 -0.289  
 5     4    -1     1      5 <dbl [4]> -1.21  1.93  -0.221  
 6     4    -1     1      6 <dbl [4]> -0.190 0.671  2.42   
 7     4    -1     1      7 <dbl [4]> -1.45  1.00  -0.891  
 8     4    -1     1      8 <dbl [4]> -1.20  1.38  -0.288  
 9     4    -1     1      9 <dbl [4]> -1.29  1.10  -0.517  
10     4    -1     1     10 <dbl [4]> -0.572 0.951  0.901  
# ℹ 269,990 more rows
  t_density <- t_sim %>%
  group_by(n, mu, sigma) %>%
  summarise(
    t_grid = list(seq(min(T), max(T), length.out = 300)),
    .groups = "drop"
  ) %>%
  unnest(t_grid) %>%
  mutate(
    f_t = dt(t_grid, df = n - 1)
  )

ggplot(t_sim, aes(x = T))+
  geom_histogram(aes(y = after_stat(density)),
                 fill = "goldenrod",
                 bins = 60,
                 color = "white")+
  geom_line(data = t_density, 
            aes(x = t_grid, y = f_t),
            color = "cornflowerblue",
            linewidth = 1)+
  facet_grid(n ~ mu + sigma, labeller= label_both, 
             scales = "free_y")+
  theme_classic(base_size = 16)+
  labs(
    title = expression("Simulated densities vs analytic of" * (bar(Y) - mu) / (S/sqrt(n))),
    x = expression((bar(Y)-mu)/(S/sqrt(n))),
    y = "density"
  )

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).