To simulate longitudinal data, we start with a ‘cross-sectional’ data set and convert it to a time-dependent data set. The original cross-sectional data set may or may not include time-dependent data in the columns. In the next example, we measure outcome Y
once before and twice after intervention T
in a randomized trial:
<- 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",
tdef variance = 1)
<- defData(tdef, varname = "Y2", dist = "normal", formula = "Y0 + 10 + 5 * T",
tdef variance = 1)
set.seed(483726)
<- genData(500, tdef)
dtTrial dtTrial
## id T Y0 Y1 Y2
## 1: 1 0 10.42 14.2 20.9
## 2: 2 1 9.36 21.9 25.4
## 3: 3 1 8.25 19.1 23.9
## 4: 4 0 10.86 16.5 20.4
## 5: 5 1 11.29 21.7 25.9
## ---
## 496: 496 1 11.87 22.6 26.6
## 497: 497 1 10.62 19.5 24.9
## 498: 498 1 10.13 22.2 26.1
## 499: 499 0 11.88 17.9 23.0
## 500: 500 0 11.92 18.4 22.5
Longitudinal data are created with a call to addPeriods
. If the cross-sectional data includes time-dependent data, then the number of periods nPeriods
must be the same as the number of time-dependent columns. If a variable is not declared as one of the timevars
, it will be repeated each time period. In this example, the treatment indicator T
is not specified as a time-dependent variable. (Note: if there are two time-dependent variables, it is best to create two data sets and merge them. This will be shown later in the vignette).
<- addPeriods(dtTrial, nPeriods = 3, idvars = "id", timevars = c("Y0", "Y1",
dtTime "Y2"), timevarName = "Y")
dtTime
## id period T Y timeID
## 1: 1 0 0 10.42 1
## 2: 1 1 0 14.15 2
## 3: 1 2 0 20.93 3
## 4: 2 0 1 9.36 4
## 5: 2 1 1 21.90 5
## ---
## 1496: 499 1 0 17.94 1496
## 1497: 499 2 0 23.05 1497
## 1498: 500 0 0 11.92 1498
## 1499: 500 1 0 18.39 1499
## 1500: 500 2 0 22.50 1500
This is what the longitudinal data look like:
It is also 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 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 mean and dispersion specified.
In this simple example, the cross-sectional data generates individuals with a different number of measurement observations and different times between each observation. Data for two of these individuals is printed:
<- 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 = 0.01)
def <- defData(def, varname = "vInterval", dist = "nonrandom", formula = 0.07)
def
<- genData(200, def)
dt %in% c(8, 121)] # View individuals 8 and 121 dt[id
## id xbase nCount mInterval vInterval
## 1: 8 18.0 4 28.1 0.07
## 2: 121 22.7 6 33.2 0.07
The resulting longitudinal data for these two subjects can be inspected after a call to addPeriods
. Notice that no parameters need to be set since all information resides in the data set itself:
<- addPeriods(dt)
dtPeriod %in% c(8, 121)] # View individuals 8 and 121 only dtPeriod[id
## id period xbase time timeID
## 1: 8 0 18.0 0 41
## 2: 8 1 18.0 29 42
## 3: 8 2 18.0 51 43
## 4: 8 3 18.0 104 44
## 5: 121 0 22.7 0 691
## 6: 121 1 22.7 46 692
## 7: 121 2 22.7 81 693
## 8: 121 3 22.7 117 694
## 9: 121 4 22.7 154 695
## 10: 121 5 22.7 180 696
If a time-sensitive measurement is added to the data set …
<- defDataAdd(varname = "Y", dist = "normal", formula = "15 + .1 * time", variance = 5)
def2 <- addColumns(def2, dtPeriod) dtPeriod
… a plot of five randomly selected individuals looks like this: