Skip to content

Commit

Permalink
update wlr.counting_process following the unsuccessful test in `tes…
Browse files Browse the repository at this point in the history
…t-unvalidated_wlr`
  • Loading branch information
LittleBeannie committed Sep 10, 2024
1 parent 09e1848 commit c9620d3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
9 changes: 8 additions & 1 deletion R/counting_process.R
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,14 @@ counting_process <- function(x, arm) {
setDF(ans)
class(ans) <- c("counting_process", class(ans))

attr(ans, "ratio") <- attributes(x)$ratio
# if `x` is generated by sim_pw_surv
if ("generate_by_simpwsurv" %in% names(attributes(x))) {
ratio <- attributes(x)$ratio
} else {
# if not, calcualte the emperical ratio
ratio <- sum(x$treatment == arm) / sum(x$treatment != arm)
}
attr(ans, "ratio") <- ratio

return(ans)
}
9 changes: 8 additions & 1 deletion R/wlr.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@
#' # Users can specify the randomization ratio to calculate the statistical information under H0
#' x |> wlr(weight = fh(rho = 0, gamma = 0.5), ratio = 2)
#'
#' x |>
#' counting_process(arm = "experimental") |>
#' wlr(weight = fh(rho = 0, gamma = 0.5), ratio = 2)
#'
#' # If users don't provide the randomization ratio, we will calculate the emperical ratio
#' x |> wlr(weight = fh(rho = 0, gamma = 0.5))
#'
Expand All @@ -129,7 +133,7 @@ wlr <- function(data, weight, return_variance = FALSE, ratio = NULL) {
wlr.tte_data <- function(data, weight, return_variance = FALSE, ratio = NULL) {
# if the `data` is NOT generated by sim_pw_surv
# - if user input the randomization ratio, we will directly take its values
# - otherwise, we calculate the emperical ratio
# - otherwise, we calculate the empirical ratio
if (!"generate_by_simpwsurv" %in% names(attributes(data))) {
if (is.null(ratio)) {
ratio <- sum(data$treatment == "experimental") / sum(data$treatment == "control")
Expand All @@ -148,6 +152,9 @@ wlr.tte_data <- function(data, weight, return_variance = FALSE, ratio = NULL) {
wlr.counting_process <- function(data, weight, return_variance = FALSE, ratio = NULL) {
x <- data

if (is.null(ratio)) {
ratio <- attributes(data)$ratio
}
q_e <- ratio / (1 + ratio)
q_c <- 1 - q_e

Expand Down
14 changes: 9 additions & 5 deletions man/wlr.Rd

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

0 comments on commit c9620d3

Please sign in to comment.