The proportion of assignment to treatment is p_trt. Generation can be done fixing the proportion of patients in the treatment group to be as close as possible to p_trt and then permuting (type = "exact") or random sampling from a binomial distribution with probability p_trt (type = "random"). For type = "random" one can also specify a formula for logit(P(trt|X)) via "prop". The probability of obtaining treatment is then binomial with P(trt|X) = 1/(1+exp(-prop)). The variables names specified in "prop" need to be handed over in a data frame "X".

generate_trt(
  n,
  p_trt = 0.5,
  type = c("exact", "random"),
  X = NULL,
  prop = NULL
)

Arguments

n

Total sample size

p_trt

Proportion of treatment assignment (default = 0.5)

type

Either "exact" (proportion of p_trt is tried to match exactly), or "random" then treatment assignment is generated from a binomial distribution.

X

matrix/dataframe of predictor variables

prop

A formula used to generate logit(P(trt|X)). Probability of obtaining treatment is then Bin(1, P(trt|X)) with P(trt|X) = 1/(1+exp(-prop)). If type = "random" and prop is not specified p_trt is used in the binomial distribution

Value

Vector of treatment indicators

Examples

## by default try to match target trt proportion in observed trt proportion
trt <- generate_trt(10) # p_trt = 0.5 is default
table(trt)
#> trt
#> 0 1 
#> 5 5 
trt <- generate_trt(10, p_trt = 0.2)
trt <- generate_trt(11, p_trt = 0.5)
table(trt) # remaining patient randomly allocated
#> trt
#> 0 1 
#> 6 5 
## example for random allocation
trt <- generate_trt(10, p_trt = 0.5, type = "random") # samples from binomial with p_trt = 0.5
table(trt) # observed allocation may deviate from p_trt=0.5 for small samples
#> trt
#> 0 1 
#> 6 4 

## example, where treatment allocation depends on propensity model
X <- generate_X_dist(n=10000, p=10, rho=0.5)
## Use propensity score for treatment as 1/(1+exp(-X2))
trt <- generate_trt(n=nrow(X), type = "random", X=X, prop = "X2")
## fit propensity model
dat <- cbind(trt, X)
fit <- glm(trt~., data=dat, family=binomial)
summary(fit)
#> 
#> Call:
#> glm(formula = trt ~ ., family = binomial, data = dat)
#> 
#> Coefficients:
#>              Estimate Std. Error z value Pr(>|z|)    
#> (Intercept) -0.070667   0.061744  -1.145   0.2524    
#> X1Y         -0.011955   0.045543  -0.262   0.7929    
#> X2           1.038493   0.037639  27.591   <2e-16 ***
#> X3          -0.015390   0.027208  -0.566   0.5716    
#> X4           0.018967   0.027019   0.702   0.4827    
#> X5Y          0.050035   0.051081   0.980   0.3273    
#> X6M2        -0.024738   0.063729  -0.388   0.6979    
#> X6M3         0.023012   0.064293   0.358   0.7204    
#> X6M4        -0.015852   0.064401  -0.246   0.8056    
#> X7          -0.015343   0.022743  -0.675   0.4999    
#> X8           0.001252   0.022563   0.055   0.9557    
#> X9           0.001686   0.022533   0.075   0.9404    
#> X10          0.049464   0.022766   2.173   0.0298 *  
#> ---
#> Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#> 
#> (Dispersion parameter for binomial family taken to be 1)
#> 
#>     Null deviance: 12406  on 9999  degrees of freedom
#> Residual deviance: 11270  on 9987  degrees of freedom
#> AIC: 11296
#> 
#> Number of Fisher Scoring iterations: 5
#>