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 0 11.250332 15.15071 20.24321 #> 2: 2 0 10.386095 14.83192 20.85290 #> 3: 3 0 11.239934 15.35838 20.78620 #> 4: 4 0 9.486771 15.74710 20.10258 #> 5: 5 0 9.915813 13.22807 19.45120
dtTime <- addPeriods(dtTrial, nPeriods = 3, idvars = "id", timevars = c("Y0", "Y1", "Y2"), timevarName = "Y" ) dtTime
#> id period T Y timeID #> 1: 1 0 0 11.250332 1 #> 2: 1 1 0 15.150712 2 #> 3: 1 2 0 20.243215 3 #> 4: 2 0 0 10.386095 4 #> 5: 2 1 0 14.831922 5 #> 6: 2 2 0 20.852897 6 #> 7: 3 0 0 11.239934 7 #> 8: 3 1 0 15.358383 8 #> 9: 3 2 0 20.786202 9 #> 10: 4 0 0 9.486771 10 #> 11: 4 1 0 15.747098 11 #> 12: 4 2 0 20.102576 12 #> 13: 5 0 0 9.915813 13 #> 14: 5 1 0 13.228072 14 #> 15: 5 2 0 19.451203 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.08569 5 34.61167 0.07 #> 2: 121 20.92508 7 28.33791 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.08569 0 42 #> 2: 8 1 20.08569 46 43 #> 3: 8 2 20.08569 74 44 #> 4: 8 3 20.08569 93 45 #> 5: 8 4 20.08569 121 46 #> 6: 121 0 20.92508 0 682 #> 7: 121 1 20.92508 26 683 #> 8: 121 2 20.92508 47 684 #> 9: 121 3 20.92508 78 685 #> 10: 121 4 20.92508 107 686 #> 11: 121 5 20.92508 133 687 #> 12: 121 6 20.92508 166 688