From 394e19797922aeb6cc0bd9f4eada061e222b6bbe Mon Sep 17 00:00:00 2001 From: Audrey Yeo Date: Wed, 26 Jun 2024 13:57:03 +0200 Subject: [PATCH] 97_myPlot_only (#108) Co-authored-by: 27856297+dependabot-preview[bot]@users.noreply.github.com <27856297+dependabot-preview[bot]@users.noreply.github.com> Co-authored-by: Daniel Sabanes Bove --- .pre-commit-config.yaml | 1 + DESCRIPTION | 2 ++ NAMESPACE | 2 +- R/dbetabinom.R | 4 +-- R/plotBeta.R | 49 +++++++++++++++------------------- examples/myPlot.R | 2 -- examples/myPlotDiff.R | 9 ++++++- examples/plotBeta.R | 2 ++ man/myPlot.Rd | 26 ------------------ man/myPlotDiff.Rd | 11 ++++++-- man/plotBeta.Rd | 24 +++++++++++++++++ tests/testthat/test-plotBeta.R | 9 +++++++ tests/testthat/test-predprob.R | 2 +- vignettes/introduction.Rmd | 31 ++++++++++++--------- 14 files changed, 99 insertions(+), 75 deletions(-) delete mode 100644 examples/myPlot.R create mode 100644 examples/plotBeta.R delete mode 100644 man/myPlot.Rd create mode 100644 man/plotBeta.Rd create mode 100644 tests/testthat/test-plotBeta.R diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 42db738a..c6ea30b4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -9,6 +9,7 @@ repos: args: [--style_pkg=styler, --style_fun=tidyverse_style] - id: roxygenize additional_dependencies: + - ggplot2 - devtools - shiny - checkmate diff --git a/DESCRIPTION b/DESCRIPTION index cbeab397..b568d599 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -22,6 +22,7 @@ BugReports: https://github.com/genentech/phase1b/issues Depends: R (>= 3.6) Suggests: + scales, rmarkdown, bookdown, knitr, @@ -32,6 +33,7 @@ Suggests: reshape, testthat (>= 3.0.0) Imports: + ggplot2, checkmate, devtools, lifecycle diff --git a/NAMESPACE b/NAMESPACE index 9b572e48..c499d7fe 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,7 +6,6 @@ export(dbetaMix) export(dbetabinom) export(dbetabinomMix) export(dbetadiff) -export(myPlot) export(myPlotDiff) export(oc2) export(oc3) @@ -18,6 +17,7 @@ export(ocRctPostprobDist) export(ocRctPredprobDist) export(pbetaMix) export(pbetadiff) +export(plotBeta) export(plotBounds) export(plotDecision) export(plotOc) diff --git a/R/dbetabinom.R b/R/dbetabinom.R index 0150304a..e14f331a 100644 --- a/R/dbetabinom.R +++ b/R/dbetabinom.R @@ -80,8 +80,8 @@ dbetabinomMix <- Vectorize(dbetabinomMix, vectorize.args = "x") #' #' @keywords internal h_getBetamixPost <- function(x, n, par, weights) { - assert_numeric(x, lower = 0, upper = n, finite = TRUE) - assert_numeric(n, lower = 0, finite = TRUE) + assert_number(x, lower = 0, upper = n, finite = TRUE) + assert_number(n, lower = 0, finite = TRUE) assert_matrix(par, min.rows = 1, max.cols = 2, mode = "numeric") assert_numeric(weights, min.len = 0, len = nrow(par), finite = TRUE) # We renormalize weights. diff --git a/R/plotBeta.R b/R/plotBeta.R index d4ebf5a3..b81330c3 100644 --- a/R/plotBeta.R +++ b/R/plotBeta.R @@ -2,44 +2,37 @@ #' #' This function will plot the PDF of a beta distribution #' -#' @param alpha first parameter of the Beta distribution -#' @param beta second parameter of the Beta distribution -#' @param \dots additional arguments to \code{plot} -#' @return nothing, only produces the plot as side effect +#' @inheritParams dbetabinom +#' @typed alpha : number +#' first parameter of the Beta distribution +#' @typed beta : number +#' second parameter of the Beta distribution +#' @return A beta distribution density plot #' #' @importFrom graphics axis #' -#' @example examples/myPlot.R +#' @example examples/plotBeta.R #' @export #' @keywords graphics -myPlot <- function(alpha, beta, ...) { - grid <- seq(from = 0, to = 1, length = 1000) - xticks <- seq(from = 0, to = 1, by = 0.25) - - plot( - x = grid, - y = dbeta(grid, alpha, beta), - ylab = "", - xaxt = "n", - yaxt = "n", - type = "l", - xaxs = "i", - yaxs = "i", - ... - ) - - graphics::axis( - side = 1, at = xticks, - labels = paste(xticks * 100, "%", sep = "") +plotBeta <- function(alpha, beta, ...) { + x_support <- seq(from = 0, to = 1, length = 1000) + data <- data.frame( + grid = x_support, + xticks = seq(from = 0, to = 1, by = 0.25), + density = dbeta(x_support, alpha, beta) ) + ggplot2::ggplot(data) + + ggplot2::geom_line(ggplot2::aes(x = grid, y = density)) + + ggplot2::ggtitle(paste("Beta density with alpha =", alpha, "and beta =", beta, "parameters.")) + + ggplot2::xlab("response rate") + + ggplot2::ylab(quote(f(x))) + + ggplot2::theme(axis.ticks.x = ggplot2::element_line(linewidth = 0.5)) + + ggplot2::scale_x_continuous(labels = scales::percent_format()) } - - - #' Plot Diff Between two Beta distributions #' -#' This function will plot the PDF of a diffience between two Beta distributions +#' This function will plot the PDF of a difference between two Beta distributions #' #' @param parY non-negative parameters of the treatment Beta distribution. #' @param parX non-negative parameters of the historical control Beta distribution diff --git a/examples/myPlot.R b/examples/myPlot.R deleted file mode 100644 index 044ba110..00000000 --- a/examples/myPlot.R +++ /dev/null @@ -1,2 +0,0 @@ -myPlot(1, 2) -myPlot(1, 1) diff --git a/examples/myPlotDiff.R b/examples/myPlotDiff.R index 3858bec9..c9a4795e 100644 --- a/examples/myPlotDiff.R +++ b/examples/myPlotDiff.R @@ -1 +1,8 @@ -myPlotDiff(c(5, 10), c(2, 5), 0.2, 0.05, 1, 0) +myPlotDiff( + parY = c(5, 10), + parX = c(2, 5), + cut_B = 0.2, # a meaningful improvement threshold + cut_W = 0.05, # a poor improvement threshold + shade = 1, # paint the two areas under the curve, default: yes. other numbers stands for "no"; + note = 0 +) # show values of the colored area, default: yes. other numbers stands for "no"; diff --git a/examples/plotBeta.R b/examples/plotBeta.R new file mode 100644 index 00000000..6e84087b --- /dev/null +++ b/examples/plotBeta.R @@ -0,0 +1,2 @@ +plotBeta(alpha = 4, beta = 5) +plotBeta(alpha = 1, beta = 1) diff --git a/man/myPlot.Rd b/man/myPlot.Rd deleted file mode 100644 index b583c635..00000000 --- a/man/myPlot.Rd +++ /dev/null @@ -1,26 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/plotBeta.R -\name{myPlot} -\alias{myPlot} -\title{Plot the Beta distribution} -\usage{ -myPlot(alpha, beta, ...) -} -\arguments{ -\item{alpha}{first parameter of the Beta distribution} - -\item{beta}{second parameter of the Beta distribution} - -\item{\dots}{additional arguments to \code{plot}} -} -\value{ -nothing, only produces the plot as side effect -} -\description{ -This function will plot the PDF of a beta distribution -} -\examples{ -myPlot(1, 2) -myPlot(1, 1) -} -\keyword{graphics} diff --git a/man/myPlotDiff.Rd b/man/myPlotDiff.Rd index 6be1ecbf..8d949d11 100644 --- a/man/myPlotDiff.Rd +++ b/man/myPlotDiff.Rd @@ -25,9 +25,16 @@ myPlotDiff(parY, parX, cut_B = 0.2, cut_W = 0.1, shade = 1, note = 1, ...) nothing, only produces the plot as side effect } \description{ -This function will plot the PDF of a diffience between two Beta distributions +This function will plot the PDF of a difference between two Beta distributions } \examples{ -myPlotDiff(c(5, 10), c(2, 5), 0.2, 0.05, 1, 0) +myPlotDiff( + parY = c(5, 10), + parX = c(2, 5), + cut_B = 0.2, # a meaningful improvement threshold + cut_W = 0.05, # a poor improvement threshold + shade = 1, # paint the two areas under the curve, default: yes. other numbers stands for "no"; + note = 0 +) # show values of the colored area, default: yes. other numbers stands for "no"; } \keyword{graphics} diff --git a/man/plotBeta.Rd b/man/plotBeta.Rd new file mode 100644 index 00000000..f45481aa --- /dev/null +++ b/man/plotBeta.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plotBeta.R +\name{plotBeta} +\alias{plotBeta} +\title{Plot the Beta distribution} +\usage{ +plotBeta(alpha, beta, ...) +} +\arguments{ +\item{alpha}{(\code{number}):\cr first parameter of the Beta distribution} + +\item{beta}{(\code{number}):\cr second parameter of the Beta distribution} +} +\value{ +A beta distribution density plot +} +\description{ +This function will plot the PDF of a beta distribution +} +\examples{ +plotBeta(alpha = 4, beta = 5) +plotBeta(alpha = 1, beta = 1) +} +\keyword{graphics} diff --git a/tests/testthat/test-plotBeta.R b/tests/testthat/test-plotBeta.R new file mode 100644 index 00000000..f795c0eb --- /dev/null +++ b/tests/testthat/test-plotBeta.R @@ -0,0 +1,9 @@ +# plotBetaDist ---- +test_that("plotBeta gives a correct result", { + result <- plotBeta(alpha = 4, beta = 5) + expect_equal(length(result$data$grid), 1000) + expect_numeric(result$data$grid) + expect_numeric(result$data$xticks) + expect_numeric(result$data$density) + expect_identical(result$labels$title, "Beta density with alpha = 4 and beta = 5 parameters.") +}) diff --git a/tests/testthat/test-predprob.R b/tests/testthat/test-predprob.R index efd9d462..413ba6ba 100644 --- a/tests/testthat/test-predprob.R +++ b/tests/testthat/test-predprob.R @@ -34,7 +34,7 @@ test_that("predprob gives an error when x is a numeric", { p = 0.6, thetaT = 0.9, parE = c(0.6, 0.4) - ), "is not a multiple of replacement length") + ), "Must have length 1") }) test_that("predprob gives an error when K columns of weights exceed rows parE", { diff --git a/vignettes/introduction.Rmd b/vignettes/introduction.Rmd index e2b7a214..743a1ba8 100644 --- a/vignettes/introduction.Rmd +++ b/vignettes/introduction.Rmd @@ -143,6 +143,7 @@ The first time you use `phase1b` on your own computer you will need to download and install it, however, subsequent use will only require calling of the R-package. ```{r install-phase1b, eval=FALSE} install.packages("phase1b") +install.packages("ggplot2") ``` # Getting started {#getting-started} @@ -153,6 +154,7 @@ following the instructions in the [Installation section](#installation) with the following command: ```{r load} library(phase1b) +library(ggplot2) ``` Once loaded, you will have access to all of the functions in the `phase1b` R-package. To see a list of all of the functions in the `phase1b` R-package, @@ -644,9 +646,8 @@ a PET-CR rate of 68.75%, then our posterior updating based on the prior distribu observed data would lead to a $\text{Beta}(60.75,29.25)$ posterior distribution for the novel combination PET-CR rate. - -Alternatively, we could use the R-package function `myPlot()` to achieve the -same result, see Figure~\@ref(fig:ex1-prior_myPlot). +Alternatively, we could use the R-package function `plotBeta()` to achieve the +same result, see Figure~\@ref(fig:ex1-prior_plotBeta1) and Figure~\@ref(fig:ex1-prior_plotBeta2). ```{r, echo = FALSE} ex1_prior_myplot_cap <- paste( @@ -655,18 +656,24 @@ ex1_prior_myplot_cap <- paste( "control, and a $\text{Beta}(5.75,4.25)$ prior is used for the novel", "combination treatment group." ) +ex1_prior_myplot_cap <- paste( + "The prior distributions for the historical control and novel combination", + "treatment group. A $\text{Beta}(75,75)$ prior is used for the historical", + "control, and a $\text{Beta}(5.75,4.25)$ prior is used for the novel", + "combination treatment group." +) ``` -```{r ex1-prior_myPlot, echo=TRUE, fig.cap = ex1_prior_myplot_cap} -myPlot(75, 75, col = "darkgrey", ylim = c(0, 10), xlab = "PET-CR Rate", lwd = 2) -par(new = TRUE) -myPlot(5.75, 4.25, col = "deepskyblue", ylim = c(0, 10), lwd = 2, xlab = "") -legend("topright", c("Hist. Control", "Novel Combo"), - lwd = 2, lty = 1, - col = c("darkgrey", "deepskyblue"), bty = "n" -) +```{r ex1_prior_plotBeta, echo=TRUE, fig.cap = ex1_prior_myplot_cap} +plotBeta(alpha = 75, beta = 75, col = "darkgrey", ylim = c(0, 10), xlab = "PET-CR Rate", lwd = 2) ``` +For the Novel combination, the following posterior has been updated. +```{r ex1_prior_plotBeta2, fig.cap = ex1_prior_myplot_cap} +plotBeta(5.75, 4.25, col = "deepskyblue", ylim = c(0, 10), lwd = 2, xlab = "") +``` + + The following code allows the reader to visualize the posterior distribution for the PET-CR rate against the posterior distribution of the historical control. @@ -763,7 +770,7 @@ results <- ocPostprob( tL = 0.6, tU = 0.8, parE = c(1, 1), - sim = 10000, + sim = 100, wiggle = FALSE, nnF = c(10, 20, 30) )