Skip to content

Commit 19b977e

Browse files
Update the R functions to allow outputting spending time (#602)
* Update the R folder - update `gs_design_ahr` to output spending time - update `summary` with a new argument to allow outputting spending time or not - update `as_gt` * fix the independent `as_gt` test as the gt pkg has a new release? * address John's comments * delete extra 4
1 parent ae1ed90 commit 19b977e

File tree

5 files changed

+112
-40
lines changed

5 files changed

+112
-40
lines changed

R/as_gt.R

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,17 @@ as_gt.gs_design_summary <- function(
271271
# get different default columns to display
272272
gsd_columns <- function(columns, method, x) {
273273
# set different default columns to display
274-
if (is.null(columns)) columns <- c(
275-
"Analysis", "Bound", "Z", "Nominal p",
276-
sprintf("%s at bound", switch(method, ahr = "~HR", wlr = "~wHR", rd = "~Risk difference")),
277-
"Alternate hypothesis", "Null hypothesis"
278-
)
274+
if (is.null(columns)){
275+
columns <- c(
276+
"Analysis", "Bound", "Z", "Nominal p",
277+
sprintf("%s at bound", switch(method, ahr = "~HR", wlr = "~wHR", rd = "~Risk difference")),
278+
"Alternate hypothesis", "Null hypothesis")
279+
280+
if ("Spending time" %in% names(x)) {
281+
columns <- c(columns[1:3], "Spending time", columns[4:length(columns)])
282+
}
283+
}
284+
279285
# filter the columns to display as the output: if `Probability` is selected to
280286
# output, transform it to `c("Alternate hypothesis", "Null hypothesis")`
281287
if (any(i <- columns == "Probability"))

R/gs_design_ahr.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ gs_design_ahr <- function(
337337
allout$event <- allout$event * inflac_fct
338338
allout$n <- allout$n * inflac_fct
339339

340+
340341
# Get bounds to output ----
341342
bound <- allout |>
342343
select(all_of(c(
@@ -345,6 +346,42 @@ gs_design_ahr <- function(
345346
))) |>
346347
arrange(analysis, desc(bound))
347348

349+
# Output spending time to the bounds table
350+
info0 <- (allout |> filter(bound == "upper"))$info0
351+
info <- (allout |> filter(bound == "upper"))$info
352+
info0_final <- (allout |> filter(analysis == n_analysis, bound == "upper"))$info0
353+
info_final <- (allout |> filter(analysis == n_analysis, bound == "upper"))$info
354+
355+
bound$spending_time <- NA
356+
357+
if (identical(upper, gs_spending_bound)) {
358+
359+
if (!is.null(upar$timing)) {
360+
spending_time_upper <- upar$timing
361+
} else {
362+
spending_time_upper <- info0 / info0_final
363+
}
364+
365+
366+
bound$spending_time[which(bound$bound == "upper")] <- spending_time_upper
367+
}
368+
369+
if (identical(lower, gs_spending_bound)) {
370+
if (!is.null(lpar$timing)) {
371+
spending_time_lower <- lpar$timing
372+
} else if (h1_spending) {
373+
spending_time_lower <- info / info_final
374+
} else if (!h1_spending) {
375+
spending_time_lower <- info0 / info0_final
376+
}
377+
378+
bound$spending_time[which(bound$bound == "lower")] <- spending_time_lower
379+
}
380+
381+
if (all(is.na(bound$spending_time))){
382+
bound$spending_time <- NULL
383+
}
384+
348385
# Get analysis summary to output ----
349386
analysis <- allout |>
350387
select(analysis, time, n, event, ahr, theta, info, info0, info_frac) |>

R/summary.R

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ summary.fixed_design <- function(object, ...) {
104104
#' vector is named, you only have to specify the number of digits for the
105105
#' columns you want to be displayed differently than the defaults.
106106
#' @param bound_names Names for bounds; default is `c("Efficacy", "Futility")`.
107+
#' @param display_spending_time A logical value (TRUE/FALSE) indicating if the spending time
108+
#' is summarized in the table.
107109
#'
108110
#' @export
109111
#'
@@ -256,6 +258,7 @@ summary.gs_design <- function(object,
256258
col_vars = NULL,
257259
col_decimals = NULL,
258260
bound_names = c("Efficacy", "Futility"),
261+
display_spending_time = FALSE,
259262
...) {
260263
x <- object
261264
x_bound <- x$bound
@@ -325,6 +328,12 @@ summary.gs_design <- function(object,
325328
"nominal p", "Alternate hypothesis", "Null hypothesis"
326329
)
327330

331+
if (display_spending_time) {
332+
default_decimals <- c(default_decimals[1:3], 4, default_decimals[4:length(default_decimals)])
333+
default_vars <- c(default_vars[1:3], "spending_time", default_vars[4:length(default_vars)])
334+
}
335+
336+
328337
# Filter columns and update decimal places
329338
col_decimals <- get_decimals(col_vars, col_decimals, default_vars, default_decimals)
330339

@@ -388,7 +397,8 @@ cap_names <- function(x) {
388397
map, ahr = "AHR", event = "Events", rd = "Risk difference",
389398
probability = "Alternate hypothesis", probability0 = "Null hypothesis",
390399
info_frac0 = "Information fraction", info_frac = "Information fraction",
391-
event_frac = "Event fraction"
400+
event_frac = "Event fraction",
401+
spending_time = "Spending time"
392402
)
393403
replace_names(x, map)
394404
}

man/summary.Rd

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

tests/testthat/_snaps/independent_as_gt.md

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
\bottomrule
1414
\end{tabular*}
1515
\begin{minipage}{\linewidth}
16-
\textsuperscript{\textit{1}}Power computed with average hazard ratio method.\\
16+
\vspace{.05em}
17+
\textsuperscript{\textit{1}} Power computed with average hazard ratio method.\\
1718
\end{minipage}
1819
\end{table}
1920

@@ -32,7 +33,8 @@
3233
\bottomrule
3334
\end{tabular*}
3435
\begin{minipage}{\linewidth}
35-
\textsuperscript{\textit{1}}Custom footnote.\\
36+
\vspace{.05em}
37+
\textsuperscript{\textit{1}} Custom footnote.\\
3638
\end{minipage}
3739
\end{table}
3840

@@ -51,7 +53,8 @@
5153
\bottomrule
5254
\end{tabular*}
5355
\begin{minipage}{\linewidth}
54-
\textsuperscript{\textit{1}}Power for Fleming-Harrington test FH(0, 0) (logrank) using method of Yung and Liu.\\
56+
\vspace{.05em}
57+
\textsuperscript{\textit{1}} Power for Fleming-Harrington test FH(0, 0) (logrank) using method of Yung and Liu.\\
5558
\end{minipage}
5659
\end{table}
5760

@@ -75,8 +78,9 @@
7578
\bottomrule
7679
\end{tabular*}
7780
\begin{minipage}{\linewidth}
78-
\textsuperscript{\textit{1}}One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
79-
\textsuperscript{\textit{2}}Approximate hazard ratio to cross bound.\\
81+
\vspace{.05em}
82+
\textsuperscript{\textit{1}} One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
83+
\textsuperscript{\textit{2}} Approximate hazard ratio to cross bound.\\
8084
\end{minipage}
8185
\end{table}
8286

@@ -111,8 +115,9 @@
111115
\bottomrule
112116
\end{tabular*}
113117
\begin{minipage}{\linewidth}
114-
\textsuperscript{\textit{1}}One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
115-
\textsuperscript{\textit{2}}Approximate hazard ratio to cross bound.\\
118+
\vspace{.05em}
119+
\textsuperscript{\textit{1}} One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
120+
\textsuperscript{\textit{2}} Approximate hazard ratio to cross bound.\\
116121
\end{minipage}
117122
\end{table}
118123

@@ -136,9 +141,10 @@
136141
\bottomrule
137142
\end{tabular*}
138143
\begin{minipage}{\linewidth}
139-
\textsuperscript{\textit{1}}One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
140-
\textsuperscript{\textit{2}}Approximate hazard ratio to cross bound.\\
141-
\textsuperscript{\textit{3}}wAHR is the weighted AHR.\\
144+
\vspace{.05em}
145+
\textsuperscript{\textit{1}} One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
146+
\textsuperscript{\textit{2}} Approximate hazard ratio to cross bound.\\
147+
\textsuperscript{\textit{3}} wAHR is the weighted AHR.\\
142148
\end{minipage}
143149
\end{table}
144150

@@ -173,10 +179,11 @@
173179
\bottomrule
174180
\end{tabular*}
175181
\begin{minipage}{\linewidth}
176-
\textsuperscript{\textit{1}}this table is generated by gs\_power\_wlr.\\
177-
\textsuperscript{\textit{2}}the crossing probability.\\
178-
\textsuperscript{\textit{3}}approximate weighted hazard ratio to cross bound.\\
179-
\textsuperscript{\textit{4}}wAHR is the weighted AHR.\\
182+
\vspace{.05em}
183+
\textsuperscript{\textit{1}} this table is generated by gs\_power\_wlr.\\
184+
\textsuperscript{\textit{2}} the crossing probability.\\
185+
\textsuperscript{\textit{3}} approximate weighted hazard ratio to cross bound.\\
186+
\textsuperscript{\textit{4}} wAHR is the weighted AHR.\\
180187
\end{minipage}
181188
\end{table}
182189

@@ -211,8 +218,9 @@
211218
\bottomrule
212219
\end{tabular*}
213220
\begin{minipage}{\linewidth}
214-
\textsuperscript{\textit{1}}One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
215-
\textsuperscript{\textit{2}}EF is event fraction. AHR is under regular weighted log rank test.\\
221+
\vspace{.05em}
222+
\textsuperscript{\textit{1}} One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
223+
\textsuperscript{\textit{2}} EF is event fraction. AHR is under regular weighted log rank test.\\
216224
\end{minipage}
217225
\end{table}
218226

@@ -236,7 +244,8 @@
236244
\bottomrule
237245
\end{tabular*}
238246
\begin{minipage}{\linewidth}
239-
\textsuperscript{\textit{1}}One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
247+
\vspace{.05em}
248+
\textsuperscript{\textit{1}} One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
240249
\end{minipage}
241250
\end{table}
242251

@@ -269,8 +278,9 @@
269278
\bottomrule
270279
\end{tabular*}
271280
\begin{minipage}{\linewidth}
272-
\textsuperscript{\textit{1}}One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
273-
\textsuperscript{\textit{2}}Cumulative alpha for final analysis (0.0238) is less than the full alpha (0.025) when the futility bound is non-binding. The smaller value subtracts the probability of crossing a futility bound before crossing an efficacy bound at a later analysis (0.025 - 0.0012 = 0.0238) under the null hypothesis.\\
281+
\vspace{.05em}
282+
\textsuperscript{\textit{1}} One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
283+
\textsuperscript{\textit{2}} Cumulative alpha for final analysis (0.0238) is less than the full alpha (0.025) when the futility bound is non-binding. The smaller value subtracts the probability of crossing a futility bound before crossing an efficacy bound at a later analysis (0.025 - 0.0012 = 0.0238) under the null hypothesis.\\
274284
\end{minipage}
275285
\end{table}
276286

@@ -305,9 +315,10 @@
305315
\bottomrule
306316
\end{tabular*}
307317
\begin{minipage}{\linewidth}
308-
\textsuperscript{\textit{1}}One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
309-
\textsuperscript{\textit{2}}Approximate hazard ratio to cross bound.\\
310-
\textsuperscript{\textit{3}}wAHR is the weighted AHR.\\
318+
\vspace{.05em}
319+
\textsuperscript{\textit{1}} One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
320+
\textsuperscript{\textit{2}} Approximate hazard ratio to cross bound.\\
321+
\textsuperscript{\textit{3}} wAHR is the weighted AHR.\\
311322
\end{minipage}
312323
\end{table}
313324

@@ -342,9 +353,10 @@
342353
\bottomrule
343354
\end{tabular*}
344355
\begin{minipage}{\linewidth}
345-
\textsuperscript{\textit{1}}One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
346-
\textsuperscript{\textit{2}}Approximate hazard ratio to cross bound.\\
347-
\textsuperscript{\textit{3}}wAHR is the weighted AHR.\\
356+
\vspace{.05em}
357+
\textsuperscript{\textit{1}} One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
358+
\textsuperscript{\textit{2}} Approximate hazard ratio to cross bound.\\
359+
\textsuperscript{\textit{3}} wAHR is the weighted AHR.\\
348360
\end{minipage}
349361
\end{table}
350362

@@ -379,10 +391,11 @@
379391
\bottomrule
380392
\end{tabular*}
381393
\begin{minipage}{\linewidth}
382-
\textsuperscript{\textit{1}}this table is generated by gs\_power\_wlr.\\
383-
\textsuperscript{\textit{2}}the crossing probability.\\
384-
\textsuperscript{\textit{3}}approximate weighted hazard ratio to cross bound.\\
385-
\textsuperscript{\textit{4}}wAHR is the weighted AHR.\\
394+
\vspace{.05em}
395+
\textsuperscript{\textit{1}} this table is generated by gs\_power\_wlr.\\
396+
\textsuperscript{\textit{2}} the crossing probability.\\
397+
\textsuperscript{\textit{3}} approximate weighted hazard ratio to cross bound.\\
398+
\textsuperscript{\textit{4}} wAHR is the weighted AHR.\\
386399
\end{minipage}
387400
\end{table}
388401

@@ -414,9 +427,10 @@
414427
\bottomrule
415428
\end{tabular*}
416429
\begin{minipage}{\linewidth}
417-
\textsuperscript{\textit{1}}One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
418-
\textsuperscript{\textit{2}}Approximate hazard ratio to cross bound.\\
419-
\textsuperscript{\textit{3}}wAHR is the weighted AHR.\\
430+
\vspace{.05em}
431+
\textsuperscript{\textit{1}} One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
432+
\textsuperscript{\textit{2}} Approximate hazard ratio to cross bound.\\
433+
\textsuperscript{\textit{3}} wAHR is the weighted AHR.\\
420434
\end{minipage}
421435
\end{table}
422436

@@ -451,8 +465,9 @@
451465
\bottomrule
452466
\end{tabular*}
453467
\begin{minipage}{\linewidth}
454-
\textsuperscript{\textit{1}}One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
455-
\textsuperscript{\textit{2}}wAHR is the weighted AHR.\\
468+
\vspace{.05em}
469+
\textsuperscript{\textit{1}} One-sided p-value for experimental vs control treatment. Value < 0.5 favors experimental, > 0.5 favors control.\\
470+
\textsuperscript{\textit{2}} wAHR is the weighted AHR.\\
456471
\end{minipage}
457472
\end{table}
458473

0 commit comments

Comments
 (0)