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 Apr 28, 2024
1 parent db75305 commit e1755e9
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ importFrom(ggplot2,aes)
importFrom(ggplot2,aes_string)
importFrom(ggplot2,after_stat)
importFrom(ggplot2,annotate)
importFrom(ggplot2,coord_cartesian)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_text)
importFrom(ggplot2,geom_abline)
Expand All @@ -31,6 +32,8 @@ importFrom(ggplot2,labs)
importFrom(ggplot2,scale_color_manual)
importFrom(ggplot2,scale_colour_manual)
importFrom(ggplot2,scale_fill_manual)
importFrom(ggplot2,scale_x_continuous)
importFrom(ggplot2,scale_y_continuous)
importFrom(ggplot2,theme)
importFrom(glue,glue)
importFrom(graphics,hist)
Expand Down
17 changes: 15 additions & 2 deletions R/qqplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ 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 ... Additional arguments.
#' @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.
Expand All @@ -65,22 +66,34 @@ 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
#' @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 = "#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")) +
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) +
coord_cartesian(xlim = xlim, ylim = xlim) +
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")
)

if (!continuous) {
plot <- plot +
scale_x_continuous(breaks = xlim[1L]:xlim[2L]) +
scale_y_continuous(breaks = xlim[1L]:xlim[2L])
}
return(plot)
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ case_1 <- accept_reject(
xlim = c(0, 10)
)
toc()
#> 0.005 sec elapsed
#> 0.01 sec elapsed

# Specifying the base probability density function
tic()
Expand All @@ -341,7 +341,7 @@ case_2 <- accept_reject(
c = 1.2
)
toc()
#> 0.004 sec elapsed
#> 0.006 sec elapsed

# Visualizing the results
p1 <- plot(case_1)
Expand Down
Binary file modified man/figures/README-unnamed-chunk-2-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 man/figures/README-unnamed-chunk-2-2.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 man/figures/README-unnamed-chunk-3-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 man/figures/README-unnamed-chunk-3-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion man/qqplot.accept_reject.Rd

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

0 comments on commit e1755e9

Please sign in to comment.