From 8086214a5129898d616e01f980e8941d24103f81 Mon Sep 17 00:00:00 2001 From: prdm0 Date: Thu, 2 May 2024 08:26:15 -0300 Subject: [PATCH] Version 0.1.2 --- NAMESPACE | 1 + NEWS.md | 2 - R/print.r | 8 +-- R/qqplot.R | 65 +++++----------------- docs/articles/accept_reject.html | 4 +- docs/articles/inspect.html | 4 +- docs/news/index.html | 1 - docs/pkgdown.yml | 4 +- docs/reference/Rplot001.png | Bin 19600 -> 19914 bytes docs/reference/Rplot002.png | Bin 21585 -> 20586 bytes docs/reference/print.accept_reject.html | 10 ++-- docs/reference/qqplot.accept_reject-1.png | Bin 50952 -> 52220 bytes docs/reference/qqplot.accept_reject-2.png | Bin 65771 -> 63685 bytes docs/reference/qqplot.accept_reject.html | 32 ++++------- docs/search.json | 2 +- man/figures/README-unnamed-chunk-3-1.png | Bin 43673 -> 44498 bytes man/figures/README-unnamed-chunk-3-2.png | Bin 35208 -> 36971 bytes man/figures/README-unnamed-chunk-4-1.png | Bin 49071 -> 49756 bytes man/figures/README-unnamed-chunk-4-2.png | Bin 42111 -> 40545 bytes man/figures/README-unnamed-chunk-6-2.png | Bin 28889 -> 28242 bytes man/print.accept_reject.Rd | 8 +-- man/qqplot.accept_reject.Rd | 29 ++++------ 22 files changed, 58 insertions(+), 112 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index ebaa1ba..6ae1e8b 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -50,6 +50,7 @@ importFrom(stats,density) importFrom(stats,dunif) importFrom(stats,dweibull) importFrom(stats,optimize) +importFrom(stats,quantile) importFrom(stats,runif) importFrom(utils,capture.output) importFrom(utils,head) diff --git a/NEWS.md b/NEWS.md index 3b29bca..5c030f7 100644 --- a/NEWS.md +++ b/NEWS.md @@ -30,8 +30,6 @@ * The method `qqplot.accept_reject()` has been added, which constructs the QQ-Plot of an object of class `accept_reject` returned by the function `accept_reject()`; -* The `qqplot.accept_reject()` function can be parallelized using the `parallel = TRUE` argument, allowing theoretical quantiles to be calculated in parallel. The cores argument controls the number of cores that will be used. The default value, `cores = NULL`, means that all processor cores will be used. If `parallel = FALSE`, the cores argument is ignored. - * The `qqplot.accept_reject()` function utilizes the [**scattermore**](https://github.com/exaexa/scattermore) package if the point density is high, i.e., above 10 thousand observations; * The function `accept_reject()` now has the argument cores, which allows the user to control the number of cores that will be used if `parallel = TRUE`. The default, `cores = NULL`, means that all processor cores will be used. If `parallel = FALSE`, the cores argument is ignored; diff --git a/R/print.r b/R/print.r index fb21621..241a971 100644 --- a/R/print.r +++ b/R/print.r @@ -1,8 +1,8 @@ #' @title Print method for accept_reject objects -#' @description Print method for accept_reject objects -#' @param x An accept_reject object -#' @param n_min Minimum number of observations to print -#' @param ... Additional arguments +#' @description Print method for accept_reject objects. +#' @param x An accept_reject object. +#' @param n_min Minimum number of observations to print. +#' @param ... Additional arguments. #' #' @details #' The function [print.accept_reject()] is responsible for printing an object of diff --git a/R/qqplot.R b/R/qqplot.R index b684f8e..4415bf2 100644 --- a/R/qqplot.R +++ b/R/qqplot.R @@ -1,25 +1,3 @@ -quantile_custom <- function(x, p) { - f <- attr(x, "f") - continuous <- attr(x, "continuous") - xlim <- attr(x, "xlim") - - return( - internal_quantile( - continuous, - f, - p, - xlim[1L], - xlim[2L] - ) - ) -} - -quantile_custom_vec <- - Vectorize( - quantile_custom, - vectorize.args = "p" - ) - #' QQ-Plot #' QQ-Plot between observed quantiles and theoretical quantiles. #' @param x Object of the class `accept_reject` returned by the function @@ -49,24 +27,16 @@ qqplot <- function(x, ...) { #' @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. +#' @param ... Additional arguments for the `quantile()` function. For instance, +#' it's possible to change the algorithm type for quantile calculation. #' @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 +#' The function `qqplot.accept_reject()` 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 +#' @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. #' @examples @@ -89,11 +59,16 @@ qqplot <- function(x, ...) { #' xlim = c(-4, 4) #' ) #' qqplot(y) +#' +#' @seealso [accept_reject()], [plot.accept_reject()], [inspect()] and +#' [qqplot()]. +#' #' @importFrom Rcpp evalCpp #' @importFrom ggplot2 ggplot geom_point geom_abline labs theme element_text #' coord_cartesian scale_x_continuous scale_y_continuous aes_string #' @importFrom scattermore geom_scattermore #' @importFrom parallel mclapply detectCores +#' @importFrom stats quantile #' @export qqplot.accept_reject <- function(x, @@ -102,27 +77,15 @@ qqplot.accept_reject <- 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 <- - 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 - ) - ) - ) + n <- length(x) + p <- seq(1L, n) / (n + 1L) + theoretical_quantiles <- quantile(x, probs = p, ...) df <- data.frame(Theoretical = theoretical_quantiles, Sample = sample_quantiles) xlim <- attr(x, "xlim") diff --git a/docs/articles/accept_reject.html b/docs/articles/accept_reject.html index b0504c1..9bd197c 100644 --- a/docs/articles/accept_reject.html +++ b/docs/articles/accept_reject.html @@ -130,7 +130,7 @@

Understanding the Method\(X\) and go back to step 1.

Proof: Consider the discrete case, that is, \(X\) and \(Y\) are random variables with pfs \(f\) and \(g\), respectively. By step 3 of the -algorithm above, we have \(\{accept\} = \{x = +algorithm above, we have \(\{accept\} = \{x = y\} = u < \frac{f(y)}{cg(y)}\). That is,

\[P(accept | Y = y) = \frac{P(accept \cap \{Y = y\})}{g(y)} = \frac{P(U \leq f(y)/cg(y)) \times g(y)}{g(y)} = @@ -569,7 +569,7 @@

Accessing metadata#> <partialised> #> function (...) #> f(mean = 0, sd = 1, ...) -#> <environment: 0x57d6007fc988> +#> <environment: 0x62b16df07600> #> #> $args_f #> $args_f$mean diff --git a/docs/articles/inspect.html b/docs/articles/inspect.html index bbe696f..47394e0 100644 --- a/docs/articles/inspect.html +++ b/docs/articles/inspect.html @@ -202,7 +202,7 @@

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

Example of inspection c = 1.2 ) toc() -#> 0.118 sec elapsed +#> 0.123 sec elapsed # Visualizing the results p1 <- plot(case_1) diff --git a/docs/news/index.html b/docs/news/index.html index a5acaef..d23380a 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -72,7 +72,6 @@

AcceptReject

AcceptReject 0.1.2