lagged: History functions

View source: R/histories.R

laggedR Documentation

History functions

Description

These functions create new columns in an input data table for covariate histories. Users must specify which covariates are to be used in the history functions.

Usage

lagged(
  pool,
  histvars,
  histvals,
  time_name,
  t,
  id_name,
  baselags,
  below_zero_indicator
)

cumavg(pool, histvars, time_name, t, id_name, below_zero_indicator)

lagavg(
  pool,
  histvars,
  histvals,
  time_name,
  t,
  id_name,
  baselags,
  below_zero_indicator
)

Arguments

pool

Data table containing all information prior to time t (t noninclusive).

histvars

Vector of character strings specifying the names of the variables for which history functions are to be applied.

histvals

For lagged, this argument is a vector specifying the lags used in the model statements (e.g., if lag1_varname and lag2_varname were included in the model statements, this vector would be c(1,2)). For lagavg, this argument is a numeric vector specifying the lag averages used in the model statements.

time_name

Character string specifying the name of the time variable in pool.

t

Integer specifying the current time index.

id_name

Character string specifying the name of the ID variable in pool.

baselags

Logical scalar for specifying the convention used for lagi and lag_cumavgi terms in the model statements when pre-baseline times are not included in obs_data and when the current time index, t, is such that t < i. If this argument is set to FALSE, the value of all lagi and lag_cumavgi terms in this context are set to 0 (for non-categorical covariates) or the reference level (for categorical covariates). If this argument is set to TRUE, the value of lagi and lag_cumavgi terms are set to their values at time 0. The default is FALSE.

below_zero_indicator

Logical scalar indicating whether the observed data set contains rows for time t < 0.

Details

lagged creates new columns for lagged versions of existing variables in the dataset. The user must specify which variables are to be lagged.

cumavg creates new columns for the cumulative average up until time t of existing variables in the dataset.

lagavg creates new columns for the "lagged cumulative average" (cumulative average up until time t, then lagged by one time unit) up until time t of existing variables in the dataset.

Value

No value is returned. The data table pool is modified in place.

Examples

## Estimating the effect of static treatment strategies on risk of a
## failure event

id <- 'id'
time_points <- 7
time_name <- 't0'
covnames <- c('L1', 'L2', 'A')
outcome_name <- 'Y'
outcome_type <- 'survival'
covtypes <- c('binary', 'bounded normal', 'binary')
histories <- c(lagged, lagavg)
histvars <- list(c('A', 'L1', 'L2'), c('L1', 'L2'))
covparams <- list(covmodels = c(L1 ~ lag1_A + lag_cumavg1_L1 + lag_cumavg1_L2 +
                                  L3 + t0,
                                L2 ~ lag1_A + L1 + lag_cumavg1_L1 +
                                  lag_cumavg1_L2 + L3 + t0,
                                A ~ lag1_A + L1 + L2 + lag_cumavg1_L1 +
                                  lag_cumavg1_L2 + L3 + t0))
ymodel <- Y ~ A + L1 + L2 + L3 + lag1_A + lag1_L1 + lag1_L2 + t0
intervention1.A <- list(static, rep(0, time_points))
intervention2.A <- list(static, rep(1, time_points))
int_descript <- c('Never treat', 'Always treat')
nsimul <- 10000

gform_basic <- gformula(obs_data = basicdata_nocomp, id = id,
                        time_points = time_points,
                        time_name = time_name, covnames = covnames,
                        outcome_name = outcome_name,
                        outcome_type = outcome_type, covtypes = covtypes,
                        covparams = covparams, ymodel = ymodel,
                        intervention1.A = intervention1.A,
                        intervention2.A = intervention2.A,
                        int_descript = int_descript,
                        histories = histories, histvars = histvars,
                        basecovs = c('L3'), nsimul = nsimul,
                        seed = 1234)
gform_basic



CausalInference/gfoRmula documentation built on Oct. 1, 2024, 8:36 p.m.