-
Notifications
You must be signed in to change notification settings - Fork 6
/
ahr.R
131 lines (130 loc) · 5.16 KB
/
ahr.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Copyright (c) 2024 Merck & Co., Inc., Rahway, NJ, USA and its affiliates.
# All rights reserved.
#
# This file is part of the gsDesign2 program.
#
# gsDesign2 is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#' Average hazard ratio under non-proportional hazards
#'
#' Provides a geometric average hazard ratio under
#' various non-proportional hazards assumptions for either single or multiple strata studies.
#' The piecewise exponential distribution allows a simple method to specify a distribution
#' and enrollment pattern where the enrollment, failure and dropout rates changes over time.
#'
#' @param enroll_rate An `enroll_rate` data frame with or without stratum
#' created by [define_enroll_rate()].
#' @param fail_rate A `fail_rate` data frame with or without stratum
#' created by [define_fail_rate()].
#' @param total_duration Total follow-up from start of enrollment to data
#' cutoff; this can be a single value or a vector of positive numbers.
#' @param ratio Ratio of experimental to control randomization.
#'
#' @return A data frame with `time` (from `total_duration`),
#' `ahr` (average hazard ratio), `event` (expected number of events),
#' `info` (information under given scenarios), `and` info0
#' (information under related null hypothesis) for each value of
#' `total_duration` input.
#'
#' @section Specification:
#' \if{latex}{
#' \itemize{
#' \item Validate if input enrollment rate contains stratum column.
#' \item Validate if input enrollment rate contains total duration column.
#' \item Validate if input enrollment rate contains rate column.
#' \item Validate if input failure rate contains stratum column.
#' \item Validate if input failure rate contains duration column.
#' \item Validate if input failure rate contains failure rate column.
#' \item Validate if input failure rate contains hazard ratio column.
#' \item Validate if input failure rate contains dropout rate column.
#' \item Validate if input trial total follow-up (total duration) is a non-empty vector of positive integers.
#' \item Validate if strata is the same in enrollment rate and failure rate.
#' \item Compute the proportion in each group.
#' \item Compute the expected events by treatment groups, stratum and time period.
#' \item Calculate the expected number of events for all time points in the total
#' duration and for all stratification variables.
#' \itemize{
#' \item Compute the expected events in for each strata.
#' \itemize{
#' \item Combine the expected number of events of all stratification variables.
#' \item Recompute events, hazard ratio and information under
#' the given scenario of the combined data for each strata.
#' }
#' \item Combine the results for all time points by summarizing the results by adding up the number of events,
#' information under the null and the given scenarios.
#' }
#' \item Return a data frame of overall event count, statistical information and average hazard ratio
#' of each value in total_duration.
#' \item Calculation of \code{ahr} for different design scenarios, and the comparison to the
#' simulation studies are defined in vignette/AHRVignette.Rmd.
#' }
#' }
#' \if{html}{The contents of this section are shown in PDF user manual only.}
#'
#' @importFrom data.table setDF setDT
#'
#' @export
#'
#' @examples
#' # Example: default
#' ahr()
#'
#' # Example: default with multiple analysis times (varying total_duration)
#' ahr(total_duration = c(15, 30))
#'
#' # Stratified population
#' enroll_rate <- define_enroll_rate(
#' stratum = c(rep("Low", 2), rep("High", 3)),
#' duration = c(2, 10, 4, 4, 8),
#' rate = c(5, 10, 0, 3, 6)
#' )
#' fail_rate <- define_fail_rate(
#' stratum = c(rep("Low", 2), rep("High", 2)),
#' duration = 1,
#' fail_rate = c(.1, .2, .3, .4),
#' dropout_rate = .001,
#' hr = c(.9, .75, .8, .6)
#' )
#' ahr(enroll_rate = enroll_rate, fail_rate = fail_rate, total_duration = c(15, 30))
ahr <- function(
enroll_rate = define_enroll_rate(
duration = c(2, 2, 10),
rate = c(3, 6, 9)
),
fail_rate = define_fail_rate(
duration = c(3, 100),
fail_rate = log(2) / c(9, 18),
hr = c(.9, .6),
dropout_rate = .001
),
total_duration = 30,
ratio = 1) {
res <- pw_info(
enroll_rate = enroll_rate,
fail_rate = fail_rate,
total_duration = total_duration,
ratio = ratio
)
setDT(res)
ans <- res[,
.(
ahr = exp(sum(log(hr) * event) / sum(event)),
event = sum(event),
info = sum(info),
info0 = sum(info0)
),
by = "time"
]
setDF(ans)
return(ans)
}