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  9.456965 18.86038 25.19263
#> 2:  2 1  8.480449 17.72920 22.54907
#> 3:  3 0  8.376164 14.48071 19.41728
#> 4:  4 1 11.317251 20.54854 25.70655
#> 5:  5 1 11.609371 20.65682 25.77851

dtTime <- addPeriods(dtTrial,
  nPeriods = 3, idvars = "id",
  timevars = c("Y0", "Y1", "Y2"), timevarName = "Y"
)
dtTime
#>     id period T         Y timeID
#>  1:  1      0 1  9.456965      1
#>  2:  1      1 1 18.860375      2
#>  3:  1      2 1 25.192630      3
#>  4:  2      0 1  8.480449      4
#>  5:  2      1 1 17.729199      5
#>  6:  2      2 1 22.549071      6
#>  7:  3      0 0  8.376164      7
#>  8:  3      1 0 14.480710      8
#>  9:  3      2 0 19.417279      9
#> 10:  4      0 1 11.317251     10
#> 11:  4      1 1 20.548543     11
#> 12:  4      2 1 25.706550     12
#> 13:  5      0 1 11.609371     13
#> 14:  5      1 1 20.656824     14
#> 15:  5      2 1 25.778510     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 22.52055      4  32.20669      0.07
#> 2: 121 17.72246      8  31.27010      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 22.52055    0     41
#>  2:   8      1 22.52055   37     42
#>  3:   8      2 22.52055   82     43
#>  4:   8      3 22.52055  118     44
#>  5: 121      0 17.72246    0    725
#>  6: 121      1 17.72246   24    726
#>  7: 121      2 17.72246   60    727
#>  8: 121      3 17.72246   87    728
#>  9: 121      4 17.72246  111    729
#> 10: 121      5 17.72246  165    730
#> 11: 121      6 17.72246  193    731
#> 12: 121      7 17.72246  222    732