Observed exposure or treatment
trtObserve(dt, formulas, logit.link = FALSE, grpName = "trtGrp")
data table
collection of formulas that determine probabilities
indicator that specifies link. If TRUE, then logit link is used. If FALSE, the identity link is used.
character string representing name of treatment/exposure group variable
An integer (group) ranging from 1 to length of the probability vector
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
#> cid male over65 baseDBP
#> 1: 1 0 0 75.16916
#> 2: 2 0 0 76.26197
#> 3: 3 1 1 66.78133
#> 4: 4 0 0 71.67887
#> 5: 5 1 0 64.18112
#> ---
#> 996: 996 0 1 69.86304
#> 997: 997 1 0 63.88180
#> 998: 998 0 0 72.93428
#> 999: 999 0 0 79.72073
#> 1000: 1000 1 0 81.65354
formula1 <- c("-2 + 2*male - .5*over65", "-1 + 2*male + .5*over65")
dtObs <- trtObserve(dtstudy, formulas = formula1, logit.link = TRUE, grpName = "exposure")
dtObs
#> cid exposure male over65 baseDBP
#> 1: 1 1 0 0 75.16916
#> 2: 2 3 0 0 76.26197
#> 3: 3 2 1 1 66.78133
#> 4: 4 2 0 0 71.67887
#> 5: 5 3 1 0 64.18112
#> ---
#> 996: 996 3 0 1 69.86304
#> 997: 997 2 1 0 63.88180
#> 998: 998 1 0 0 72.93428
#> 999: 999 3 0 0 79.72073
#> 1000: 1000 2 1 0 81.65354
# Check actual distributions
dtObs[, .(pctMale = round(mean(male), 2)), keyby = exposure]
#> exposure pctMale
#> 1: 1 0.61
#> 2: 2 0.71
#> 3: 3 0.24
dtObs[, .(pctMale = round(mean(over65), 2)), keyby = exposure]
#> exposure pctMale
#> 1: 1 0.15
#> 2: 2 0.27
#> 3: 3 0.15
dtSum <- dtObs[, .N, keyby = .(male, over65, exposure)]
dtSum[, grpPct := round(N / sum(N), 2), keyby = .(male, over65)]
#> male over65 exposure N grpPct
#> 1: 0 0 1 52 0.12
#> 2: 0 0 2 110 0.25
#> 3: 0 0 3 280 0.63
#> 4: 0 1 1 3 0.05
#> 5: 0 1 2 18 0.29
#> 6: 0 1 3 41 0.66
#> 7: 1 0 1 69 0.20
#> 8: 1 0 2 207 0.59
#> 9: 1 0 3 77 0.22
#> 10: 1 1 1 18 0.13
#> 11: 1 1 2 101 0.71
#> 12: 1 1 3 24 0.17
dtSum
#> male over65 exposure N grpPct
#> 1: 0 0 1 52 0.12
#> 2: 0 0 2 110 0.25
#> 3: 0 0 3 280 0.63
#> 4: 0 1 1 3 0.05
#> 5: 0 1 2 18 0.29
#> 6: 0 1 3 41 0.66
#> 7: 1 0 1 69 0.20
#> 8: 1 0 2 207 0.59
#> 9: 1 0 3 77 0.22
#> 10: 1 1 1 18 0.13
#> 11: 1 1 2 101 0.71
#> 12: 1 1 3 24 0.17