## Longitudinal data

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:

```
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(500, tdef)
dtTrial
```

```
## id T Y0 Y1 Y2
## 1: 1 1 9.465444 20.31767 23.55068
## 2: 2 1 10.571767 21.38158 27.67457
## 3: 3 1 11.664527 21.53523 26.51475
## 4: 4 1 10.687010 19.51139 25.32802
## 5: 5 1 9.491987 19.15681 24.55246
## ---
## 496: 496 1 8.557838 18.52464 25.16032
## 497: 497 1 10.665368 19.30674 25.14935
## 498: 498 1 8.240092 17.52485 22.05565
## 499: 499 0 8.464584 12.35172 18.25443
## 500: 500 0 11.403908 16.11624 20.28438
```

The data in longitudinal form is 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).```
dtTime <- addPeriods(dtTrial, nPeriods = 3, idvars = "id", timevars = c("Y0",
"Y1", "Y2"), timevarName = "Y")
dtTime
```

This is what the longitudinal data look like:

### Longitudinal data with varying observation and interval times

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 an 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:

```
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 = 0.01)
def <- defData(def, varname = "vInterval", dist = "nonrandom", formula = 0.07)
dt <- genData(200, def)
dt[id %in% c(8, 121)] # View individuals 8 and 121
```

```
## id xbase nCount mInterval vInterval
## 1: 8 16.82454 7 29.72927 0.07
## 2: 121 17.70609 6 37.52446 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:

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

```
## id period xbase time timeID
## 1: 8 0 16.82454 0 47
## 2: 8 1 16.82454 30 48
## 3: 8 2 16.82454 50 49
## 4: 8 3 16.82454 75 50
## 5: 8 4 16.82454 111 51
## 6: 8 5 16.82454 139 52
## 7: 8 6 16.82454 165 53
## 8: 121 0 17.70609 0 752
## 9: 121 1 17.70609 40 753
## 10: 121 2 17.70609 81 754
## 11: 121 3 17.70609 117 755
## 12: 121 4 17.70609 135 756
## 13: 121 5 17.70609 189 757
```

If a time sensitive measurement is added to the data set …

```
def2 <- defDataAdd(varname = "Y", dist = "normal", formula = "15 + .1 * time",
variance = 5)
dtPeriod <- addColumns(def2, dtPeriod)
```

… a plot of a five randomly selected individuals looks like this: