Warning: package 'tidyverse' 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 3.5.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
param_tbl <-parameters(~alpha, ~beta, c(1), c(2)) %>%add_trials(10000)sim_df <- param_tbl %>%rowwise() %>%mutate(Y =rbeta(1,alpha,beta),U =1-2*Y, V =1/Y,W =sqrt(Y) )fY <-function(y) ifelse(y>0& y <1, 2*(1-y), 0)fU <-function(u) ifelse(u >-1& u <1, (1+ U)/2, 0)fV <-function(v) ifelse(v >1, 2*(v-1)/v^3, 0)fW <-function(w) ifelse(w >0& w <1, 4*w*(1-w^2), 0 )ggplot(sim_df, aes(x = Y))+geom_histogram(aes(y =after_stat(density)),bins =40, fill ="goldenrod", color ="black")+stat_function(fun = fY, color ="red", size =1)+theme_classic(base_size =14) +labs(x ="Y", y ="Density", title ="Histogram of beta")
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
ggplot(sim_df, aes(x = U)) +geom_histogram(aes(y =after_stat(density)), bins =40, fill ="cornflowerblue", color ="black")+stat_function(fun = fU, color ="red", size =1.2)+theme_classic()+labs(title ="Histogram of U", x="U", y ="Density")
Warning: Computation failed in `stat_function()`.
Caused by error in `fun()`:
! object 'U' not found
ggplot(sim_df, aes(x = V)) +geom_histogram(aes(y =after_stat(density)), bins =40, fill ="lightgray", color ="black")+stat_function(fun = fV, color ="red", size =1.2)+coord_cartesian(xlim=c(1,5)) +theme_classic()+labs(title ="Histogram of V", x ="V", y ="Densisty")
ggplot(sim_df, aes(x = W)) +geom_histogram(aes(y =after_stat(density)), bins =40, fill ="orange", color ="black")+stat_function(fun = fW, color ="red", size =1.2)+theme_classic()+labs(title ="Histogram of W", x ="W", y ="Density")