8.1

  1. Unbiasedness: all the bias result are around -1 to 1. The tauhat2 all had negative numbers which shows that it is a better predictor
  2. Varaince: The variance for all n and predictors are all postive and under 10. Agian Tauhat2 have the lowest value number which again shows it is the better predictor when looking at the varaince.
  3. MSE: Comparing the values show that tauhat2 again has the lowest values which tells us that it is the best predictor.
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.496 8.427 10.664
3 tauhat2 -0.027 4.049 4.049
3 tauhat3 0.679 9.140 9.601
11 tauhat1 1.523 2.222 4.540
11 tauhat2 -0.008 1.067 1.068
11 tauhat3 0.198 2.315 2.354
51 tauhat1 1.535 0.492 2.849
51 tauhat2 0.001 0.236 0.236
51 tauhat3 0.048 0.495 0.497