@@ -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+
0 commit comments