add_diff_variable: Add the lagged difference between multiple variables to a...

Description Usage Arguments Value Examples

View source: R/add_diff_variable.R

Description

Differences multiple variables (either exactly or partially defined) by another variable for a specified lag and stratification.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
add_diff_variable(
  dt,
  variable,
  label,
  by,
  shift_var = "value",
  vars = c("median", "mean", "secondary", "value"),
  partial_vars = c("lower_", "upper_"),
  fill = 1,
  n = 1L,
  type = "lag"
)

Arguments

dt

A data.table containing at a target variable and the variables identified in the other arguments.

variable

Character string identifying the variable in target to calculate the lagged difference for.

label

Character string giving the name for the new lagged variable.

by

An optional character vector identifying the variables to stratify by.

shift_var

Character string, indicating the variable to use to calculate the difference.

vars

Character vector giving the exact names of variables to calculate the difference for.

partial_vars

Character vector giving the partial names of variables to calculate the difference for.

fill

Value to use for padding when the window goes beyond the input length.

n

integer vector denoting the offset by which to lead or lag the input. To create multiple lead/lag vectors, provide multiple values to n; negative values of n will "flip" the value of type, i.e., n=-1 and type='lead' is the same as n=1 and type='lag'.

type

default is "lag" (look "backwards"). The other possible values "lead" (look "forwards") and "shift" (behave same as "lag" except given names).

Value

The input data.table combined with the new differenced variable.

Examples

1
2
3
4
dt <- data.frame(target = "test", var = 1:100, var2 = c(1, 2),
                 strat = c("a", "b"))
add_diff_variable(dt, "test", "new", vars = "var", shift_var = "var2",
                  by = "strat")

epiforecasts/covid19.track.severity documentation built on July 6, 2021, 4:50 p.m.