8.1

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
library(knitr)
Warning: package 'knitr' was built under R version 4.4.3
theta <- 5
true_median <- theta * log(2)

(parameters(~n,
            c(3,11,51)
)
  %>% add_trials(10000)
  %>% mutate(sample = map(n, ~rexp(.x, rate = 1/theta)))
  %>% mutate(ybar = map_dbl(sample, mean))
  %>% mutate(ymed = map_dbl(sample, median))
  %>% mutate(tauhat1 = ybar)
  %>% mutate(tauhat2 = log(2)*ybar)
  %>% mutate(tauhat3 = ymed)
  ) -> sim_data

summary_table <- 
sim_data %>%
    pivot_longer(tauhat1:tauhat3,
                 names_to = "estimator",
                 values_to = "estimate")%>%
  group_by(n, estimator) %>%
  summarize(
    bias = mean(estimate - true_median),
    variance = var(estimate),
    mse = bias^2 + variance,
    .groups="drop"
  )%>%
  mutate(across(c(bias, variance, mse), round, 3))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `across(c(bias, variance, mse), round, 3)`.
Caused by warning:
! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
Supply arguments directly to `.fns` through an anonymous function instead.

  # Previously
  across(a:b, mean, na.rm = TRUE)

  # Now
  across(a:b, \(x) mean(x, na.rm = TRUE))
kable(summary_table)
n estimator bias variance mse
3 tauhat1 1.537 8.561 10.923
3 tauhat2 0.002 4.113 4.113
3 tauhat3 0.735 9.521 10.062
11 tauhat1 1.531 2.315 4.658
11 tauhat2 -0.002 1.112 1.112
11 tauhat3 0.233 2.415 2.469
51 tauhat1 1.538 0.497 2.864
51 tauhat2 0.003 0.239 0.239
51 tauhat3 0.059 0.499 0.502

ou can add options to executable code like this

[1] 4

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