Skip to content

Commit

Permalink
97_myPlot_only (#108)
Browse files Browse the repository at this point in the history
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 <danielinteractive@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 26, 2024
1 parent 799330f commit 394e197
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 75 deletions.
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repos:
args: [--style_pkg=styler, --style_fun=tidyverse_style]
- id: roxygenize
additional_dependencies:
- ggplot2
- devtools
- shiny
- checkmate
Expand Down
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ BugReports: https://github.com/genentech/phase1b/issues
Depends:
R (>= 3.6)
Suggests:
scales,
rmarkdown,
bookdown,
knitr,
Expand All @@ -32,6 +33,7 @@ Suggests:
reshape,
testthat (>= 3.0.0)
Imports:
ggplot2,
checkmate,
devtools,
lifecycle
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export(dbetaMix)
export(dbetabinom)
export(dbetabinomMix)
export(dbetadiff)
export(myPlot)
export(myPlotDiff)
export(oc2)
export(oc3)
Expand All @@ -18,6 +17,7 @@ export(ocRctPostprobDist)
export(ocRctPredprobDist)
export(pbetaMix)
export(pbetadiff)
export(plotBeta)
export(plotBounds)
export(plotDecision)
export(plotOc)
Expand Down
4 changes: 2 additions & 2 deletions R/dbetabinom.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
49 changes: 21 additions & 28 deletions R/plotBeta.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions examples/myPlot.R

This file was deleted.

9 changes: 8 additions & 1 deletion examples/myPlotDiff.R
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 2 additions & 0 deletions examples/plotBeta.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
plotBeta(alpha = 4, beta = 5)
plotBeta(alpha = 1, beta = 1)
26 changes: 0 additions & 26 deletions man/myPlot.Rd

This file was deleted.

11 changes: 9 additions & 2 deletions man/myPlotDiff.Rd

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

24 changes: 24 additions & 0 deletions man/plotBeta.Rd

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

9 changes: 9 additions & 0 deletions tests/testthat/test-plotBeta.R
Original file line number Diff line number Diff line change
@@ -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.")
})
2 changes: 1 addition & 1 deletion tests/testthat/test-predprob.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
31 changes: 19 additions & 12 deletions vignettes/introduction.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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,
Expand Down Expand Up @@ -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(
Expand All @@ -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.
Expand Down Expand Up @@ -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)
)
Expand Down

0 comments on commit 394e197

Please sign in to comment.