6.2

6

library(tidyverse)
── 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.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── 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

Sim study

sim_df <- data.frame(Y = rbeta(10000, 2, 1)) %>%
  mutate(U = 1 - 2*Y) %>%
  mutate(V = 1 / Y) %>%
  mutate(W = sqrt(Y)) %>%
  mutate(fY = dbeta(Y, 2, 1)) %>%
  mutate(fU = (1 + U)/2) %>%
  mutate(fV = 2 / V^3) %>%
  mutate(fW = 4 * W^3)

head(sim_df)
          Y          U        V         W        fY         fU          fV
1 0.8613010 -0.7226020 1.161034 0.9280630 1.7226020 0.13869900 1.277894039
2 0.9231488 -0.8462976 1.083249 0.9608063 1.8462976 0.07685121 1.573421586
3 0.6345661 -0.2691321 1.575880 0.7965966 1.2691321 0.36543393 0.511046643
4 0.9399862 -0.8799724 1.063845 0.9695289 1.8799724 0.06001379 1.661094882
5 0.1591478  0.6817043 6.283466 0.3989334 0.3182957 0.84085217 0.008061802
6 0.2354025  0.5291951 4.248044 0.4851829 0.4708049 0.76459755 0.026089331
         fW
1 3.1973665
2 3.5478688
3 2.0219726
4 3.6453750
5 0.2539575
6 0.4568530
library(ggplot2)
library(patchwork)
Warning: package 'patchwork' was built under R version 4.5.2
pY <- ggplot(sim_df, aes(x = Y)) +
  geom_histogram(aes(y = after_stat(density)),
                 fill = 'goldenrod', color = 'black',
                 binwidth = 0.02, center = 0.01) +
  geom_line(aes(y = fY), color = "red", linewidth = 1) +
  xlim(0, 1) + ylim(0, 2.5) +
  labs(x = 'Y', y = expression(f[Y](y))) +
  theme_classic(base_size = 14) 
  
pU <-  ggplot(sim_df, aes(x = U)) +
  geom_histogram(aes(y = after_stat(density)),
                 fill = 'cornflowerblue', color = 'black',
                 binwidth = 0.02, center = -0.99) +
  geom_line(aes(y = (1-U)/2), color = "red", linewidth = 1) +
  scale_x_reverse() + 
  ylim(0, 1.5) +
  labs(x = 'U', y = expression(f[U](u))) +
  theme_classic(base_size = 14) 
  
pV <-   ggplot(sim_df, aes(x = V)) +
  geom_histogram(aes(y = after_stat(density)),
                 fill = 'red', color = 'black',
                 binwidth = 0.05, center = 1.01) +
  geom_line(aes(y = fV), color = "red", linewidth = 1) +
  xlim(1, 10) +
  labs(x = 'V', y = expression(f[V](v))) +
  theme_classic(base_size = 14) 
  
pW <-  ggplot(sim_df, aes(x = W)) +
  geom_histogram(aes(y = after_stat(density)),
                 fill = 'lightgreen', color = 'black',
                 binwidth = 0.02, center = 0.01) +
  geom_line(aes(y = fW), color = "red", linewidth = 1) +
  xlim(0, 1) + ylim(0, 1.5) +
  labs(x = 'W', y = expression(f[W](w))) +
  theme_classic(base_size = 14)


combined_plot <- (pY | pU) / (pV | pW)
combined_plot
Warning: Removed 93 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 93 rows containing missing values or values outside the scale range
(`geom_line()`).
Warning: Removed 14 rows containing missing values or values outside the scale range
(`geom_bar()`).
Warning: Removed 7278 rows containing missing values or values outside the scale range
(`geom_line()`).