Skip to content

Commit

Permalink
Convert wlr() to S3 generic to accept tte_data or counting_process
Browse files Browse the repository at this point in the history
  • Loading branch information
jdblischak committed Aug 20, 2024
1 parent 58d45ce commit 6821cac
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: simtrial
Type: Package
Title: Clinical Trial Simulation
Version: 0.4.1.8
Version: 0.4.1.9
Authors@R: c(
person("Keaven", "Anderson", email = "[email protected]", role = c("aut")),
person("Yujie", "Zhao", email = "[email protected]", role = c("ctb","cre")),
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method(wlr,counting_process)
S3method(wlr,default)
S3method(wlr,tte_data)
export(counting_process)
export(create_cut)
export(create_test)
Expand Down
1 change: 1 addition & 0 deletions R/cut_data_by_date.R
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,6 @@ cut_data_by_date <- function(x, cut_date) {
ans <- ans[, c("tte", "event", "stratum", "treatment")]

setDF(ans)
class(ans) <- c("tte_data", class(ans))
return(ans)
}
1 change: 1 addition & 0 deletions R/cut_data_by_event.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,6 @@
cut_data_by_event <- function(x, event) {
cut_date <- get_cut_date_by_event(x, event)
ans <- x |> cut_data_by_date(cut_date = cut_date)
class(ans) <- c("tte_data", class(ans))
return(ans)
}
29 changes: 28 additions & 1 deletion R/wlr.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

#' Weighted logrank test
#'
#' @param data Dataset that has been cut, generated by [sim_pw_surv()].
#' @param data Dataset (generated by [sim_pw_surv()]) that has been cut by
#' [counting_process()], [cut_data_by_date()], or [cut_data_by_event()].
#' @param weight Weighting functions, such as [fh()], [mb()], and
#' [early_zero()].
#' @param return_variance A logical flag that, if `TRUE`, adds columns
Expand Down Expand Up @@ -85,8 +86,34 @@
#'
#' # Example 3: WLR test with early zero wights
#' x |> wlr(weight = early_zero(early_period = 4))
#'
#' # For increased computational speed when running many WLR tests, you can
#' # pre-compute the counting_process() step first, and then pass the result of
#' # counting_process() directly to wlr()
#' x <- x |> counting_process(arm = "experimental")
#' x |> wlr(weight = fh(rho = 0, gamma = 1))
#' x |> wlr(weight = mb(delay = 4, w_max = 2))
#' x |> wlr(weight = early_zero(early_period = 4))
wlr <- function(data, weight, return_variance = FALSE) {
UseMethod("wlr", data)
}

#' @rdname wlr
#' @export
wlr.counting_process <- function(data, weight, return_variance = FALSE) {
wlr.default(data, weight, return_variance = FALSE)
}

#' @rdname wlr
#' @export
wlr.tte_data <- function(data, weight, return_variance = FALSE) {
x <- data |> counting_process(arm = "experimental")
wlr.default(x, weight, return_variance = FALSE)
}

#' @export
wlr.default <- function(data, weight, return_variance = FALSE) {
x <- data

ans <- list()
ans$method <- "WLR"
Expand Down
17 changes: 16 additions & 1 deletion man/wlr.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-unvalidated-data.table.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test_that("functions that use data.table still return a data frame", {

# cut_data_by_date()
x <- sim_pw_surv(n = 20)
expect_identical(class(cut_data_by_date(x, 5)), class_expected)
expect_identical(class(cut_data_by_date(x, 5)), c("tte_data", class_expected))

# early_zero_weight()
x <- sim_pw_surv(n = 200)
Expand Down
23 changes: 23 additions & 0 deletions tests/testthat/test-unvalidated-wlr.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
test_that("wlr() accepts tte_data and counting_process objects as input", {
# cut_data_by_event()
x <- sim_pw_surv(n = 300) |> cut_data_by_event(100)
expect_s3_class(x, "tte_data")
results_tte_data <- x |> wlr(weight = fh(0, 0.5))

x <- x |> counting_process(arm = "experimental")
expect_s3_class(x, "counting_process")
results_counting_process <- x |> wlr(weight = fh(0, 0.5))

expect_equal(results_tte_data, results_counting_process)

# cut_data_by_date()
x <- sim_pw_surv(n = 300) |> cut_data_by_date(cut_date = 300)
expect_s3_class(x, "tte_data")
results_tte_data <- x |> wlr(weight = fh(0, 0.5))

x <- x |> counting_process(arm = "experimental")
expect_s3_class(x, "counting_process")
results_counting_process <- x |> wlr(weight = fh(0, 0.5))

expect_equal(results_tte_data, results_counting_process)
})

0 comments on commit 6821cac

Please sign in to comment.