## Generating the treatment/exposure

Treatment assignment can be accomplished through the original data generation process, using `defData`

and `genData`

. However, the functions `trtAssign`

and `trtObserve`

provide more options to generate treatment assignment.

### Assigned treatment

Treatment assignment can simulate how treatment is made in a randomized study. Assignment to treatment groups can be (close to) balanced (as would occur in a block randomized trial); this balancing can be done without or without strata. Alternatively, the assignment can be left to chance without blocking; in this case, balance across treatment groups is not guaranteed, particularly with small sample sizes.

First, create the data definition:

```
def <- defData(varname = "male", dist = "binary", formula = 0.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(330, def)
```

*Balanced treatment assignment, stratified by gender and age category (not blood pressure)*

```
study1 <- trtAssign(dtstudy, n = 3, balanced = TRUE, strata = c("male", "over65"),
grpName = "rxGrp")
study1
```

```
## cid rxGrp male over65 baseDBP
## 1: 1 2 0 0 79.41260
## 2: 2 1 0 0 65.07555
## 3: 3 1 1 0 63.93037
## 4: 4 3 1 0 64.90775
## 5: 5 3 1 0 69.86494
## ---
## 326: 326 3 0 0 59.88214
## 327: 327 2 0 1 70.78260
## 328: 328 2 1 0 76.29750
## 329: 329 3 1 0 65.01489
## 330: 330 3 0 1 71.29769
```

*Balanced treatment assignment (without stratification)*

`study2 <- trtAssign(dtstudy, n = 3, balanced = TRUE, grpName = "rxGrp")`

*Random (unbalanced) treatment assignment*

`study3 <- trtAssign(dtstudy, n = 3, balanced = FALSE, grpName = "rxGrp")`

*Comparison of three treatment assignment mechanisms*

### Observed treatment

If exposure or treatment is observed (rather than randomly assigned), use `trtObserved`

to generate groups. There may be any number of possible exposure or treatment groups, and the probability of exposure to a specific level can depend on covariates already in the data set. In this case, there are three exposure groups that vary by gender and age:

```
formula1 <- c("-2 + 2*male - .5*over65", "-1 + 2*male + .5*over65")
dtExp <- trtObserve(dtstudy, formulas = formula1, logit.link = TRUE, grpName = "exposure")
```

Here are the exposure distributions by gender and age:

Here is a second case of three exposures where the exposure is independent of any covariates. Note that specifying the formula as `c(.35, .45)`

is the same as specifying it is `c(.35, .45, .20)`

. Also, when referring to probabilities, the identity link is used:

```
formula2 <- c(0.35, 0.45)
dtExp2 <- trtObserve(dtstudy, formulas = formula2, logit.link = FALSE, grpName = "exposure")
```