Skip to contents

For use with Trial objects, this function makes it possible to easily add additional covariates to an existing list of covariates (in the form of a data.frame or data.table).

Usage

covar_add(covars, x, names = NULL, ...)

Arguments

covars

list of covariates (data.frame's or data.table's)

x

new covariates (function or list of functions/scalars)

names

optional names of new covariates

...

additional arguments to function x or functions in x

Value

matching format of covariates in covars

Author

Klaus Kähler Holst

Examples

# adding "fixed" treatment indicator in each period
n <- 5
xt <- function(n, ...) {
 covar_loggamma(n, normal.cor = 0.2) |>
   covar_add(list(a = 0, a = 1))
}
xt(n)
#> $`0`
#>             z     a
#>         <num> <num>
#> 1: -0.4516861     0
#> 2: -2.1892365     0
#> 3: -2.6154933     0
#> 4:  0.5078047     0
#> 5: -1.7551775     0
#> 
#> $`1`
#>             z     a
#>         <num> <num>
#> 1: -0.8288163     1
#> 2: -3.6019418     1
#> 3: -0.4412044     1
#> 4:  0.6409044     1
#> 5:  0.4852669     1
#> 
# adding randomized treatment indicator
xt <- function(n, ...) {
 covar_loggamma(n, normal.cor = 0.2) |>
   covar_add(list(a = rbinom(n, 1, 0.5), a = rbinom(n, 1, 0.5)))
}
xt(5)
#> $`0`
#>             z     a
#>         <num> <int>
#> 1: -0.9950625     1
#> 2:  0.3403022     0
#> 3: -0.2265323     1
#> 4:  0.0303355     1
#> 5: -1.1902159     0
#> 
#> $`1`
#>             z     a
#>         <num> <int>
#> 1: -1.3981285     1
#> 2: -0.9163669     1
#> 3: -0.8012435     0
#> 4:  0.5819330     0
#> 5:  1.1173551     0
#> 
# adding baseline covariates
xt <- function(n, ...) {
 covar_loggamma(n, normal.cor = 0.2) |>
   covar_add(rnorm(n), names = "w1") |> # data
   covar_add(list(w2 = rnorm(n))) |> # data
   covar_add(data.frame(w3 = rnorm(n))) |> # data
   covar_add(\(n) data.frame(w4 = rnorm(n))) |> # function
   covar_add(\(n) rnorm(n), names = "w5") # function
}
xt(5)
#> $`0`
#>             z         w1         w2          w3          w4         w5
#>         <num>      <num>      <num>       <num>       <num>      <num>
#> 1:  0.8195252  1.6229452 -1.0305073  0.18822825 -0.69553077 -1.5869430
#> 2:  0.1850486 -0.7080310  0.7704762  1.95150653 -0.89125131  0.6758453
#> 3: -2.2238584  0.3308867 -1.2894581 -1.10794783 -0.24231246 -0.4758486
#> 4: -1.5360723  0.6711626  1.5771846 -0.04758160  1.77474338 -0.4963323
#> 5:  1.0351734  0.8351916 -0.1139907 -0.09504016  0.05588728  0.1729910
#> 
#> $`1`
#>             z         w1         w2          w3          w4         w5
#>         <num>      <num>      <num>       <num>       <num>      <num>
#> 1:  0.1261988  1.6229452 -1.0305073  0.18822825 -0.69553077 -1.5869430
#> 2:  0.2678649 -0.7080310  0.7704762  1.95150653 -0.89125131  0.6758453
#> 3: -1.3207321  0.3308867 -1.2894581 -1.10794783 -0.24231246 -0.4758486
#> 4: -2.2563337  0.6711626  1.5771846 -0.04758160  1.77474338 -0.4963323
#> 5:  0.9176566  0.8351916 -0.1139907 -0.09504016  0.05588728  0.1729910
#>