Skip to content

Commit

Permalink
Updated the documentation for the plot.accept_reject and print.accept…
Browse files Browse the repository at this point in the history
…_reject functions.
  • Loading branch information
prdm0 committed Apr 11, 2024
1 parent eb9f0eb commit 1ef15c1
Show file tree
Hide file tree
Showing 51 changed files with 415 additions and 58 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@
^Meta$
^CRAN-SUBMISSION$
^cran-comments\.md$
^\.github/workflows/R-CMD-check\.yaml$
^\.github/workflows/pkgdown\.yaml$
^\.github/workflows/pr-commands\.yaml$
1 change: 1 addition & 0 deletions .github/workflows/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.html
62 changes: 62 additions & 0 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
#
# NOTE: This workflow is overkill for most R packages and
# check-standard.yaml is likely a better choice.
# usethis::use_github_action("check-standard") will install it.
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}

- {os: windows-latest, r: 'release'}
# Use 3.6 to trigger usage of RTools35
- {os: windows-latest, r: '3.6'}
# use 4.1 to check with rtools40's older compiler
- {os: windows-latest, r: '4.1'}

- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}
- {os: ubuntu-latest, r: 'oldrel-2'}
- {os: ubuntu-latest, r: 'oldrel-3'}
- {os: ubuntu-latest, r: 'oldrel-4'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
48 changes: 48 additions & 0 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]
release:
types: [published]
workflow_dispatch:

name: pkgdown

jobs:
pkgdown:
runs-on: ubuntu-latest
# Only restrict concurrency for non-PR jobs
concurrency:
group: pkgdown-${{ github.event_name != 'pull_request' || github.run_id }}
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
permissions:
contents: write
steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::pkgdown, local::.
needs: website

- name: Build site
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
shell: Rscript {0}

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@v4.4.1
with:
clean: false
branch: gh-pages
folder: docs
4 changes: 2 additions & 2 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 0.1.0
Date: 2024-04-09 12:46:41 UTC
SHA: 1887c8683f4223991bb88bf5ee95a2c4e48c2f1b
Date: 2024-04-10 18:22:40 UTC
SHA: eb9f0eb16bd6b8e4b5952f5735463c60001c229e
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# AcceptReject 0.1.0

* Initial CRAN submission.
80 changes: 47 additions & 33 deletions R/plot.r
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
.onLoad <- function(libname, pkgname) {
utils::globalVariables("prop")
}

#' @title Plot Accept-Reject
#'
#' @description
#' Inspects the probability function (discrete case) or probability density
#' (continuous case) by comparing the theoretical case with the observed one.
#'
#' @param x An object of class `accept reject`
#' @param color_observed_density Observed density color (continuous case)
#' @param color_observed_density Observed density color (continuous case).
#' @param color_true_density True density color (continuous case)
#' @param color_bar Bar chart fill color (discrete case)
#' @param color_observable_point Color of generated points (discrete case)
#' @param color_real_point Color of real probability points (discrete case)
#' @param alpha Bar chart transparency (discrete case)
#' @param ... Additional arguments
#' @param alpha Bar chart transparency (discrete case) and observed density
#' (continuous case)
#' @param ... Additional arguments.
#'
#' @details
#' The function [plot.accept_reject()] is responsible for plotting the
#' probability function (in the discrete case) or the probability density (in
#' the continuous case), comparing the theoretical case with the observed one.
#' It is useful, therefore, for inspecting the quality of the samples generated
#' by the acceptance-rejection method. The returned plot is an object of classes
#' `gg` and `ggplot`. Easily, you can further customize the plot.
#'
#' The function [plot.accept_reject()], or simply [plot()], constructs the plot
#' for inspection and expects an object of class `accept_reject` as an argument.
#'
#' @return An object of class `gg` and `ggplot` from the package \pkg{ggplot2}.
#' The function [plot.accept_reject()] expects an object of class
#' `accept_reject` as an argument.
#'
#' @seealso [accept_reject()] and [print.accept_reject()].
#'
#' @importFrom ggplot2 ggplot aes after_stat geom_line geom_freqpoly geom_point geom_bar labs scale_color_manual theme element_text
#' @importFrom ggplot2 ggplot aes after_stat geom_line geom_freqpoly geom_point
#' geom_bar labs scale_color_manual theme element_text
#' @importFrom glue glue
#' @importFrom rlang list2
#' @importFrom cli cli_alert_success cli_alert_info
#' @importFrom scales percent
#' @importFrom stats density
#' @import rlang
#'
#' @examples
#'
#' accept_reject(
#' n = 2000L,
#' f = dbinom,
Expand All @@ -41,16 +60,15 @@
#' xlim = c(-4, 4)
#' ) |> plot()
#'
#' @import rlang
#' @export
plot.accept_reject <-
function(
x,
color_observed_density = "blue",
color_true_density = "black",
color_bar = "blue",
color_observable_point = "red",
color_real_point = "green",
color_observed_density = "#FBBA78",
color_true_density = "#1D7DA5",
color_bar = "#FCEFC3",
color_observable_point = "#7BBDB3",
color_real_point = "#FE4F0E",
alpha = .3,
...
){
Expand All @@ -67,35 +85,30 @@ plot.accept_reject <-
data <- data.frame(x = as.vector(x), y = y)

graphic <- function(x){
p <- ggplot2::ggplot(data, ggplot2::aes(x = x))
if(attr(x, "continuous")){
capture.output(
p <-
ggplot2::ggplot(data, ggplot2::aes(x = x)) +
p +
ggplot2::geom_density(aes(y = after_stat(density), color = "Observed density"), fill = color_observed_density, alpha = alpha) +
ggplot2::geom_line(aes(y = y, color = "True density")) +
ggplot2::geom_freqpoly(aes(y = after_stat(density), color = "Observed density"), bins = 30) +
ggplot2::scale_color_manual(values = c("True density" = color_true_density, "Observed density" = color_observed_density)) +
ggplot2::labs(
x = "x",
y = "f(x)",
title = "Probability density function",
subtitle = "Real x Observed",
color = "Legenda"
) +
ggplot2::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")
color = "Legend"
)
)
return(p)
} else {
capture.output(
p <- ggplot2::ggplot(data, ggplot2::aes(x = x)) +
ggplot2::geom_line(aes(y = y)) +
p <-
p +
ggplot2::geom_bar(aes(y = after_stat(prop), group = 1L), fill = color_bar, alpha = alpha) +
ggplot2::geom_line(aes(y = y), linetype = "dotted") +
ggplot2::geom_point(aes(y = y, color = "Observable Probability")) +
ggplot2::geom_bar(aes(y = after_stat(prop), group = 1), fill = color_bar, alpha = alpha) +
ggplot2::geom_point(aes(y = after_stat(prop), group = 1, color = "Real Probability"), stat = "count") +
ggplot2::geom_point(aes(y = after_stat(prop), group = 1L, color = "Real Probability"), stat = "count") +
ggplot2::scale_color_manual(values = c("Observable Probability" = color_observable_point, "Real Probability" = color_real_point)) +
ggplot2::scale_y_continuous(labels = scales::percent) +
ggplot2::labs(
Expand All @@ -104,16 +117,17 @@ plot.accept_reject <-
title = "Probability Function",
subtitle = "Real x Observed",
color = "Legend"
) +
ggplot2::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")
)
)
return(p)
}
p <- p +
ggplot2::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")
)
return(p)
}
graphic(x)
}
25 changes: 24 additions & 1 deletion R/print.r
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,30 @@
#' @param x An accept_reject object
#' @param n_min Minimum number of observations to print
#' @param ... Additional arguments
#' @return NULL
#'
#' @details
#' The function [print.accept_reject()] is responsible for printing an object of
#' class `accept_reject` in a formatted manner, providing some information
#' about the `accept_reject` object, including the number of observations, the
#' value of the constant \eqn{c} that maximizes acceptance, and the acceptance
#' probability \eqn{1/c}. Additionally, it prints the first generated
#' observations. The function [print.accept_reject()] delivers formatted output
#' when executing an object of class `accept_reject` in the console or when
#' executing the function [print()] on an object of class `accept_reject`,
#' returned by the function [accept_reject()].

#' @return
#' An object of class `character`, providing a formatted output with some
#' information about the `accept_reject` object, including the number of
#' observations, the value of the constant \eqn{c} that maximizes acceptance, and
#' the acceptance probability \eqn{1/c}. Additionally, it prints the first
#' generated observations. The function [print.accept_reject()] enables
#' formatting when executing an object of class 'accept_reject' in the console
#' or when executing the function [print()] on an object of class
#' `accept_reject`, returned by the function [accept_reject()].
#'
#' @seealso [accept_reject()] and [plot.accept_reject().
#'
#' @importFrom cli cli_h1 cli_alert_success
#' @importFrom glue glue
#' @examples
Expand Down
3 changes: 3 additions & 0 deletions docs/404.html

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

3 changes: 3 additions & 0 deletions docs/LICENSE.html

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

Binary file modified docs/apple-touch-icon-120x120.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/apple-touch-icon-152x152.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/apple-touch-icon-180x180.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/apple-touch-icon-60x60.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/apple-touch-icon-76x76.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/apple-touch-icon.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 docs/articles/accept_reject.html

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/articles/index.html

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

3 changes: 3 additions & 0 deletions docs/authors.html

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

Binary file modified docs/favicon-16x16.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/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions docs/index.html

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

Loading

0 comments on commit 1ef15c1

Please sign in to comment.