Skip to content

Commit 9f4fca0

Browse files
author
Thomas Debray
committed
minor update
1 parent c1aa7d8 commit 9f4fca0

File tree

6 files changed

+93
-56
lines changed

6 files changed

+93
-56
lines changed

R/SampleSize.R

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ sampleSize <- function(mu_list, varcov_list = NA, sigma_list = NA, cor_mat = NA,
144144

145145
# Conduct validations
146146
validate_sample_size_limits(lower = lower, upper = upper)
147-
validate_tar(TAR = TAR, n_arms = n)
147+
TAR <- derive_TAR(TAR = TAR, n_arms = n)
148148

149149

150150
# Derive the Arm Names
@@ -736,3 +736,55 @@ derive_varcov_list <- function(mu_list, sigma_list, ynames_list, varcov_list = N
736736
return(varcov_list)
737737
}
738738

739+
#' Derive Treatment Allocation Rate (TAR)
740+
#'
741+
#' This function validates and adjusts the treatment allocation rate (`TAR`) to ensure it matches
742+
#' the number of treatment arms (`n_arms`). If `TAR` is missing or NULL, it is set to a default
743+
#' vector of ones. If `TAR` is shorter than `n_arms`, missing values are replaced with ones.
744+
#' If `TAR` contains NA values, they are replaced with ones. If `TAR` exceeds `n_arms` in length
745+
#' or contains non-positive values, an error is raised.
746+
#'
747+
#' @param TAR Numeric vector. Treatment allocation rates for each arm.
748+
#' @param n_arms Numeric. The number of treatment arms that `TAR` must correspond to.
749+
#'
750+
#' @details
751+
#' - If `TAR` is NULL or missing, it is set to a vector of ones of length `n_arms`.
752+
#' - If `TAR` is shorter than `n_arms`, missing values are replaced with ones and a warning is issued.
753+
#' - If `TAR` is longer than `n_arms`, an error is raised.
754+
#' - If `TAR` contains NA values, they are replaced with ones and a warning is issued.
755+
#' - If `TAR` contains negative or zero values, an error is raised.
756+
#'
757+
#' @author
758+
#' Thomas Debray \email{[email protected]}
759+
#'
760+
#' @return A numeric vector of length `n_arms`, where any missing or NA values in `TAR`
761+
#' have been replaced with ones.
762+
derive_TAR <- function(TAR = NULL, n_arms) {
763+
764+
if (missing(TAR) || is.null(TAR)) {
765+
warning("Warning: TAR is missing or NULL. Setting TAR to a default vector of ones.")
766+
TAR <- rep(1, n_arms)
767+
}
768+
769+
# Ensure TAR has the correct length by filling missing values with 1
770+
if (length(TAR) < n_arms) {
771+
warning("Warning: TAR length is shorter than the number of arms. Missing values will be replaced with 1.")
772+
TAR <- c(TAR, rep(1, n_arms - length(TAR)))
773+
} else if (length(TAR) > n_arms) {
774+
stop("Validation Error: The length of TAR must not exceed the number of arms specified by n_arms.")
775+
}
776+
777+
# Replace any NA values with 1
778+
if (any(is.na(TAR))) {
779+
warning("Warning: NA values detected in TAR. These will be replaced with 1.")
780+
TAR[is.na(TAR)] <- 1
781+
}
782+
783+
# Validate that all values are positive
784+
if (any(TAR <= 0, na.rm = TRUE)) {
785+
stop("Validation Error: TAR must contain only positive values. Negative or zero values are not allowed.")
786+
}
787+
788+
return(TAR)
789+
}
790+

R/utils.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,8 @@ test_studies <- function(nsim, n, comp, param, param.d, arm_seed, ncores){
221221
SigmaT = as.matrix(SigmaT),SigmaR = as.matrix(SigmaR),
222222
lequi_tol = lequi.tol ,uequi_tol = uequi.tol,
223223
alpha = alpha, k = k, dropout = as.numeric(c(dropout[treat1],dropout[treat2])),
224-
typey = which(param$type_y[endp] == 1) - 1, adseq=param.d$adjust=="seq",
224+
typey = ifelse(any(param$type_y[endp] == 1),which(param$type_y[endp] == 1) - 1,-1),
225+
adseq=param.d$adjust=="seq",
225226
arm_seedT = arm_seedx[treat1], arm_seedR = arm_seedx[treat2],
226227
TART = param$TAR_list[[treat1]], TARR = param$TAR_list[[treat2]],
227228
vareq =param.d$vareq))
@@ -231,7 +232,7 @@ test_studies <- function(nsim, n, comp, param, param.d, arm_seed, ncores){
231232
SigmaT = as.matrix(SigmaT),SigmaR = as.matrix(SigmaR),
232233
lequi_tol = lequi.tol ,uequi_tol = uequi.tol,
233234
alpha = alpha, k = k, dropout = as.numeric(c(dropout[treat1],dropout[treat2])),
234-
typey = which(param$type_y[endp] == 1) - 1,
235+
typey = ifelse(any(param$type_y[endp] == 1),which(param$type_y[endp] == 1) - 1,-1),
235236
adseq = param.d$adjust=="seq",
236237
arm_seedT = arm_seedx[treat1], arm_seedR = arm_seedx[treat2],
237238
TART = param$TAR_list[[treat1]], TARR = param$TAR_list[[treat2]],

R/validation.R

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -63,34 +63,5 @@ validate_positive_definite <- function(varcov_list) {
6363
}
6464
}
6565

66-
#' Validate Treatment Allocation Rate (TAR)
67-
#'
68-
#' This function validates whether the length of the treatment allocation rate (`TAR`) matches
69-
#' the number of treatment arms (`arm_names`). If the lengths do not match, it throws an error.
70-
#'
71-
#' @param TAR Numeric vector. Treatment allocation rates for each arm.
72-
#' @param n_arms Numeric. The number of treatment arms that `TAR` must correspond to.
73-
#'
74-
#' @author
75-
#' Thomas Debray \email{[email protected]}
76-
#' @return NULL (used for validation only).
77-
validate_tar <- function(TAR = NULL, n_arms) {
78-
if (missing(TAR)) {
79-
return(NULL)
80-
}
81-
if (is.null(TAR)) {
82-
return(NULL)
83-
}
8466

85-
# Validate TAR length
86-
if (length(TAR) != n_arms) {
87-
stop("Validation Error: The length of TAR must match the number of arms specified by arm_names.")
88-
}
89-
90-
if (any(TAR <= 0, na.rm = TRUE)) {
91-
stop("Validation Error: TAR must contain only positive values. Negative or zero values are not allowed.")
92-
}
93-
94-
95-
}
9667

man/derive_TAR.Rd

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

man/print.simss.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/validate_tar.Rd

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)