diff --git a/DESCRIPTION b/DESCRIPTION index fc8ac35..129cc7f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,7 +25,8 @@ Imports: purrr, Rcpp, rlang, - scales + scales, + scattermore Suggests: knitr, rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index 1df63b4..ebaa1ba 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -45,6 +45,7 @@ importFrom(purrr,map_dbl) importFrom(purrr,partial) importFrom(rlang,list2) importFrom(scales,percent) +importFrom(scattermore,geom_scattermore) importFrom(stats,density) importFrom(stats,dunif) importFrom(stats,dweibull) diff --git a/R/accept_reject.r b/R/accept_reject.r index e59c033..c5c1ec1 100644 --- a/R/accept_reject.r +++ b/R/accept_reject.r @@ -92,7 +92,7 @@ #' f = dbinom, #' continuous = FALSE, #' args_f = list(size = 5, prob = 0.5), -#' xlim = c(0, 10) +#' xlim = c(0, 5) #' ) #' plot(x) #' diff --git a/R/plot.r b/R/plot.r index 2724d1b..58a5704 100644 --- a/R/plot.r +++ b/R/plot.r @@ -15,8 +15,8 @@ #' @param color_real_point Color of real probability points (discrete case) #' @param alpha Bar chart transparency (discrete case) and observed density #' (continuous case) -#' @param hist If TRUE, a histogram will be plotted in the continuous case, -#' comparing the theoretical density with the observed one. If FALSE, +#' @param hist If `TRUE`, a histogram will be plotted in the continuous case, +#' comparing the theoretical density with the observed one. If `FALSE`, #' [ggplot2::geom_density()] will be used instead of the histogram. #' #' @param ... Additional arguments. @@ -72,10 +72,10 @@ plot.accept_reject <- function( x, color_observed_density = "#BB9FC9", #"#E65A65", # "#FBBA78", - color_true_density = "#FE4F0E", + color_true_density = "#F890C2", #"#FE4F0E", color_bar = "#BB9FC9", #"#E65A65", #"#FCEFC3", color_observable_point = "#7BBDB3", - color_real_point = "#FE4F0E", + color_real_point = "#F890C2", #"#FE4F0E", alpha = .3, hist = TRUE, ... diff --git a/R/plot_inspect.r b/R/plot_inspect.r index 0d3fc60..854dbe8 100644 --- a/R/plot_inspect.r +++ b/R/plot_inspect.r @@ -103,7 +103,7 @@ inspect <- c = 1, alpha = 0.4, color_intersection = "#BB9FC9", - color_f = "#FE4F0E", + color_f = "#F890C2", #"#FE4F0E", color_f_base = "#7BBDB3" ) { diff --git a/R/qqplot.R b/R/qqplot.R index 99ca71a..b684f8e 100644 --- a/R/qqplot.R +++ b/R/qqplot.R @@ -22,17 +22,19 @@ quantile_custom_vec <- #' QQ-Plot #' QQ-Plot between observed quantiles and theoretical quantiles. -#' @param x Object of the class `accept_reject` returned by the function `accept_reject()`. +#' @param x Object of the class `accept_reject` returned by the function +#' `accept_reject()`. #' @param ... Additional arguments to be passed to methods. -#' @returns An object of classes `gg` and `ggplot` with the QQ-Plot of theoretical quantiles versus observed quantiles. +#' @returns An object of classes `gg` and `ggplot` with the QQ-Plot of +#' theoretical quantiles versus observed quantiles. #' @details #' Generic method to plot the QQ-Plot between observed quantiles and theoretical #' quantiles. The generic method will call the specific method #' `qqplot.accept_reject()`, which operates on objects of class accept_reject #' returned by the function `accept_reject()`. #' -#' @seealso [accept_reject()], [print.accept_reject()], [plot.accept_reject()] and -#' [inspect()]. +#' @seealso [accept_reject()], [print.accept_reject()], [plot.accept_reject()] +#' and [inspect()]. #' @export qqplot <- function(x, ...) { UseMethod("qqplot") @@ -41,12 +43,29 @@ qqplot <- function(x, ...) { #' QQ-Plot #' Plot the QQ-Plot between observed quantiles and theoretical quantiles. #' @param x Object of the class accept_reject returned by the function `accept_reject()`. -#' @param alpha Transparency of the points and reference line representing where the quantiles should be (theoretical quantiles). -#' @param color_points Color of the points (default is `"#FE4F0E"`). +#' @param alpha Transparency of the points and reference line representing where +#' the quantiles should be (theoretical quantiles). +#' @param color_points Color of the points (default is `"#F890C2"`). #' @param color_line Color of the reference line (detault is `"#BB9FC9"`). #' @param size_points Size of the points (default is `1`). #' @param size_line Thickness of the reference line (default is `1`). +#' @param parallel If `TRUE`, all cores will be used for internal calculations of +#' theoretical quantiles. The default is `FALSE`. Use TRUE if you find the plot +#' is taking too long. +#' @param cores Number of cores to be used if `parallel = TRUE`. Defalut is `NULL`, +#' which means all cores will be used. #' @param ... Additional arguments. +#' @details +#' Just like in the `accept_reject()` function, the `qqplot.accept_reject()` +#' function uses parallelism using FORK, meaning it works on Unix-based +#' operating systems (Linux and MacOS). What is parallelized are the internal +#' calculations of theoretical quantiles of the true distribution. This +#' parallelism will only be useful for excessively large samples. Additionally, +#' for samples larger than ten thousand, the `geom_scattermost()` function from +#' the [**scattermore**](https://CRAN.R-project.org/package=scattermore) library +#' is used to plot the points, as it is more efficient than `geom_point()` from +#' the [**ggplot2**](https://CRAN.R-project.org/package=ggplot2) library. +#' #' @return An object of classes gg and ggplot with the QQ-Plot between the #' observed quantiles generated by the return of the function `accept_reject()` #' and the theoretical quantiles of the true distribution. @@ -58,7 +77,7 @@ qqplot <- function(x, ...) { #' f = dbinom, #' continuous = FALSE, #' args_f = list(size = 5, prob = 0.5), -#' xlim = c(0, 10) +#' xlim = c(0, 5) #' ) #' qqplot(x) #' @@ -72,20 +91,59 @@ qqplot <- function(x, ...) { #' qqplot(y) #' @importFrom Rcpp evalCpp #' @importFrom ggplot2 ggplot geom_point geom_abline labs theme element_text -#' coord_cartesian scale_x_continuous scale_y_continuous -#' aes_string +#' coord_cartesian scale_x_continuous scale_y_continuous aes_string +#' @importFrom scattermore geom_scattermore +#' @importFrom parallel mclapply detectCores #' @export -qqplot.accept_reject <- function(x, alpha = 0.5, color_points = "#FE4F0E", color_line = "#BB9FC9", size_points = 1, size_line = 1, ...) { +qqplot.accept_reject <- + function(x, + alpha = 0.5, + color_points = "#F890C2", + color_line = "#BB9FC9", + size_points = 1, + size_line = 1, + parallel = FALSE, + cores = NULL, + ... + ) { + + continuous <- attr(x, "continuous") sample_quantiles <- sort(x) p <- (rank(sample_quantiles) - 0.375) / (length(sample_quantiles) + 0.25) - theoretical_quantiles <- quantile_custom_vec(x = x, p = p) + + theoretical_quantiles <- + unlist( + parallel::mclapply( + X = p, + FUN = function(i) quantile_custom_vec(x = x, i), + mc.cores = ifelse( + parallel, + ifelse(is.null(cores), parallel::detectCores(), cores), + 1L + ) + ) + ) + df <- data.frame(Theoretical = theoretical_quantiles, Sample = sample_quantiles) xlim <- attr(x, "xlim") - continuous <- attr(x, "continuous") plot <- ggplot(df, aes_string(x = "Theoretical", y = "Sample")) + - geom_abline(slope = 1, intercept = 0, color = color_line, size = size_line) + - geom_point(alpha = alpha, color = color_points, size = size_points) + + geom_abline(slope = 1, intercept = 0, color = color_line, size = size_line) + + if (continuous && length(x) >= 10e3){ + plot <- plot + + geom_scattermore( + pointsize = size_points + 2, + interpolate = TRUE, + color = color_points + ) + } else { + plot <- plot + + geom_point(alpha = alpha, color = color_points, size = size_points) + } + + plot <- + plot + coord_cartesian(xlim = xlim, ylim = xlim) + labs(x = "Theoretical Quantiles", y = "Sample Quantiles", title = "QQ-Plot") + theme( diff --git a/README.Rmd b/README.Rmd index 5caf938..b4d119b 100644 --- a/README.Rmd +++ b/README.Rmd @@ -96,10 +96,10 @@ p4 <- plot(d) plot_grid(p1, p2, p3, p4, nrow = 2L, labels = c("a", "b", "c", "d")) # QQ-Plots -q1 <- qqplot(a) -q2 <- qqplot(b) -q3 <- qqplot(c) -q4 <- qqplot(d) +q1 <- qqplot(a, size_points = 2) +q2 <- qqplot(b, size_points = 2) +q3 <- qqplot(c, size_points = 2) +q4 <- qqplot(d, size_points = 2) plot_grid(q1, q2, q3, q4, nrow = 2L, labels = c("a", "b", "c", "d")) ``` diff --git a/README.md b/README.md index 7dcb26a..3bb886c 100644 --- a/README.md +++ b/README.md @@ -190,10 +190,10 @@ plot_grid(p1, p2, p3, p4, nrow = 2L, labels = c("a", "b", "c", "d")) ``` r # QQ-Plots -q1 <- qqplot(a) -q2 <- qqplot(b) -q3 <- qqplot(c) -q4 <- qqplot(d) +q1 <- qqplot(a, size_points = 2) +q2 <- qqplot(b, size_points = 2) +q3 <- qqplot(c, size_points = 2) +q4 <- qqplot(d, size_points = 2) plot_grid(q1, q2, q3, q4, nrow = 2L, labels = c("a", "b", "c", "d")) ``` @@ -377,7 +377,7 @@ case_2 <- accept_reject( c = 1.2 ) toc() -#> 0.007 sec elapsed +#> 0.006 sec elapsed # Visualizing the results p1 <- plot(case_1) diff --git a/docs/articles/accept_reject.html b/docs/articles/accept_reject.html index eef05c8..570fce4 100644 --- a/docs/articles/accept_reject.html +++ b/docs/articles/accept_reject.html @@ -569,7 +569,7 @@

Accessing metadata#> <partialised> #> function (...) #> f(mean = 0, sd = 1, ...) -#> <environment: 0x580be468eec0> +#> <environment: 0x648a3576acc8> #> #> $args_f #> $args_f$mean diff --git a/docs/articles/accept_reject_files/figure-html/unnamed-chunk-12-1.png b/docs/articles/accept_reject_files/figure-html/unnamed-chunk-12-1.png index f4f8fb2..e92c666 100644 Binary files a/docs/articles/accept_reject_files/figure-html/unnamed-chunk-12-1.png and b/docs/articles/accept_reject_files/figure-html/unnamed-chunk-12-1.png differ diff --git a/docs/articles/accept_reject_files/figure-html/unnamed-chunk-13-1.png b/docs/articles/accept_reject_files/figure-html/unnamed-chunk-13-1.png index 5fb1d4b..36f687a 100644 Binary files a/docs/articles/accept_reject_files/figure-html/unnamed-chunk-13-1.png and b/docs/articles/accept_reject_files/figure-html/unnamed-chunk-13-1.png differ diff --git a/docs/articles/inspect.html b/docs/articles/inspect.html index ad6032f..f55025b 100644 --- a/docs/articles/inspect.html +++ b/docs/articles/inspect.html @@ -202,7 +202,7 @@

Example of inspection xlim = c(0, 10) ) toc() -#> 0.417 sec elapsed +#> 0.408 sec elapsed # Specifying the base probability density function tic() @@ -218,7 +218,7 @@

Example of inspection c = 1.2 ) toc() -#> 0.149 sec elapsed +#> 0.151 sec elapsed # Visualizing the results p1 <- plot(case_1) diff --git a/docs/articles/inspect_files/figure-html/unnamed-chunk-2-1.png b/docs/articles/inspect_files/figure-html/unnamed-chunk-2-1.png index 403106c..1ebc8d9 100644 Binary files a/docs/articles/inspect_files/figure-html/unnamed-chunk-2-1.png and b/docs/articles/inspect_files/figure-html/unnamed-chunk-2-1.png differ diff --git a/docs/articles/inspect_files/figure-html/unnamed-chunk-3-1.png b/docs/articles/inspect_files/figure-html/unnamed-chunk-3-1.png index 4ddfc82..9f779fd 100644 Binary files a/docs/articles/inspect_files/figure-html/unnamed-chunk-3-1.png and b/docs/articles/inspect_files/figure-html/unnamed-chunk-3-1.png differ diff --git a/docs/index.html b/docs/index.html index 1eb1ff6..9d902d2 100644 --- a/docs/index.html +++ b/docs/index.html @@ -91,7 +91,7 @@

By default, the accept_reject function attempts to find the value of c that maximizes the probability of acceptance of the pseudo-random observations generated. However, it is possible to provide a value of c to the accept_reject() function through the argument c, where Y is a random variable for which we know how to generate observations. For the accept_reject() function, it is not necessary to specify the probability function or probability density function of Y to generate observations of X for discrete and continuous cases, respectively. For the discrete and continuous cases, Y follows the discrete uniform distribution function and continuous uniform distribution function, respectively.

Since the probability of acceptance is 1/c, the accept_reject() function attempts to find the minimum value of c that satisfies the description above. Unless you have compelling reasons to provide a value for the c argument of the accept_reject() function, it is recommended to use c = NULL (default), allowing a value of c to be automatically determined.

-

πŸ’»Installation +

πŸ’» Installation

The package is being versioned on GitHub. You can install the development version of AcceptReject, and to do this, you must first install the remotes package and then run the following command:

@@ -150,10 +150,10 @@ 

Generating discrete observations
 
 # QQ-Plots
-q1 <- qqplot(a)
-q2 <- qqplot(b)
-q3 <- qqplot(c)
-q4 <- qqplot(d)
+q1 <- qqplot(a, size_points = 2)
+q2 <- qqplot(b, size_points = 2)
+q3 <- qqplot(c, size_points = 2)
+q4 <- qqplot(d, size_points = 2)
 
 plot_grid(q1, q2, q3, q4, nrow = 2L, labels = c("a", "b", "c", "d"))

@@ -272,7 +272,7 @@

πŸ•΅οΈβ€β™€οΈ Example of inspection< xlim = c(0, 6) ) toc() -#> 0.008 sec elapsed +#> 0.01 sec elapsed # Specifying the base probability density function tic() @@ -288,7 +288,7 @@

πŸ•΅οΈβ€β™€οΈ Example of inspection< c = 1.2 ) toc() -#> 0.005 sec elapsed +#> 0.006 sec elapsed # Visualizing the results p1 <- plot(case_1) diff --git a/docs/pkgdown.yml b/docs/pkgdown.yml index ce71f05..50b2988 100644 --- a/docs/pkgdown.yml +++ b/docs/pkgdown.yml @@ -4,5 +4,5 @@ pkgdown_sha: ~ articles: accept_reject: accept_reject.html inspect: inspect.html -last_built: 2024-04-29T11:14Z +last_built: 2024-04-30T13:40Z diff --git a/docs/reference/Rplot001.png b/docs/reference/Rplot001.png index e3d64c8..ad4185a 100644 Binary files a/docs/reference/Rplot001.png and b/docs/reference/Rplot001.png differ diff --git a/docs/reference/Rplot002.png b/docs/reference/Rplot002.png index 81bb25c..58587d8 100644 Binary files a/docs/reference/Rplot002.png and b/docs/reference/Rplot002.png differ diff --git a/docs/reference/Rplot003.png b/docs/reference/Rplot003.png index b05efa4..1d6b5f7 100644 Binary files a/docs/reference/Rplot003.png and b/docs/reference/Rplot003.png differ diff --git a/docs/reference/accept_reject-1.png b/docs/reference/accept_reject-1.png index 737f419..21d9330 100644 Binary files a/docs/reference/accept_reject-1.png and b/docs/reference/accept_reject-1.png differ diff --git a/docs/reference/accept_reject-2.png b/docs/reference/accept_reject-2.png index c39c20c..34254ff 100644 Binary files a/docs/reference/accept_reject-2.png and b/docs/reference/accept_reject-2.png differ diff --git a/docs/reference/accept_reject.html b/docs/reference/accept_reject.html index 7687928..744dd14 100644 --- a/docs/reference/accept_reject.html +++ b/docs/reference/accept_reject.html @@ -215,8 +215,9 @@

Examples f = dbinom, continuous = FALSE, args_f = list(size = 5, prob = 0.5), - xlim = c(0, 10) + xlim = c(0, 5) ) +#> ! Warning: f(5) is 0.03125. If f is defined for x >= f(5), trying a upper limit might be better. plot(x) diff --git a/docs/reference/figures/README-unnamed-chunk-3-1.png b/docs/reference/figures/README-unnamed-chunk-3-1.png index 30fecb2..00b4bbe 100644 Binary files a/docs/reference/figures/README-unnamed-chunk-3-1.png and b/docs/reference/figures/README-unnamed-chunk-3-1.png differ diff --git a/docs/reference/figures/README-unnamed-chunk-3-2.png b/docs/reference/figures/README-unnamed-chunk-3-2.png index bc34b80..93d05b5 100644 Binary files a/docs/reference/figures/README-unnamed-chunk-3-2.png and b/docs/reference/figures/README-unnamed-chunk-3-2.png differ diff --git a/docs/reference/figures/README-unnamed-chunk-4-1.png b/docs/reference/figures/README-unnamed-chunk-4-1.png index 1d4d3e0..bbe9d21 100644 Binary files a/docs/reference/figures/README-unnamed-chunk-4-1.png and b/docs/reference/figures/README-unnamed-chunk-4-1.png differ diff --git a/docs/reference/figures/README-unnamed-chunk-4-2.png b/docs/reference/figures/README-unnamed-chunk-4-2.png index de781df..9085f00 100644 Binary files a/docs/reference/figures/README-unnamed-chunk-4-2.png and b/docs/reference/figures/README-unnamed-chunk-4-2.png differ diff --git a/docs/reference/figures/README-unnamed-chunk-5-1.png b/docs/reference/figures/README-unnamed-chunk-5-1.png index c406833..07544e0 100644 Binary files a/docs/reference/figures/README-unnamed-chunk-5-1.png and b/docs/reference/figures/README-unnamed-chunk-5-1.png differ diff --git a/docs/reference/figures/README-unnamed-chunk-6-1.png b/docs/reference/figures/README-unnamed-chunk-6-1.png index e6e287f..108ed5a 100644 Binary files a/docs/reference/figures/README-unnamed-chunk-6-1.png and b/docs/reference/figures/README-unnamed-chunk-6-1.png differ diff --git a/docs/reference/figures/README-unnamed-chunk-6-2.png b/docs/reference/figures/README-unnamed-chunk-6-2.png index 146a07d..4e078b3 100644 Binary files a/docs/reference/figures/README-unnamed-chunk-6-2.png and b/docs/reference/figures/README-unnamed-chunk-6-2.png differ diff --git a/docs/reference/inspect-1.png b/docs/reference/inspect-1.png index d13e2c7..566be32 100644 Binary files a/docs/reference/inspect-1.png and b/docs/reference/inspect-1.png differ diff --git a/docs/reference/inspect-2.png b/docs/reference/inspect-2.png index 58ee109..6f170f0 100644 Binary files a/docs/reference/inspect-2.png and b/docs/reference/inspect-2.png differ diff --git a/docs/reference/inspect-3.png b/docs/reference/inspect-3.png index 5d9079c..684f4a9 100644 Binary files a/docs/reference/inspect-3.png and b/docs/reference/inspect-3.png differ diff --git a/docs/reference/inspect.html b/docs/reference/inspect.html index f358021..9064b8b 100644 --- a/docs/reference/inspect.html +++ b/docs/reference/inspect.html @@ -70,7 +70,7 @@

Usage c = 1, alpha = 0.4, color_intersection = "#BB9FC9", - color_f = "#FE4F0E", + color_f = "#F890C2", color_f_base = "#7BBDB3" )

diff --git a/docs/reference/plot.accept_reject-1.png b/docs/reference/plot.accept_reject-1.png index 755d2ff..a205447 100644 Binary files a/docs/reference/plot.accept_reject-1.png and b/docs/reference/plot.accept_reject-1.png differ diff --git a/docs/reference/plot.accept_reject-2.png b/docs/reference/plot.accept_reject-2.png index 3203b13..5fed8fe 100644 Binary files a/docs/reference/plot.accept_reject-2.png and b/docs/reference/plot.accept_reject-2.png differ diff --git a/docs/reference/plot.accept_reject.html b/docs/reference/plot.accept_reject.html index eea689f..f97fbdb 100644 --- a/docs/reference/plot.accept_reject.html +++ b/docs/reference/plot.accept_reject.html @@ -65,10 +65,10 @@

Usage plot( x, color_observed_density = "#BB9FC9", - color_true_density = "#FE4F0E", + color_true_density = "#F890C2", color_bar = "#BB9FC9", color_observable_point = "#7BBDB3", - color_real_point = "#FE4F0E", + color_real_point = "#F890C2", alpha = 0.3, hist = TRUE, ... @@ -107,8 +107,8 @@

Argumentsggplot2::geom_density() will be used instead of the histogram.

diff --git a/docs/reference/qqplot.accept_reject-1.png b/docs/reference/qqplot.accept_reject-1.png index dcad580..95c31a2 100644 Binary files a/docs/reference/qqplot.accept_reject-1.png and b/docs/reference/qqplot.accept_reject-1.png differ diff --git a/docs/reference/qqplot.accept_reject-2.png b/docs/reference/qqplot.accept_reject-2.png index 2d5b0eb..2246f63 100644 Binary files a/docs/reference/qqplot.accept_reject-2.png and b/docs/reference/qqplot.accept_reject-2.png differ diff --git a/docs/reference/qqplot.accept_reject.html b/docs/reference/qqplot.accept_reject.html index 95f8507..14dcbd1 100644 --- a/docs/reference/qqplot.accept_reject.html +++ b/docs/reference/qqplot.accept_reject.html @@ -68,10 +68,12 @@

Usage qqplot( x, alpha = 0.5, - color_points = "#FE4F0E", + color_points = "#F890C2", color_line = "#BB9FC9", size_points = 1, size_line = 1, + parallel = FALSE, + cores = NULL, ... ) @@ -83,11 +85,12 @@

Arguments).

+

Color of the points (default is "#F890C2").

color_line
@@ -102,6 +105,17 @@

ArgumentsValue

and the theoretical quantiles of the true distribution.

+
+

Details

+

Just like in the accept_reject() function, the qqplot.accept_reject() +function uses parallelism using FORK, meaning it works on Unix-based +operating systems (Linux and MacOS). What is parallelized are the internal +calculations of theoretical quantiles of the true distribution. This +parallelism will only be useful for excessively large samples. Additionally, +for samples larger than ten thousand, the geom_scattermost() function from +the scattermore library +is used to plot the points, as it is more efficient than geom_point() from +the ggplot2 library.

+

Examples

@@ -126,8 +152,9 @@

Examples f = dbinom, continuous = FALSE, args_f = list(size = 5, prob = 0.5), - xlim = c(0, 10) + xlim = c(0, 5) ) +#> ! Warning: f(5) is 0.03125. If f is defined for x >= f(5), trying a upper limit might be better. qqplot(x) diff --git a/docs/reference/qqplot.html b/docs/reference/qqplot.html index 2015e56..2420864 100644 --- a/docs/reference/qqplot.html +++ b/docs/reference/qqplot.html @@ -70,7 +70,8 @@

Usage

Arguments

x
-

Object of the class accept_reject returned by the function accept_reject().

+

Object of the class accept_reject returned by the function +accept_reject().

...
@@ -81,7 +82,8 @@

ArgumentsValue

-

An object of classes gg and ggplot with the QQ-Plot of theoretical quantiles versus observed quantiles.

+

An object of classes gg and ggplot with the QQ-Plot of +theoretical quantiles versus observed quantiles.

Details

@@ -92,8 +94,8 @@

Details