Assign treatment

trtAssign(
  dtName,
  nTrt = 2,
  balanced = TRUE,
  strata = NULL,
  grpName = "trtGrp",
  ratio = NULL
)

Arguments

dtName

data table

nTrt

number of treatment groups

balanced

indicator for treatment assignment process

strata

vector of strings representing stratifying variables

grpName

string representing variable name for treatment or exposure group

ratio

vector of values indicating relative proportion of group assignment

Value

An integer (group) ranging from 1 to length of the probability vector

See also

Examples

dt <- genData(15)

dt1 <- trtAssign(dt, nTrt = 3, balanced = TRUE)
dt1[, .N, keyby = trtGrp]
#> Key: <trtGrp>
#>    trtGrp     N
#>     <int> <int>
#> 1:      1     5
#> 2:      2     5
#> 3:      3     5

dt2 <- trtAssign(dt, nTrt = 3, balanced = FALSE)
dt2[, .N, keyby = trtGrp]
#> Key: <trtGrp>
#>    trtGrp     N
#>     <int> <int>
#> 1:      1     5
#> 2:      2     6
#> 3:      3     4

def <- defData(varname = "male", formula = .4, dist = "binary")
dt <- genData(1000, def)
dt
#> Key: <id>
#>          id  male
#>       <int> <int>
#>    1:     1     0
#>    2:     2     0
#>    3:     3     0
#>    4:     4     0
#>    5:     5     1
#>   ---            
#>  996:   996     0
#>  997:   997     1
#>  998:   998     0
#>  999:   999     1
#> 1000:  1000     0

dt3 <- trtAssign(dt, nTrt = 5, strata = "male", balanced = TRUE, grpName = "Group")
dt3
#> Key: <id>
#>          id  male Group
#>       <int> <int> <int>
#>    1:     1     0     4
#>    2:     2     0     2
#>    3:     3     0     2
#>    4:     4     0     4
#>    5:     5     1     2
#>   ---                  
#>  996:   996     0     5
#>  997:   997     1     2
#>  998:   998     0     5
#>  999:   999     1     2
#> 1000:  1000     0     5
dt3[, .N, keyby = .(male, Group)]
#> Key: <male, Group>
#>      male Group     N
#>     <int> <int> <int>
#>  1:     0     1   122
#>  2:     0     2   122
#>  3:     0     3   122
#>  4:     0     4   122
#>  5:     0     5   122
#>  6:     1     1    78
#>  7:     1     2    78
#>  8:     1     3    78
#>  9:     1     4    78
#> 10:     1     5    78
dt3[, .N, keyby = .(Group)]
#> Key: <Group>
#>    Group     N
#>    <int> <int>
#> 1:     1   200
#> 2:     2   200
#> 3:     3   200
#> 4:     4   200
#> 5:     5   200

dt4 <- trtAssign(dt, nTrt = 5, strata = "male", balanced = FALSE, grpName = "Group")
dt4[, .N, keyby = .(male, Group)]
#> Key: <male, Group>
#>      male Group     N
#>     <int> <int> <int>
#>  1:     0     1   130
#>  2:     0     2   112
#>  3:     0     3    98
#>  4:     0     4   137
#>  5:     0     5   133
#>  6:     1     1    69
#>  7:     1     2    92
#>  8:     1     3    77
#>  9:     1     4    74
#> 10:     1     5    78
dt4[, .N, keyby = .(Group)]
#> Key: <Group>
#>    Group     N
#>    <int> <int>
#> 1:     1   199
#> 2:     2   204
#> 3:     3   175
#> 4:     4   211
#> 5:     5   211

dt5 <- trtAssign(dt, nTrt = 5, balanced = TRUE, grpName = "Group")
dt5[, .N, keyby = .(male, Group)]
#> Key: <male, Group>
#>      male Group     N
#>     <int> <int> <int>
#>  1:     0     1   124
#>  2:     0     2   126
#>  3:     0     3   126
#>  4:     0     4   117
#>  5:     0     5   117
#>  6:     1     1    76
#>  7:     1     2    74
#>  8:     1     3    74
#>  9:     1     4    83
#> 10:     1     5    83
dt5[, .N, keyby = .(Group)]
#> Key: <Group>
#>    Group     N
#>    <int> <int>
#> 1:     1   200
#> 2:     2   200
#> 3:     3   200
#> 4:     4   200
#> 5:     5   200

dt6 <- trtAssign(dt, nTrt = 3, ratio = c(1, 2, 2), grpName = "Group")
dt6[, .N, keyby = .(Group)]
#> Key: <Group>
#>    Group     N
#>    <int> <int>
#> 1:     1   200
#> 2:     2   400
#> 3:     3   400