Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

109 my plot diff #110

Open
wants to merge 52 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
4f8d4c8
Merge remote-tracking branch 'origin/HEAD'
audreyyeoCH May 17, 2024
dff2ff6
delete helpers
audreyyeoCH May 21, 2024
37561fc
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] May 21, 2024
ab9ba06
clean
audreyyeoCH May 24, 2024
ab51030
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] May 24, 2024
008502d
clean
audreyyeoCH May 24, 2024
3bc1cf1
"clean+"
audreyyeoCH May 24, 2024
34a2313
empty
audreyyeoCH May 24, 2024
0cd95a7
examples and test there
audreyyeoCH May 26, 2024
51db59a
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] May 26, 2024
df9a7ca
clean
audreyyeoCH May 26, 2024
bc6dac2
Mergen"
audreyyeoCH May 26, 2024
bd327cc
NAMESPACE stuff
audreyyeoCH May 26, 2024
e25e4fd
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] May 26, 2024
c6ec5cc
empty
audreyyeoCH May 27, 2024
fe22df6
add rmarkdown to DESCRIPTIOPM
audreyyeoCH May 27, 2024
f1a2978
Merge branch 'main' into 94_helpers.R
audreyyeoCH May 27, 2024
95a6080
clean
audreyyeoCH May 27, 2024
cc665fc
Merge branch 'main' into 92_boundsPostprob
audreyyeoCH May 27, 2024
670558d
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] May 27, 2024
4f3c785
Merge remote-tracking branch 'origin/HEAD'
audreyyeoCH May 27, 2024
947e1cb
Merge branch 'main' of github.com:Genentech/phase1b
audreyyeoCH May 28, 2024
9d8a2b3
Merge branch 'main' into 92_boundsPostprob
audreyyeoCH May 31, 2024
42595d7
Merge branch 'main' of github.com:Genentech/phase1b
audreyyeoCH Jun 2, 2024
62611fa
Merge branch 'main' into 94_helpers.R
audreyyeoCH Jun 2, 2024
a29fd0f
should pass CMD checks
audreyyeoCH Jun 2, 2024
b9c7abd
Merge branch '94_helpers.R'
audreyyeoCH Jun 3, 2024
fe3f37f
see if CMD checks pass now
audreyyeoCH Jun 3, 2024
155571b
Merge branch '92_boundsPostprob'
audreyyeoCH Jun 6, 2024
d4759ef
clean
audreyyeoCH Jun 11, 2024
188027d
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Jun 11, 2024
b8cb139
Merge branch 'main' into 109_myPlotDiff
audreyyeoCH Jun 26, 2024
87f8bbe
clean
audreyyeoCH Sep 16, 2024
40b490e
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 16, 2024
0747fbb
Merge branch 'main' into 109_myPlotDiff
audreyyeoCH Sep 16, 2024
97b0685
just some first drafts
audreyyeoCH Sep 22, 2024
be2e55b
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 22, 2024
7e75e68
exame file for plotBetaDiff.R
audreyyeoCH Sep 22, 2024
e1094e1
Merge branch '109_myPlotDiff' of github.com:Genentech/phase1b into 10…
audreyyeoCH Sep 22, 2024
ec5abb8
empty
audreyyeoCH May 27, 2024
2757aab
should pass CMD checks
audreyyeoCH Jun 2, 2024
93f463a
clean
audreyyeoCH Sep 22, 2024
27cfdc9
[skip roxygen] [skip vbump] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 22, 2024
b569a7a
clean
audreyyeoCH Sep 23, 2024
db9412d
clean
audreyyeoCH May 24, 2024
6484d12
clean
audreyyeoCH Sep 23, 2024
9694d1b
Merge branch 'main' into 109_myPlotDiff
audreyyeoCH Jan 2, 2025
21f0ee4
clean
audreyyeoCH Jan 2, 2025
f922d46
clean
audreyyeoCH Jan 2, 2025
01b8185
plot and examples added
audreyyeoCH Jan 3, 2025
c475a98
for PR review before .svg files for test
audreyyeoCH Jan 3, 2025
3a65c36
[skip style] [skip vbump] Restyle files
github-actions[bot] Jan 6, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ coverage.*
.vscode/
.rds
node_modules
vintage_NAMESPACE
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(myPlotDiff)
export(oc2)
export(oc3)
export(ocPostprob)
Expand All @@ -18,6 +17,7 @@ export(ocRctPredprobDist)
export(pbetaMix)
export(pbetadiff)
export(plotBeta)
export(plotBetaDiff)
export(plotBounds)
export(plotDecision)
export(plotOc)
Expand Down
6 changes: 3 additions & 3 deletions R/boundsPostprob.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' This function is used to identify the efficacy and futility
#' boundaries based on the following rules:
audreyyeoCH marked this conversation as resolved.
Show resolved Hide resolved
#' Efficacy boundary: find minimum x (xU) where Pr(RR > p1 |x, n, a, b) >= tU and
#' Efficacy boundary: find minimum x (xU) where Pr(RR > p1 | x, n, a, b) >= tU and
#' Futility boundary: find maximum x (xL) where Pr(RR < p0 | x, n, a, b) >= tL
#'
#' @inheritParams postprob
Expand Down Expand Up @@ -48,12 +48,12 @@ boundsPostprob <- function(looks, p0, p1 = p0, tL, tU, parE = c(1, 1), weights)
xL <- NA
xU <- NA
for (x in 0:n) {
postp_fut <- 1 - postprob(x, n, p0, parE, weights) # futility look
postp_fut <- 1 - postprob(x = x, n = n, p = p0, parE = parE, weights = weights) # futility look
if (postp_fut >= tL) { # Rule is P(RR < p0) > tL
postL <- postp_fut
xL <- x
}
postp_eff <- postprob(x, n, p1, parE, weights) # efficacy look
postp_eff <- postprob(x = x, n = n, p = p1, parE = parE, weights = weights) # efficacy look
if (postp_eff >= tU) { # Rule is P(RR > p1) > tU
postU <- postp_eff
xU <- x
Expand Down
196 changes: 73 additions & 123 deletions R/plotBeta.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@
#'
#' This function will plot the PDF of a beta distribution
#'
#' @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/plotBeta.R
#' @export
#' @keywords graphics
plotBeta <- function(alpha, beta) {
plotBeta <- function(alpha, beta, ...) {
x_support <- seq(from = 0, to = 1, length = 1000)
data <- data.frame(
grid = x_support,
Expand All @@ -30,141 +30,91 @@ plotBeta <- function(alpha, beta) {
ggplot2::scale_x_continuous(labels = scales::percent_format())
}

#' Plot Diff Between two Beta distributions
#' Plot difference 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
#' @param cut_B a meaningful improvement threshold
#' @param cut_W a poor improvement throshold
#' @param shade paint the two areas under the curve, default value=1 as "yes". other numbers stands for "no";
#' @param note show values of the colored area, default value=1 as "yes". other numbers stands for "no"
#' @param \dots additional arguments to \code{plot}
#' @return nothing, only produces the plot as side effect
#' @typed parY : numeric
#' non-negative parameters of the treatment Beta distribution.
#' @typed parX : numeric
#' non-negative parameters of the historical control Beta distribution
#' @typed cut_B : number
#' a meaningful improvement threshold, the lower boundary of a meaningfully improvement in response rate
#' @typed cut_W : number
#' a poor improvement threshold, the upper boundary of a meaningfully poor improvement in response rate
#' @typed shade : flag
#' paint the two areas under the curve, default value = TRUE
#' @typed note : flag
#' show values of the colored area, default value = TRUE
#' @typed ... :
#' additional arguments to `ggplot()`
#' @return a ggplot object
#'
#' @example examples/myPlotDiff.R
#' @example examples/plotBetaDiff.R
#'
#' @importFrom graphics par axis polygon mtext
#' @importFrom stats integrate
#'
#' @export
#' @keywords graphics
myPlotDiff <- function(parY, # parameters of phase Ib trial;
parX, # parameters of HC;
cut_B = 0.20, # a meaningful improvement threshold;
cut_W = 0.1, # a poor improvement threshold;
shade = 1, # paint the two areas under the curve, default: yes. other numbers stands for "no";
note = 1, # show values of the colored area, default: yes. other numbers stands for "no";
...) {
if (note == 1) {
graphics::par(mar = c(5, 15, 1, 15) + .1)
} else {
graphics::par(mar = c(5, 5, 1, 5) + .1)
}
grid <- seq(from = -0.5, to = 0.75, length = 1000)
xticks <- seq(from = -1, to = 1, by = 0.25)



graphics::plot(
x = grid,
y = dbetadiff(grid, parY = parY, parX = parX),
ylab = "",
xaxt = "n",
yaxt = "n",
type = "l",
xaxs = "i",
yaxs = "i",
...
plotBetaDiff <- function(parY, # parameters of experimental arm
parX, # parameters of control or SOC
Go_cut = 0.20, # a meaningful improvement threshold
Stop_cut = 0.1, # a poor improvement threshold
shade = TRUE, # paint the two areas under the curve
note = TRUE) { # show values of the colored area
diff <- seq(from = -1, to = 1, length = 1000)
data <- data.frame(
grid = diff,
density = dbetadiff(z = diff, parY = parY, parX = parX)
)
data$Stop <- ifelse(diff > -1 & diff < Stop_cut, TRUE, FALSE)
data$Go <- ifelse(diff > Go_cut & diff < 1, TRUE, FALSE)

graphics::axis(
side = 1, at = xticks,
labels =
paste(ifelse(xticks >= 0, "+", ""),
xticks * 100, "%",
sep = ""
)
Go_auc <- integrate(
f = dbetadiff,
parY = parY,
parX = parX,
lower = Go_cut, # Calculate probability of Go, if difference was at least `Go_cut`.
upper = 1
)
Stop_auc <- integrate(
f = dbetadiff,
parY = parY,
parX = parX,
lower = -1,
upper = Stop_cut # Calculate probability of Stop, if difference was at most `Stop_cut`.
)

## now color the go / stop prob areas

if (shade == 1) {
## first stop:
stopGrid <- grid[grid <= cut_W]
nStop <- length(stopGrid)

graphics::polygon(
x =
c(
stopGrid,
rev(stopGrid)
),
y =
c(
rep(0, nStop),
dbetadiff(rev(stopGrid), parY = parY, parX = parX)
),
col = "red"
)

A_value <- stats::integrate(
f = dbetadiff,
parY = parY,
parX = parX,
lower = -1,
upper = cut_W
)
if (note == 1) {
graphics::mtext(
paste("Prob(diff< ", round(cut_W * 100), "%)=",
sprintf("%1.2f%%", 100 * as.numeric(A_value$value)),
sep = ""
),
side = 2, line = 1, las = 1, cex = 1
)
}

## then go:
goGrid <- grid[grid >= cut_B]
nGo <- length(goGrid)

graphics::polygon(
x =
c(
goGrid,
rev(goGrid)
),
y =
c(
rep(0, nGo),
dbetadiff(rev(goGrid), parY = parY, parX = parX)
),
col = "green"
)

B_value <- stats::integrate(
f = dbetadiff,
parY = parY,
parX = parX,
lower = cut_B,
upper = 1
)
Go_label <- paste("P(Go) is", round(Go_auc$value * 100, digits = 2), "%")
Stop_label <- paste("P(S) is", round(Stop_auc$value * 100, digits = 2), "%")
plot_title <- paste("According to Beta difference density", Go_label, "and", Stop_label)

if (note == 1) {
graphics::mtext(
paste(
sprintf("%1.2f%%", 100 * as.numeric(B_value$value)),
"=Prob(diff> ",
round(cut_B * 100), "%)",
sep = ""
),
side = 4,
line = 1,
las = 1,
cex = 1
)
}
if (shade == TRUE) {
pbetadiff_plot <- ggplot2::ggplot(data = data, aes(x = grid, y = density)) +
ggplot2::geom_line(colour = "#888888") +
geom_area(
data = data[data$grid < Stop_cut, ], fill = "#FF0046",
mapping = aes(x = ifelse(grid < 0.2 & grid < 0.5, grid, 0))
) +
geom_area(
data = data[data$grid > Go_cut, ], fill = "#009E73",
mapping = aes(x = ifelse(grid > 0.3, grid, 0))
) +
xlab("Difference between treatment") +
ggplot2::ylab(quote(f(x))) +
ggplot2::ggtitle(plot_title)
} else {
pbetadiff_plot <- ggplot2::ggplot(data = data, aes(x = grid, y = density)) +
ggplot2::geom_line(colour = "#888888") +
xlab("Difference between treatment") +
ggplot2::ylab(quote(f(x))) +
ggplot2::ggtitle(plot_title)
}
if (note == TRUE) {
pbetadiff_plot <- pbetadiff_plot +
ggplot2::annotate("text", x = -0.5, y = 3.75, size = 5, label = Stop_label, colour = "#FF0046") +
ggplot2::annotate("text", x = -0.5, y = 3.25, size = 5, label = Go_label, colour = "#009E73")
}
pbetadiff_plot
}
8 changes: 0 additions & 8 deletions examples/myPlotDiff.R

This file was deleted.

1 change: 1 addition & 0 deletions examples/plotBeta.R
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# plotBeta
plotBeta(alpha = 4, beta = 5)
plotBeta(alpha = 1, beta = 1)
23 changes: 23 additions & 0 deletions examples/plotBetaDiff.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# The beta distribution and acceptable bounds for
# a meaningful improvement of 0.20 and worsening of 0.1
parX <- c(1, 52) # prior parameters of experimental arm
parY <- c(5.5, 20.5) # prior parameters of control or SOC
plotBetaDiff(
parY = parY,
parX = parX,
Go_cut = 0.3,
Stop_cut = 0.1, # below a difference of 10%, is an unsuccesful trial
shade = TRUE,
note = TRUE
)


# a larger Go_cut with uniform prior
plotBetaDiff(
parY = c(1, 1), # prior parameters for experimental arm
parX = c(1, 1), # prior parameters for control or SOC arm
Go_cut = 0.3,
Stop_cut = 0.1, # below a difference of 10%, is an unsuccesful trial
shade = TRUE,
note = TRUE
)
4 changes: 4 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ gelman
Gelman
generalizable
geq
ggplot
grayzone
grey
Gsponer
Expand Down Expand Up @@ -201,6 +202,8 @@ renewcommand
reproducibility
responder
responders
roxygen
Roxygen
Sabanes
sabanes
Sabanés
Expand Down Expand Up @@ -243,6 +246,7 @@ USUBJID
VAD
vanillaBayes
vanillaPP
vbump
Vehtari
WeightedBayes
weightedBetaPrior
Expand Down
2 changes: 1 addition & 1 deletion man/boundsPostprob.Rd

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

40 changes: 0 additions & 40 deletions man/myPlotDiff.Rd

This file was deleted.

Loading