addPeriods: Create longitudinal/panel data

Description Usage Arguments Value Examples

View source: R/addPeriods.R

Description

Create longitudinal/panel data

Usage

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

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"

Value

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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

dtTime <- addPeriods(dtTrial, nPeriods = 3, idvars = "id",
                     timevars = c("Y0", "Y1", "Y2"), timevarName = "Y")
dtTime

# 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)]

dtPeriod <- addPeriods(dt)
dtPeriod[id %in% c(8,121)]  # View individuals 8 and 121 only

kgoldfeld/simstudy documentation built on June 6, 2018, 11:25 a.m.