Create longitudinal/panel data

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

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"

periodVec

Vector of period times. Defaults to NULL

Value

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

Details

It is possible to generate longitudinal data with varying numbers of measurement periods as well as varying time intervals between each measurement period. This is done by defining specific variables in the data set that define the number of observations per subject and the average interval time between each observation. nCount defines the number of measurements for an individual; mInterval specifies the average time between intervals for a subject; and vInterval specifies the variance of those interval times. If mInterval is not defined, no intervals are used. If vInterval is set to 0 or is not defined, the interval for a subject is determined entirely by the mean interval. If vInterval is greater than 0, time intervals are generated using a gamma distribution with specified mean and dispersion. If either nPeriods or timevars is specified, that will override any nCount, mInterval, and vInterval data.

periodVec is used to specify measurement periods that are different the default counting variables. If periodVec is not specified, the periods default to 0, 1, ... n-1, with n periods. If periodVec is specified as c(x_1, x_2, ... x_n), then x_1, x_2, ... x_n represent the measurement periods.

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
#> Key: <id>
#>       id     T       Y0       Y1       Y2
#>    <int> <int>    <num>    <num>    <num>
#> 1:     1     1 11.41055 20.48760 27.10624
#> 2:     2     0 10.57090 16.28004 18.22835
#> 3:     3     0 11.05450 16.02915 21.83916
#> 4:     4     0 10.66699 16.46587 21.53544
#> 5:     5     0 10.40096 13.54061 21.05604

dtTime <- addPeriods(dtTrial,
  nPeriods = 3, idvars = "id",
  timevars = c("Y0", "Y1", "Y2"), timevarName = "Y"
)
dtTime
#> Key: <timeID>
#>        id period     T        Y timeID
#>     <int>  <int> <int>    <num>  <int>
#>  1:     1      0     1 11.41055      1
#>  2:     1      1     1 20.48760      2
#>  3:     1      2     1 27.10624      3
#>  4:     2      0     0 10.57090      4
#>  5:     2      1     0 16.28004      5
#>  6:     2      2     0 18.22835      6
#>  7:     3      0     0 11.05450      7
#>  8:     3      1     0 16.02915      8
#>  9:     3      2     0 21.83916      9
#> 10:     4      0     0 10.66699     10
#> 11:     4      1     0 16.46587     11
#> 12:     4      2     0 21.53544     12
#> 13:     5      0     0 10.40096     13
#> 14:     5      1     0 13.54061     14
#> 15:     5      2     0 21.05604     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)]
#> Key: <id>
#>       id    xbase nCount mInterval vInterval
#>    <int>    <num>  <num>     <num>     <num>
#> 1:     8 19.21504      4  27.55155      0.07
#> 2:   121 20.31966      3  34.23716      0.07

dtPeriod <- addPeriods(dt)
dtPeriod[id %in% c(8, 121)] # View individuals 8 and 121 only
#> Key: <timeID>
#>       id period    xbase  time timeID
#>    <int>  <int>    <num> <num>  <int>
#> 1:     8      0 19.21504     0     41
#> 2:     8      1 19.21504    36     42
#> 3:     8      2 19.21504    62     43
#> 4:     8      3 19.21504    84     44
#> 5:   121      0 20.31966     0    734
#> 6:   121      1 20.31966    36    735
#> 7:   121      2 20.31966    64    736