Create longitudinal/panel data

addPeriods(
  dtName,
  nPeriods = NULL,
  idvars = "id",
  timevars = NULL,
  timevarName = "timevar",
  timeid = "timeID",
  perName = "period"
)

Arguments

dtName

Name of existing data table

nPeriods

Number of time periods for each record

idvars

Names of index variables (in a string vector) that will be repeated during each time period

timevars

Names of time dependent variables. Defaults to NULL.

timevarName

Name of new time dependent variable

timeid

Variable name for new index field. Defaults to "timevar"

perName

Variable name for period field. Defaults to "period"

Value

An updated data.table that that has multiple rows per observation in dtName

Examples

tdef <- defData(varname = "T", dist = "binary", formula = 0.5)
tdef <- defData(tdef, varname = "Y0", dist = "normal", formula = 10, variance = 1)
tdef <- defData(tdef, varname = "Y1", dist = "normal", formula = "Y0 + 5 + 5 * T", variance = 1)
tdef <- defData(tdef, varname = "Y2", dist = "normal", formula = "Y0 + 10 + 5 * T", variance = 1)

dtTrial <- genData(5, tdef)
dtTrial
#>    id T        Y0       Y1       Y2
#> 1:  1 1 10.402067 20.01140 24.60762
#> 2:  2 1  9.308089 17.23768 23.46689
#> 3:  3 0 10.926028 17.38689 19.53244
#> 4:  4 0 11.815208 15.96515 20.15412
#> 5:  5 0 10.170695 14.37923 19.81478

dtTime <- addPeriods(dtTrial,
  nPeriods = 3, idvars = "id",
  timevars = c("Y0", "Y1", "Y2"), timevarName = "Y"
)
dtTime
#>     id period T         Y timeID
#>  1:  1      0 1 10.402067      1
#>  2:  1      1 1 20.011398      2
#>  3:  1      2 1 24.607624      3
#>  4:  2      0 1  9.308089      4
#>  5:  2      1 1 17.237682      5
#>  6:  2      2 1 23.466891      6
#>  7:  3      0 0 10.926028      7
#>  8:  3      1 0 17.386892      8
#>  9:  3      2 0 19.532440      9
#> 10:  4      0 0 11.815208     10
#> 11:  4      1 0 15.965149     11
#> 12:  4      2 0 20.154124     12
#> 13:  5      0 0 10.170695     13
#> 14:  5      1 0 14.379230     14
#> 15:  5      2 0 19.814775     15

# Varying # of periods and intervals - need to have variables
# called nCount and mInterval

def <- defData(varname = "xbase", dist = "normal", formula = 20, variance = 3)
def <- defData(def, varname = "nCount", dist = "noZeroPoisson", formula = 6)
def <- defData(def, varname = "mInterval", dist = "gamma", formula = 30, variance = .01)
def <- defData(def, varname = "vInterval", dist = "nonrandom", formula = .07)

dt <- genData(200, def)
dt[id %in% c(8, 121)]
#>     id    xbase nCount mInterval vInterval
#> 1:   8 20.66454      5  28.33865      0.07
#> 2: 121 20.24117      4  29.85563      0.07

dtPeriod <- addPeriods(dt)
dtPeriod[id %in% c(8, 121)] # View individuals 8 and 121 only
#>     id period    xbase time timeID
#> 1:   8      0 20.66454    0     42
#> 2:   8      1 20.66454   37     43
#> 3:   8      2 20.66454   70     44
#> 4:   8      3 20.66454  105     45
#> 5:   8      4 20.66454  133     46
#> 6: 121      0 20.24117    0    733
#> 7: 121      1 20.24117   21    734
#> 8: 121      2 20.24117   44    735
#> 9: 121      3 20.24117   74    736