-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
35 changed files
with
656 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
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] | ||
) | ||
) | ||
} | ||
|
||
#' QQ-Plot | ||
#' QQ-Plot between observed quantiles and theoretical quantiles. | ||
#' @param x Object of the class `accept_reject` returned by the function `accept_reject()`. | ||
#' @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()]. | ||
#' @export | ||
qqplot <- function(x) { | ||
UseMethod("qqplot") | ||
} | ||
|
||
#' 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 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`). | ||
#' @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 | ||
#' set.seed(0) # setting a seed for reproducibility | ||
#' | ||
#' x <- accept_reject( | ||
#' n = 2000L, | ||
#' f = dbinom, | ||
#' continuous = FALSE, | ||
#' args_f = list(size = 5, prob = 0.5), | ||
#' xlim = c(0, 10) | ||
#' ) | ||
#' qqplot(x) | ||
#' | ||
#' y <- accept_reject( | ||
#' n = 1000L, | ||
#' f = dnorm, | ||
#' continuous = TRUE, | ||
#' args_f = list(mean = 0, sd = 1), | ||
#' xlim = c(-4, 4) | ||
#' ) | ||
#' qqplot(y) | ||
#' @importFrom Rcpp evalCpp | ||
#' @importFrom ggplot2 ggplot geom_point geom_abline labs theme element_text | ||
#' aes_string | ||
#' @export | ||
qqplot.accept_reject <- function(x, alpha = 0.5, color_points = "#FE4F0E", color_line = "#BB9FC9", size_points = 1, size_line = 1) { | ||
sample_quantiles <- sort(x) | ||
p <- (rank(sample_quantiles) - 0.375) / (length(sample_quantiles) + 0.25) | ||
theoretical_quantiles <- sapply(p, function(p) quantile_custom(x = x, p = p)) | ||
|
||
df <- data.frame(Theoretical = theoretical_quantiles, Sample = sample_quantiles) | ||
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) + | ||
labs(x = "Theoretical Quantiles", y = "Sample Quantiles", title = "QQ-Plot") + | ||
theme( | ||
axis.title = ggplot2::element_text(face = "bold"), | ||
title = ggplot2::element_text(face = "bold"), | ||
legend.title = ggplot2::element_text(face = "bold"), | ||
plot.subtitle = ggplot2::element_text(face = "plain") | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.