Observed exposure or treatment

trtObserve(dt, formulas, logit.link = FALSE, grpName = "trtGrp")

Arguments

dt

data table

formulas

collection of formulas that determine probabilities

indicator that specifies link. If TRUE, then logit link is used. If FALSE, the identity link is used.

grpName

character string representing name of treatment/exposure group variable

Value

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

trtAssign

Examples

def <- defData(varname = "male", dist = "binary", formula = .5, id = "cid")
def <- defData(def, varname = "over65", dist = "binary", formula = "-1.7 + .8*male", link = "logit")
def <- defData(def, varname = "baseDBP", dist = "normal", formula = 70, variance = 40)

dtstudy <- genData(1000, def)
dtstudy
#> Key: <cid>
#>         cid  male over65  baseDBP
#>       <int> <int>  <int>    <num>
#>    1:     1     0      0 70.91353
#>    2:     2     0      1 68.09961
#>    3:     3     1      1 75.66393
#>    4:     4     1      0 71.98746
#>    5:     5     1      1 77.18121
#>   ---
#>  996:   996     1      1 70.36416
#>  997:   997     1      0 69.82684
#>  998:   998     1      0 74.31698
#>  999:   999     1      0 71.24927
#> 1000:  1000     1      1 81.51260

formula1 <- c("-2 + 2*male - .5*over65", "-1 + 2*male + .5*over65")
dtObs <- trtObserve(dtstudy, formulas = formula1, logit.link = TRUE, grpName = "exposure")
dtObs
#> Key: <cid>
#>         cid exposure  male over65  baseDBP
#>       <int>    <int> <int>  <int>    <num>
#>    1:     1        2     0      0 70.91353
#>    2:     2        3     0      1 68.09961
#>    3:     3        2     1      1 75.66393
#>    4:     4        1     1      0 71.98746
#>    5:     5        2     1      1 77.18121
#>   ---
#>  996:   996        2     1      1 70.36416
#>  997:   997        1     1      0 69.82684
#>  998:   998        2     1      0 74.31698
#>  999:   999        2     1      0 71.24927
#> 1000:  1000        2     1      1 81.51260

# Check actual distributions

dtObs[, .(pctMale = round(mean(male), 2)), keyby = exposure]
#> Key: <exposure>
#>    exposure pctMale
#>       <int>   <num>
#> 1:        1    0.67
#> 2:        2    0.70
#> 3:        3    0.23
dtObs[, .(pctMale = round(mean(over65), 2)), keyby = exposure]
#> Key: <exposure>
#>    exposure pctMale
#>       <int>   <num>
#> 1:        1    0.10
#> 2:        2    0.29
#> 3:        3    0.15

dtSum <- dtObs[, .N, keyby = .(male, over65, exposure)]
dtSum[, grpPct := round(N / sum(N), 2), keyby = .(male, over65)]
#> Key: <male, over65>
#>      male over65 exposure     N grpPct
#>     <int>  <int>    <int> <int>  <num>
#>  1:     0      0        1    40   0.09
#>  2:     0      0        2   107   0.24
#>  3:     0      0        3   290   0.66
#>  4:     0      1        1     3   0.04
#>  5:     0      1        2    23   0.34
#>  6:     0      1        3    41   0.61
#>  7:     1      0        1    77   0.22
#>  8:     1      0        2   206   0.58
#>  9:     1      0        3    75   0.21
#> 10:     1      1        1    10   0.07
#> 11:     1      1        2   103   0.75
#> 12:     1      1        3    25   0.18
dtSum
#> Key: <male, over65>
#>      male over65 exposure     N grpPct
#>     <int>  <int>    <int> <int>  <num>
#>  1:     0      0        1    40   0.09
#>  2:     0      0        2   107   0.24
#>  3:     0      0        3   290   0.66
#>  4:     0      1        1     3   0.04
#>  5:     0      1        2    23   0.34
#>  6:     0      1        3    41   0.61
#>  7:     1      0        1    77   0.22
#>  8:     1      0        2   206   0.58
#>  9:     1      0        3    75   0.21
#> 10:     1      1        1    10   0.07
#> 11:     1      1        2   103   0.75
#> 12:     1      1        3    25   0.18