Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qualify namespaces in tests to avoid library() calls #332

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions tests/testthat.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
library(testthat)
library(gsDesign)
library(gsDesign2)
library(dplyr)

test_check("gsDesign2")
42 changes: 21 additions & 21 deletions tests/testthat/old_function/AHR_.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,37 +189,37 @@ AHR_ <- function(enrollRates = tibble::tibble(
events <- NULL
for (s in strata) {
# subset to stratum
enroll <- enrollRates %>% filter(Stratum == s)
fail <- failRates %>% filter(Stratum == s)
enroll <- enrollRates %>% dplyr::filter(Stratum == s)
fail <- failRates %>% dplyr::filter(Stratum == s)
# Control events
enrollc <- enroll %>% mutate(rate = rate * Qc)
enrollc <- enroll %>% dplyr::mutate(rate = rate * Qc)
control <- eEvents_df_(enrollRates = enrollc, failRates = fail, totalDuration = td, simple = FALSE)
# Experimental events
enrolle <- enroll %>% mutate(rate = rate * Qe)
fre <- fail %>% mutate(failRate = failRate * hr)
enrolle <- enroll %>% dplyr::mutate(rate = rate * Qe)
fre <- fail %>% dplyr::mutate(failRate = failRate * hr)
experimental <- eEvents_df_(enrollRates = enrolle, failRates = fre, totalDuration = td, simple = FALSE)
# Combine control and experimental; by period recompute HR, events, information
events <-
rbind(
control %>% mutate(Treatment = "Control"),
experimental %>% mutate(Treatment = "Experimental")
control %>% dplyr::mutate(Treatment = "Control"),
experimental %>% dplyr::mutate(Treatment = "Experimental")
) %>%
arrange(t, Treatment) %>%
ungroup() %>%
group_by(t) %>%
summarize(
dplyr::arrange(t, Treatment) %>%
dplyr::ungroup() %>%
dplyr::group_by(t) %>%
dplyr::summarize(
Stratum = s, info = (sum(1 / Events))^(-1),
Events = sum(Events), HR = last(failRate) / first(failRate)
Events = sum(Events), HR = dplyr::last(failRate) / dplyr::first(failRate)
) %>%
rbind(events)
}
rval <- rbind(
rval,
events %>%
mutate(Time = td, lnhr = log(HR), info0 = Events * Qc * Qe) %>%
ungroup() %>%
group_by(Time, Stratum, HR) %>%
summarize(
dplyr::mutate(Time = td, lnhr = log(HR), info0 = Events * Qc * Qe) %>%
dplyr::ungroup() %>%
dplyr::group_by(Time, Stratum, HR) %>%
dplyr::summarize(
t = min(t),
Events = sum(Events),
info0 = sum(info0),
Expand All @@ -231,14 +231,14 @@ AHR_ <- function(enrollRates = tibble::tibble(
if (!simple) {
return(
rval %>%
select(c("Time", "Stratum", "t", "HR", "Events", "info", "info0")) %>%
group_by(Time, Stratum) %>%
arrange(t, .by_group = TRUE)
dplyr::select(c("Time", "Stratum", "t", "HR", "Events", "info", "info0")) %>%
dplyr::group_by(Time, Stratum) %>%
dplyr::arrange(t, .by_group = TRUE)
)
}
return(rval %>%
group_by(Time) %>%
summarize(
dplyr::group_by(Time) %>%
dplyr::summarize(
AHR = exp(sum(log(HR) * Events) / sum(Events)),
Events = sum(Events),
info = sum(info),
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/old_function/eAccrual_.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ eAccrual_ <- function(x = 0:24,
# check input enrollment rate assumptions
if(!is.numeric(x)){stop("gsDesign2: x in `eAccrual()` must be a strictly increasing non-negative numeric vector")}
if(!min(x) >= 0){stop("gsDesign2: x in `eAccrual()` must be a strictly increasing non-negative numeric vector")}
if(!min(lead(x,default=max(x)+1) - x) > 0){stop("gsDesign2: x in `eAccrual()` must be a strictly increasing non-negative numeric vector")}
if(!min(dplyr::lead(x,default=max(x)+1) - x) > 0){stop("gsDesign2: x in `eAccrual()` must be a strictly increasing non-negative numeric vector")}

# check enrollment rate assumptions
if(!is.data.frame(enrollRates)){stop("gsDesign2: enrollRates in `eAccrual()` must be a data frame")}
Expand All @@ -79,7 +79,7 @@ eAccrual_ <- function(x = 0:24,
xvals <- sort(unique(c(x,cumsum(enrollRates$duration))))
# make a tibble
xx <- tibble::tibble(x=xvals,
duration= xvals - lag(xvals,default = 0),
duration= xvals - dplyr::lag(xvals,default = 0),
rate=ratefn(xvals), # enrollment rates at points (right continuous)
eAccrual=cumsum(rate*duration) # expected accrual
)
Expand Down
46 changes: 23 additions & 23 deletions tests/testthat/old_function/eEvents_df_.R
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ NULL
#' gsDesign2:::eEvents_df_(totalDuration = .5)
#' # Single time period example
#' gsDesign2:::eEvents_df_(
#' enrollRates = tibble(duration = 10, rate = 10),
#' failRates = tibble(duration = 100, failRate = log(2) / 6, dropoutRate = .01),
#' enrollRates = tibble::tibble(duration = 10, rate = 10),
#' failRates = tibble::tibble(duration = 100, failRate = log(2) / 6, dropoutRate = .01),
#' totalDuration = 22,
#' simple = FALSE
#' )
#' # Single time period example, multiple enrolment periods
#' gsDesign2:::eEvents_df_(
#' enrollRates = tibble(duration = c(5, 5), rate = c(10, 20)),
#' failRates = tibble(duration = 100, failRate = log(2) / 6, dropoutRate = .01),
#' enrollRates = tibble::tibble(duration = c(5, 5), rate = c(10, 20)),
#' failRates = tibble::tibble(duration = 100, failRate = log(2) / 6, dropoutRate = .01),
#' totalDuration = 22,
#' simple = FALSE
#' )
Expand Down Expand Up @@ -165,18 +165,18 @@ eEvents_df_ <- function(enrollRates = tibble::tibble(
failRate = failRates$failRate,
dropoutRate = failRates$dropoutRate
)
df_2 <- if (last(cumsum(failRates$duration)) < totalDuration) df_2[-nrow(df_2), ] else df_2[df_2$startEnroll > 0, ] # we will use start of failure rate periods repeatedly below
df_2 <- if (dplyr::last(cumsum(failRates$duration)) < totalDuration) df_2[-nrow(df_2), ] else df_2[df_2$startEnroll > 0, ] # we will use start of failure rate periods repeatedly below
startFail <- c(0, cumsum(failRates$duration))
# Step function to define failure rates over time
sf.failRate <- stepfun(startFail,
c(0, failRates$failRate, last(failRates$failRate)),
c(0, failRates$failRate, dplyr::last(failRates$failRate)),
right = FALSE
)
# Step function to define dropout rates over time
sf.dropoutRate <- stepfun(startFail,
c(
0, failRates$dropoutRate,
last(failRates$dropoutRate)
dplyr::last(failRates$dropoutRate)
),
right = FALSE
)
Expand All @@ -197,25 +197,25 @@ eEvents_df_ <- function(enrollRates = tibble::tibble(
)
# Put everything together as laid out in vignette
# "Computing expected events by interval at risk"
df_join <- full_join(df_1, df_2, by = c("startEnroll", "endFail")) %>%
arrange(endFail) %>%
mutate(
endEnroll = lag(startEnroll, default = as.numeric(totalDuration)),
startFail = lag(endFail, default = 0),
df_join <- dplyr::full_join(df_1, df_2, by = c("startEnroll", "endFail")) %>%
dplyr::arrange(endFail) %>%
dplyr::mutate(
endEnroll = dplyr::lag(startEnroll, default = as.numeric(totalDuration)),
startFail = dplyr::lag(endFail, default = 0),
duration = endEnroll - startEnroll,
failRate = sf.failRate(startFail),
dropoutRate = sf.dropoutRate(startFail),
enrollRate = sf.enrollRate(startEnroll),
q = exp(-duration * (failRate + dropoutRate)),
Q = lag(cumprod(q), default = 1)
Q = dplyr::lag(cumprod(q), default = 1)
) %>%
arrange(desc(startFail)) %>%
mutate(
dplyr::arrange(dplyr::desc(startFail)) %>%
dplyr::mutate(
g = enrollRate * duration,
G = lag(cumsum(g), default = 0)
G = dplyr::lag(cumsum(g), default = 0)
) %>%
arrange(startFail) %>%
mutate(
dplyr::arrange(startFail) %>%
dplyr::mutate(
d = ifelse(failRate == 0, 0, Q * (1 - q) * failRate / (failRate + dropoutRate)),
nbar = ifelse(failRate == 0, 0,
G * d + (failRate * Q * enrollRate) / (failRate + dropoutRate) * (duration - (1 - q) / (failRate + dropoutRate))
Expand All @@ -225,12 +225,12 @@ eEvents_df_ <- function(enrollRates = tibble::tibble(
return(as.numeric(sum(df_join$nbar)))
}
df_join %>%
transmute(
dplyr::transmute(
t = endFail, failRate = failRate, Events = nbar,
startFail = sf.startFail(startFail)
) %>%
group_by(startFail) %>%
summarize(failRate = first(failRate), Events = sum(Events)) %>%
mutate(t = startFail) %>%
select("t", "failRate", "Events")
dplyr::group_by(startFail) %>%
dplyr::summarize(failRate = dplyr::first(failRate), Events = sum(Events)) %>%
dplyr::mutate(t = startFail) %>%
dplyr::select("t", "failRate", "Events")
}
14 changes: 7 additions & 7 deletions tests/testthat/old_function/gs_design_ahr_.R
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ gs_design_ahr_ <- function(enrollRates = tibble::tibble(
binding = FALSE,
upper = gs_b,
# Default is Lan-DeMets approximation of
upar = gsDesign(
upar = gsDesign::gsDesign(
k = 3, test.type = 1,
n.I = c(.25, .75, 1),
sfu = sfLDOF, sfupar = NULL
sfu = gsDesign::sfLDOF, sfupar = NULL
)$upper$bound,
lower = gs_b,
lpar = c(qnorm(.1), -Inf, -Inf), # Futility only at IA1
Expand Down Expand Up @@ -191,7 +191,7 @@ gs_design_ahr_ <- function(enrollRates = tibble::tibble(
tEvents_(enrollRates, failRates,
targetEvents = IF[K - i] * finalEvents, ratio = ratio,
interval = c(.01, nextTime)
) %>% mutate(theta = -log(AHR), Analysis = K - i),
) %>% dplyr::mutate(theta = -log(AHR), Analysis = K - i),
y
)
} else if (IF[K - i] > IFalt[K - i]) {
Expand Down Expand Up @@ -235,16 +235,16 @@ gs_design_ahr_ <- function(enrollRates = tibble::tibble(
tol = tol
) %>%
# Add Time, Events, AHR, N from gs_info_ahr call above
full_join(y %>% select(-c(info, info0, theta)), by = "Analysis") %>%
select(c("Analysis", "Bound", "Time", "N", "Events", "Z", "Probability", "AHR", "theta", "info", "info0")) %>%
arrange(desc(Bound), Analysis)
dplyr::full_join(y %>% dplyr::select(-c(info, info0, theta)), by = "Analysis") %>%
dplyr::select(c("Analysis", "Bound", "Time", "N", "Events", "Z", "Probability", "AHR", "theta", "info", "info0")) %>%
dplyr::arrange(dplyr::desc(Bound), Analysis)
bounds$Events <- bounds$Events * bounds$info[K] / y$info[K]
bounds$N <- bounds$N * bounds$info[K] / y$info[K]

# Document design enrollment, failure rates, and bounds
return(list(
enrollRates = enrollRates %>%
mutate(rate = rate * bounds$info[K] / y$info[K]),
dplyr::mutate(rate = rate * bounds$info[K] / y$info[K]),
failRates = failRates,
bounds = bounds
))
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/old_function/gs_design_npe_.R
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ NULL
#' theta = rep(0, 3), info = design$info0[1:3],
#' upar = design$Z[1:3], lpar = rep(-Inf, 3)
#' ) %>%
#' filter(Bound == "Upper")
#' dplyr::filter(Bound == "Upper")
#'
#' # Spending bound examples
#'
Expand Down Expand Up @@ -186,7 +186,7 @@ NULL
#'
#' # Re-use these bounds under alternate hypothesis
#' # Always use binding = TRUE for power calculations
#' upar <- (xx %>% filter(Bound == "Upper"))$Z
#' upar <- (xx %>% dplyr::filter(Bound == "Upper"))$Z
#' gsDesign2:::gs_design_npe_(
#' theta = c(.1, .2, .3), info = (1:3) * 40,
#' binding = TRUE,
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/old_function/gs_power_ahr_.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ NULL
#' library(gsDesign2)
#' library(dplyr)
#'
#' gs_power_ahr() %>% filter(abs(Z) < Inf)
#' gs_power_ahr() %>% dplyr::filter(abs(Z) < Inf)
#'
#' # 2-sided symmetric O'Brien-Fleming spending bound
#' # NOT CURRENTLY WORKING
Expand Down Expand Up @@ -93,10 +93,10 @@ gs_power_ahr_ <- function(enrollRates = tibble::tibble(
binding = FALSE,
upper = gs_b,
# Default is Lan-DeMets approximation of
upar = gsDesign(
upar = gsDesign::gsDesign(
k = length(events), test.type = 1,
n.I = events, maxn.IPlan = max(events),
sfu = sfLDOF, sfupar = NULL
sfu = gsDesign::sfLDOF, sfupar = NULL
)$upper$bound,
lower = gs_b,
lpar = c(qnorm(.1), rep(-Inf, length(events) - 1)), # Futility only at IA1
Expand All @@ -117,7 +117,7 @@ gs_power_ahr_ <- function(enrollRates = tibble::tibble(
test_upper = test_upper, test_lower = test_lower,
r = r, tol = tol
) %>%
right_join(x %>% select(-c(info, info0, theta)), by = "Analysis") %>%
select(c(Analysis, Bound, Time, Events, Z, Probability, AHR, theta, info, info0)) %>%
arrange(desc(Bound), Analysis))
dplyr::right_join(x %>% dplyr::select(-c(info, info0, theta)), by = "Analysis") %>%
dplyr::select(c(Analysis, Bound, Time, Events, Z, Probability, AHR, theta, info, info0)) %>%
dplyr::arrange(dplyr::desc(Bound), Analysis))
}
6 changes: 3 additions & 3 deletions tests/testthat/old_function/gs_power_npe_.R
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ NULL
#' library(dplyr)
#'
#' # Default (single analysis; Type I error controlled)
#' gsDesign2:::gs_power_npe_(theta = 0) %>% filter(Bound == "Upper")
#' gsDesign2:::gs_power_npe_(theta = 0) %>% dplyr::filter(Bound == "Upper")
#'
#' # Fixed bound
#' gsDesign2:::gs_power_npe_(
Expand All @@ -113,7 +113,7 @@ NULL
#' sfu = gsDesign::sfLDOF
#' )$upper$bound,
#' lpar = rep(-Inf, 3)
#' ) %>% filter(Bound == "Upper")
#' ) %>% dplyr::filter(Bound == "Upper")
#'
#' # Fixed bound with futility only at analysis 1;
#' # efficacy only at analyses 2, 3
Expand Down Expand Up @@ -192,7 +192,7 @@ NULL
#'
#' # Re-use these bounds under alternate hypothesis
#' # Always use binding = TRUE for power calculations
#' upar <- (xx %>% filter(Bound == "Upper"))$Z
#' upar <- (xx %>% dplyr::filter(Bound == "Upper"))$Z
#' gsDesign2:::gs_power_npe_(
#' theta = c(.1, .2, .3),
#' info = (1:3) * 40,
Expand Down
14 changes: 6 additions & 8 deletions tests/testthat/test-developer-ahr.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
source_files <- list.files("./old_function/", "*.R$")
sapply(paste0("./old_function/", source_files), source)

library(dplyr)

test_that("unstratified population", {
enroll_rate <- define_enroll_rate(
duration = c(2, 10, 4, 4, 8),
Expand All @@ -21,11 +19,11 @@ test_that("unstratified population", {
total_duration = c(15, 30)
)
x2 <- AHR_( # old version
enrollRates = enroll_rate %>% rename(Stratum = stratum),
failRates = fail_rate %>% rename(Stratum = stratum, failRate = fail_rate, dropoutRate = dropout_rate),
enrollRates = enroll_rate %>% dplyr::rename(Stratum = stratum),
failRates = fail_rate %>% dplyr::rename(Stratum = stratum, failRate = fail_rate, dropoutRate = dropout_rate),
totalDuration = c(15, 30)
) %>%
rename(time = Time, ahr = AHR, event = Events)
dplyr::rename(time = Time, ahr = AHR, event = Events)
expect_equal(as.data.frame(x1), as.data.frame(x2))
})

Expand All @@ -48,10 +46,10 @@ test_that("stratified population", {
total_duration = c(15, 30)
)
x2 <- AHR_( # old version
enrollRates = enroll_rate %>% rename(Stratum = stratum),
failRates = fail_rate %>% rename(Stratum = stratum, failRate = fail_rate, dropoutRate = dropout_rate),
enrollRates = enroll_rate %>% dplyr::rename(Stratum = stratum),
failRates = fail_rate %>% dplyr::rename(Stratum = stratum, failRate = fail_rate, dropoutRate = dropout_rate),
totalDuration = c(15, 30)
) %>%
rename(time = Time, ahr = AHR, event = Events)
dplyr::rename(time = Time, ahr = AHR, event = Events)
expect_equal(as.data.frame(x1), as.data.frame(x2))
})
6 changes: 2 additions & 4 deletions tests/testthat/test-developer-expected_event.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
library(dplyr)

source_files <- list.files("./old_function/", "*.R$")
sapply(paste0("./old_function/", source_files), source)

Expand All @@ -16,8 +14,8 @@ test_that("expected event vs gsDesign", {
T = total_duration
)$d
x2 <- eEvents_df_( # gsDesign2 old version
enrollRates = enroll_rate %>% rename(Stratum = stratum),
failRates = fail_rate %>% rename(Stratum = stratum, failRate = fail_rate, dropoutRate = dropout_rate),
enrollRates = enroll_rate %>% dplyr::rename(Stratum = stratum),
failRates = fail_rate %>% dplyr::rename(Stratum = stratum, failRate = fail_rate, dropoutRate = dropout_rate),
total_duration,
simple = TRUE
)
Expand Down
Loading