Practice Set 6.2

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

Question 6

Set up the dataframe.

(sim1 <- data.frame(trials = 1:10000,y = rbeta(10000,1,2))
  %>%mutate(U = map_dbl(y,\(y) 1-2*y),V = map_dbl(y,\(y) 1/y),W = map_dbl(y,\(y) (y)^(1/2)))
) %>% head()
  trials         y          U         V         W
1      1 0.1229633  0.7540733  8.132505 0.3506613
2      2 0.3680882  0.2638237  2.716740 0.6067027
3      3 0.3591177  0.2817645  2.784602 0.5992643
4      4 0.0235536  0.9528928 42.456354 0.1534718
5      5 0.1837906  0.6324188  5.440975 0.4287081
6      6 0.7089459 -0.4178918  1.410545 0.8419893

Plot simulated versus analytic for \(U=1-2Y\).

(ggplot(aes(x=U),data=sim1)
+ geom_histogram(aes(y =after_stat(density)),
fill = 'red',
binwidth = 0.05, center = 0.05)
+ geom_line(aes(y = (1+U)/2), col = 'blue', linewidth=1)
+ labs(y='P(U)', x= 'U')
+ theme_classic(base_size = 12)
)

Plot the simulated versus analytic for \(V=\frac{1}{Y}\).

(ggplot(aes(x=V),data=sim1)
+ geom_histogram(aes(y =after_stat(density)),
fill = 'red',
binwidth = 0.05, center = 0.05)
+ geom_line(aes(y = (2*V-2)/(V)^(3)), col = 'blue', linewidth=1)
+ labs(y='P(V)', x= 'V')
+ theme_classic(base_size = 12)
+ scale_x_continuous(limits = c(1,20))
)
Warning: Removed 957 rows containing non-finite outside the scale range
(`stat_bin()`).
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_bar()`).
Warning: Removed 957 rows containing missing values or values outside the scale range
(`geom_line()`).

Plot simulated versus analytic \(W = \sqrt{Y}\).

(ggplot(aes(x=W),data=sim1)
+ geom_histogram(aes(y =after_stat(density)),
fill = 'red',
binwidth = 0.05, center = 0.05)
+ geom_line(aes(y = 4*W*(1-W^2)), col = 'blue', linewidth=1)
+ labs(y='P(W)', x= 'W')
+ theme_classic(base_size = 12)
)