Skip to content

Commit 6cd3e2a

Browse files
Merge pull request #474 from Merck/473-add-comments-to-gs_info_ahr
473 add comments and update documentation of `gs_info_ahr`
2 parents 87059f9 + 6a1ca3d commit 6cd3e2a

File tree

3 files changed

+59
-68
lines changed

3 files changed

+59
-68
lines changed

R/gs_info_ahr.R

Lines changed: 49 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@
2121
#' Based on piecewise enrollment rate, failure rate, and dropout rates computes
2222
#' approximate information and effect size using an average hazard ratio model.
2323
#'
24-
#' @param enroll_rate Enrollment rates.
25-
#' @param fail_rate Failure and dropout rates.
24+
#' @param enroll_rate Enrollment rates from \code{define_enroll_rate()}.
25+
#' @param fail_rate Failure and dropout rates from \code{define_fail_rate()}.
2626
#' @param ratio Experimental:Control randomization ratio.
2727
#' @param event Targeted minimum events at each analysis.
2828
#' @param analysis_time Targeted minimum study duration at each analysis.
2929
#' @param interval An interval that is presumed to include the time at which
3030
#' expected event count is equal to targeted event.
3131
#'
32-
#' @return A data frame with columns Analysis, Time, AHR, Events, theta, info, info0.
33-
#' `info`, and `info0` contain statistical information under H1, H0, respectively.
34-
#' For analysis `k`, `Time[k]` is the maximum of `analysis_time[k]` and the
32+
#' @return A data frame with columns `analysis`, `time`, `ahr`, `event`, `theta`, `info`, `info0`.
33+
#' The columns `info` and `info0` contain statistical information under H1, H0, respectively.
34+
#' For analysis `k`, `time[k]` is the maximum of `analysis_time[k]` and the
3535
#' expected time required to accrue the targeted `event[k]`.
36-
#' `AHR` is the expected average hazard ratio at each analysis.
36+
#' `ahr` is the expected average hazard ratio at each analysis.
3737
#'
3838
#' @section Specification:
3939
#' \if{latex}{
@@ -43,7 +43,7 @@
4343
#' \item Validate if inputs event and analysis_time have the same length if they are both specified.
4444
#' \item Compute average hazard ratio:
4545
#' \itemize{
46-
#' \item If analysis_time is specified, calculate average hazard ratio using \code{AHR()}.
46+
#' \item If analysis_time is specified, calculate average hazard ratio using \code{ahr()}.
4747
#' \item If event is specified, calculate average hazard ratio using \code{expected_time()}.
4848
#' }
4949
#' \item Return a data frame of Analysis, Time, AHR, Events, theta, info, info0.
@@ -67,8 +67,8 @@
6767
#' # Only put in targeted events
6868
#' gs_info_ahr(event = c(30, 40, 50))
6969
#' }
70-
#' # Example 2 ----
7170
#'
71+
#' # Example 2 ----
7272
#' # Only put in targeted analysis times
7373
#' gs_info_ahr(analysis_time = c(18, 27, 36))
7474
#'
@@ -79,83 +79,74 @@
7979
#' gs_info_ahr(event = c(30, 40, 50), analysis_time = c(16, 19, 26))
8080
#' gs_info_ahr(event = c(30, 40, 50), analysis_time = c(14, 20, 24))
8181
#' }
82-
gs_info_ahr <- function(
83-
enroll_rate = define_enroll_rate(
84-
duration = c(2, 2, 10),
85-
rate = c(3, 6, 9)
86-
),
87-
fail_rate = define_fail_rate(
88-
duration = c(3, 100),
89-
fail_rate = log(2) / c(9, 18),
90-
hr = c(.9, .6),
91-
dropout_rate = .001
92-
),
93-
ratio = 1, # experimental:Control randomization ratio
94-
event = NULL, # event at analyses
95-
analysis_time = NULL, # times of analyses
96-
interval = c(.01, 1000)) {
97-
# Check input values ----
82+
gs_info_ahr <- function(enroll_rate = define_enroll_rate(duration = c(2, 2, 10),
83+
rate = c(3, 6, 9)),
84+
fail_rate = define_fail_rate(duration = c(3, 100),
85+
fail_rate = log(2) / c(9, 18),
86+
hr = c(.9, .6),
87+
dropout_rate = .001),
88+
ratio = 1,
89+
event = NULL,
90+
analysis_time = NULL,
91+
interval = c(.01, 1000)) {
92+
# -------------------------- #
93+
# Check input values #
94+
# -------------------------- #
9895
check_enroll_rate(enroll_rate)
9996
check_fail_rate(fail_rate)
10097
check_enroll_rate_fail_rate(enroll_rate, fail_rate)
10198

102-
if (is.null(analysis_time) && is.null(event)) {
103-
stop("gs_info_ahr(): One of `event` and `analysis_time`
104-
must be a numeric value or vector with increasing values")
105-
}
106-
107-
n_analysis <- 0
10899
if (!is.null(analysis_time)) {
109100
check_analysis_time(analysis_time)
110-
n_analysis <- length(analysis_time)
111101
}
112102

113103
if (!is.null(event)) {
114104
check_event(event)
115-
if (n_analysis == 0) {
116-
n_analysis <- length(event)
117-
} else if (n_analysis != length(event)) {
118-
stop("gs_info_ahr(): If both event and analysis_time
119-
specified, must have same length")
105+
}
106+
107+
# at least one of `analysis_time` or `event` should be provided
108+
if (is.null(analysis_time) && is.null(event)) {
109+
stop("gs_info_ahr(): One of `event` and `analysis_time`
110+
must be a numeric value or vector with increasing values")
111+
}
112+
# if both `analysis_time` and `event` are provided, check they are of same length
113+
if (!is.null(analysis_time) && !is.null(event)) {
114+
if (length(analysis_time) != length(event)) {
115+
stop("gs_info_ahr(): If both event and analysis_time specified, must have same length")
120116
}
121117
}
122118

123-
# Check input values ----
119+
# -------------------------- #
120+
# Calc statistical info #
121+
# -------------------------- #
124122
avehr <- NULL
125123
if (!is.null(analysis_time)) {
126-
# calculate AHR, Events, info, info0 given the analysis_time
127-
avehr <- ahr(
128-
enroll_rate = enroll_rate, fail_rate = fail_rate,
129-
ratio = ratio, total_duration = analysis_time
130-
) |> select(-n)
131-
# check if the output Events is larger enough than the targeted events
124+
# calculate events given the `analysis_time`
125+
avehr <- ahr(enroll_rate = enroll_rate, fail_rate = fail_rate,
126+
ratio = ratio, total_duration = analysis_time) |> select(-n)
127+
# check if the above events >= targeted events
132128
for (i in seq_along(event)) {
133129
if (avehr$event[i] < event[i]) {
134-
avehr[i, ] <- expected_time(
135-
enroll_rate = enroll_rate, fail_rate = fail_rate,
136-
ratio = ratio, target_event = event[i],
137-
interval = interval
138-
)
130+
avehr[i, ] <- expected_time(enroll_rate = enroll_rate, fail_rate = fail_rate,
131+
ratio = ratio, target_event = event[i],
132+
interval = interval)
139133
}
140134
}
141135
} else {
142136
for (i in seq_along(event)) {
143-
avehr <- rbind(
144-
avehr,
145-
expected_time(
146-
enroll_rate = enroll_rate, fail_rate = fail_rate,
147-
ratio = ratio, target_event = event[i],
148-
interval = interval
149-
)
150-
)
137+
avehr <- rbind(avehr,
138+
expected_time(enroll_rate = enroll_rate, fail_rate = fail_rate,
139+
ratio = ratio, target_event = event[i],
140+
interval = interval))
151141
}
152142
}
153143

154-
# Compute theta ----
144+
# -------------------------- #
145+
# Tidy outputs #
146+
# -------------------------- #
155147
avehr$analysis <- seq_len(nrow(avehr))
156148
avehr$theta <- -log(avehr$ahr)
157149

158-
# Output results ----
159150
ans <- avehr[, c("analysis", "time", "event", "ahr", "theta", "info", "info0")]
160151
return(ans)
161152
}

man/gs_create_arm.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/gs_info_ahr.Rd

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)