Practice_set_10.3

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.3.3
Warning: package 'tibble' was built under R version 4.3.3
Warning: package 'tidyr' was built under R version 4.3.3
Warning: package 'readr' was built under R version 4.3.3
Warning: package 'purrr' was built under R version 4.3.3
Warning: package 'dplyr' was built under R version 4.3.3
Warning: package 'stringr' was built under R version 4.3.3
Warning: package 'forcats' was built under R version 4.3.3
Warning: package 'lubridate' was built under R version 4.3.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.2     
── 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

Practice Set 10.3

Problem 8

p8df <- (parameters(~s,~n,seq(1,10,l = 100),c(5,10,15,20))
         %>%mutate(ct1 = qchisq(.95,n),
                   ct2 = qnorm(.95,0,1/n),
                   Rejects1 = 1 - pchisq((ct1/s),n),
                   Rejects2 = 1 - pnorm(ct2,0,(s)/n)
         )
)
ggplot(data=p8df) +
  geom_line(aes(x=s,y=Rejects1,col = "Test 1")) +
    geom_line(aes(x=s,y=Rejects2,col = "Test 2")) +
  facet_wrap(~n,labeller = label_both) +
  geom_hline(yintercept = .05,linetype = 2) +
  theme_classic() +
  labs(x = expression(sigma^2),y = "Reject Null")

Can see that Test 1 is more powerful than Test 2 for all \(\sigma^2 = [1,10]\) , which reaffirms the analytic results.

Problem 9

p9df <- (parameters(~n,c(5,10,15,20))
         %>%add_trials(10000)
         %>%mutate(Ys = map(n,\(n) rbeta(n,.2,1)),T1 = pmap_dbl(list(Ys,n),\(y,n) sum(log(y))/n),T2 = map_dbl(Ys,mean))
         %>%summarize("T1R" = quantile(T1,.95),
                      "T2R" = quantile(T2,.95),
                      .by = n)
)
p9df2 <-(parameters(~n,~theta,c(5,10,15,20),seq(.2,1,l=20))
         %>%add_trials(10000)
         %>%mutate(Ys = pmap(list(n,theta),\(n,t) rbeta(n,t,1)),T1 = pmap_dbl(list(Ys,n),\(y,n) sum(log(y))/n),T2 = map_dbl(Ys,mean))
         %>%inner_join(.,p9df,by = "n")
         %>%mutate(Reject1 = if_else(T1>T1R,1,0),
                   Reject2 = if_else(T2>T2R,1,0))
         %>%summarize("Rejectp1" = mean(Reject1),
                      "Rejectp2" = mean(Reject2),
                      .by = c(n,theta))
)
ggplot(p9df2) +
  geom_line(aes(x=theta,y=Rejectp1,col = "Test 1")) +
  geom_line(aes(x=theta,y=Rejectp2,col = "Test 2")) +
  facet_wrap(~n,labeller = "label_both") +
  theme_classic() +
  labs(x = expression(theta),y = "Null Rejection %",col = "")