Learn R Programming

carts (version 0.1.0)

covar_add: Add additional covariates to existing list of covariates

Description

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, ...)

Value

matching format of covariates in covars

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

Author

Klaus Kähler Holst

Examples

Run this code
# 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)
# 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)
# 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)

Run the code above in your browser using DataLab