7.3

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 '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   3.5.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
(exponential_range_sim <- parameters(~n, ~i, ~lambda,
                                     c(10,15,20), c(3,5,9), c(0.05,1.0)
             )
  %>% add_trials(10000)
  %>% mutate(y_sample = pmap(list(n,lambda), \(n,l) rexp(n, rate =l)))
  %>% mutate(y_sorted = map(y_sample, sort))
  %>% mutate( y_1 = pmap_dbl(list(i, y_sorted), \(i, y) pluck(y, i)))
  %>% mutate( y_n = pmap_dbl(list(i - 1, y_sorted), \(i, y) pluck(y,i)))
  %>% mutate(T = y_1 - y_n)
  %>% mutate( rate = lambda * (n - i + 1)) 
  %>% mutate(f_T = rate * exp(-rate * T))
  )
# A tibble: 180,000 × 11
       n     i lambda .trial y_sample   y_sorted   y_1    y_n     T  rate    f_T
   <dbl> <dbl>  <dbl>  <dbl> <list>     <list>   <dbl>  <dbl> <dbl> <dbl>  <dbl>
 1    10     3   0.05      1 <dbl [10]> <dbl>    14.1   4.96  9.10    0.4 0.0105
 2    10     3   0.05      2 <dbl [10]> <dbl>     3.03  1.08  1.95    0.4 0.183 
 3    10     3   0.05      3 <dbl [10]> <dbl>     6.26  5.94  0.321   0.4 0.352 
 4    10     3   0.05      4 <dbl [10]> <dbl>     6.47  0.819 5.65    0.4 0.0418
 5    10     3   0.05      5 <dbl [10]> <dbl>     9.15  3.08  6.07    0.4 0.0352
 6    10     3   0.05      6 <dbl [10]> <dbl>     4.68  3.82  0.863   0.4 0.283 
 7    10     3   0.05      7 <dbl [10]> <dbl>    18.7  13.7   5.04    0.4 0.0533
 8    10     3   0.05      8 <dbl [10]> <dbl>     8.47  7.27  1.20    0.4 0.248 
 9    10     3   0.05      9 <dbl [10]> <dbl>    11.6  10.1   1.43    0.4 0.226 
10    10     3   0.05     10 <dbl [10]> <dbl>    11.9   2.69  9.20    0.4 0.0101
# ℹ 179,990 more rows

You can add options to executable code like this

ggplot(exponential_range_sim, aes(x = T))+
  geom_histogram(
    aes(y = after_stat(density)),
    bins = 40, 
    fill = "goldenrod"
  )+
  geom_line(
    aes(y = f_T),
    color = "cornflowerblue",
    linewidth = 1,
    data = ~ arrange(.x,T)
  )+
  coord_cartesian(xlim = c(0,6))+
  facet_grid( i ~ n + lambda, labeller = label_both, scales = "free_y")+
  theme_classic(base_size = 15)+
  labs(
    title = " Simulated and Analytic Densities of Exponential Order Statistic",
    x = "T",
    y = "Density"
  )

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