Skip to content

Commit 12d46ee

Browse files
Keith GoldfeldKeith Goldfeld
authored andcommitted
Adding ratio argument to function trtAssign
1 parent d0f2b77 commit 12d46ee

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,5 @@
110110
# simstudy 0.1.16
111111

112112
* Added "mixture" distribution that takes a value from an existing column with a specified probability.
113-
* Modified function `trtAssign` to improve speed performance of stratified sampling with very large numbers of strata.
113+
* Modified function trtAssign to improve speed performance of stratified sampling with very large numbers of strata.
114+
* Add argument "ratio" to function trtAssign to allow users to specify more than 1:1 randomization.

R/int_addStrataCode.R

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@
3636
# @param ncat Number of treatment categories
3737
# @return A sample draw from a stratum
3838

39-
.stratSamp <- function(nrow, ncat) {
40-
neach <- floor(nrow / ncat)
41-
distrx <- rep(c(1:ncat), each = neach)
39+
.stratSamp <- function(nrow, ncat, ratio) {
40+
41+
if (is.null(ratio)) ratio <- rep(1, ncat)
42+
43+
neach <- floor(nrow / sum(ratio))
44+
distrx <- rep(c(1:ncat), times = (neach * ratio))
4245
extra <- nrow - length(distrx)
43-
sample(c(distrx, sample(1:ncat, extra)))
46+
sample(c(distrx, sample(rep(1:ncat, times = ratio), extra)))
47+
4448
}

R/trtAssign.R

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#' @param strata vector of strings representing stratifying variables
99
#' @param grpName string representing variable name for treatment or
1010
#' exposure group
11+
#' @param ratio vector of values indicating relative proportion of group
12+
#' assignment
1113
#' @return An integer (group) ranging from 1 to length of the
1214
#' probability vector
1315
#' @seealso \code{\link{trtObserve}}
@@ -36,11 +38,14 @@
3638
#' dt5 <- trtAssign(dt, nTrt = 5, balanced = TRUE, grpName = "Group")
3739
#' dt5[, .N, keyby = .(male, Group)]
3840
#' dt5[, .N, keyby = .(Group)]
41+
#'
42+
#' dt6 <- trtAssign(dt, nTrt = 3, ratio = c(1, 2, 2), grpName = "Group")
43+
#' dt6[, .N, keyby = .(Group)]
3944
#'
4045
#' @export
4146

4247
trtAssign <- function(dtName, nTrt = 2, balanced = TRUE,
43-
strata = NULL, grpName = "trtGrp") {
48+
strata = NULL, grpName = "trtGrp", ratio = NULL) {
4449

4550
# 'declare' vars
4651

@@ -56,6 +61,11 @@ trtAssign <- function(dtName, nTrt = 2, balanced = TRUE,
5661
if (grpName %in% names(dtName)) {
5762
stop("Group name has previously been defined in data table", call. = FALSE)
5863
}
64+
if (!is.null(ratio)) {
65+
if (length(ratio) != nTrt) {
66+
stop("Number of treatments does not match specified ratio", call. = FALSE)
67+
}
68+
}
5969

6070
dt <- copy(dtName)
6171

@@ -68,21 +78,28 @@ trtAssign <- function(dtName, nTrt = 2, balanced = TRUE,
6878
}
6979

7080
dt[, .n := .N, keyby = .stratum]
71-
dtrx <- dt[, list(grpExp = .stratSamp(.n[1], nTrt)), keyby = .stratum]
81+
dtrx <- dt[, list(grpExp = .stratSamp(.n[1], nTrt, ratio)), keyby = .stratum]
7282
dt[, grpExp := dtrx$grpExp]
7383
dt[, `:=`(.stratum = NULL, .n = NULL)]
7484

75-
if (nTrt==2) dt[grpExp == 2, grpExp := 0]
85+
if (nTrt==2) dt[, grpExp := grpExp - 1]
7686
data.table::setnames(dt, "grpExp", grpName)
7787
data.table::setkeyv(dt,key(dtName))
7888

7989
} else { # balanced is FALSE - strata are not relevant
8090

81-
if (nTrt == 2) {
82-
formula <- .5
83-
} else {
84-
formula <- rep(1 / nTrt, nTrt)
91+
if (is.null(ratio)) {
92+
93+
if (nTrt == 2) {
94+
formula <- .5
95+
} else {
96+
formula <- rep(1 / nTrt, nTrt)
97+
}
98+
99+
} else { # ratio not null
100+
formula <- ratio/sum(ratio)
85101
}
102+
86103

87104
dt <- trtObserve(dt, formulas = formula, logit.link = FALSE, grpName)
88105

man/trtAssign.Rd

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

0 commit comments

Comments
 (0)