Skip to content

Commit

Permalink
Version 0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
prdm0 committed May 2, 2024
1 parent 6a670bc commit 8086214
Show file tree
Hide file tree
Showing 22 changed files with 58 additions and 112 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions R/print.r
Original file line number Diff line number Diff line change
@@ -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
Expand Down
65 changes: 14 additions & 51 deletions R/qqplot.R
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions docs/articles/accept_reject.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/articles/inspect.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
pandoc: 3.1.1
pandoc: 3.1.11
pkgdown: 2.0.9
pkgdown_sha: ~
articles:
accept_reject: accept_reject.html
inspect: inspect.html
last_built: 2024-04-30T16:56Z
last_built: 2024-05-02T11:05Z

Binary file modified docs/reference/Rplot001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/reference/Rplot002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions docs/reference/print.accept_reject.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified docs/reference/qqplot.accept_reject-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/reference/qqplot.accept_reject-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8086214

Please sign in to comment.