Learn R Programming

carts (version 0.1.0)

covar_join: Add additional covariates to existing covariate random generator

Description

For use with Trial objects, this function makes it possible to easily add additional covariates to an existing random generator (function(n ...) returning a data.frame or data.table)

Usage

covar_join(f, ...)

Value

function, with returned data type matching that of f

Arguments

f

covariate random generator

...

additional covariate generators or constant covariates

Examples

Run this code
# single period
n <- 5
c1 <- function(n) data.frame(a = rnorm(n))
c2 <- function(n) data.frame(b = rnorm(n))
x <- c1 %join% c2
x(n)

# adding covariates that remain constant when sampling
x <- c1 %join% data.frame(b = rnorm(n))
all.equal(x(n)$b, x(n)$b)

# adding multiple anonymous functions require parenthesis enclosing, with
# the exception of the last function
x <- c1 %join%
 (\(n) data.frame(b = rnorm(n))) %join%
 \(n) data.frame(c = rnorm(n))
x(n)

# multiple periods
base <- setargs(covar_loggamma, normal.cor = .5)
x <- base %join%
  function(n) list(
      data.frame(a = rbinom(n, 1, 0.5)),
      data.frame(a = rbinom(n, 1, 0.5))
    )
x(n)

# constant covariate
x <- base %join% list(data.frame(a = 0), data.frame(a = 1))
x(n)

# baseline covariate
x <- base %join% function(n) data.frame(w = rnorm(n))
x(n)

Run the code above in your browser using DataLab