There are many ways to describe longitudinal data - from panel data, cross-sectional data, and time series. We define longitudinal data as:

Information from the same individuals, recorded at multiple points in time.

To explore and model longitudinal data, It is important to understand what variables represent the individual components, and the time components, and how these identify an individual moving through time. Identifying the individual and time components can sometimes be a challenge, so this vignette walks through how to do this.

Defining longitudinal data as a tsibble

The tools and workflows in brolgar are designed to work with a special tidy time series data frame called a tsibble. We can define our longitudinal data in terms of a time series to gain access to some really useful tools. To do so, we need to identify three components:

  1. The key variable in your data is the identifier of your individual.
  2. The index variable is the time component of your data.
  3. The regularity of the time interval (index). Longitudinal data typically has irregular time periods between measurements, but can have regular measurements.

Together, time index and key uniquely identify an observation with repeated measurements

The term key is used a lot in brolgar, so it is an important idea to internalise:

The key is the identifier of your individuals or series

Why care about defining longitudinal data as a time series? Once we account for this time series structure inherent in longitudinal data, we gain access to a suite of nice tools that simplify and accelerate how we work with time series data.

brolgar is built on top of the powerful tsibble package by Earo Wang, if you would like to learn more, see the official package documentation or read the paper.

Converting your longitudinal data to a time series

To convert longitudinal data into a “time series tibble”, a tsibble, we need to consider which variables identify:

  1. The individual, who would have repeated measurements. This is the key
  2. The time component, this is the index .
  3. The regularity of the time interval (index).

Together, time index and key uniquely identify an observation with repeated measurements

The vignette now walks through some examples of converting longitudinal data into a tsibble.

example data: wages

Let’s look at the wages data analysed in Singer & Willett (2003). This data contains measurements on hourly wages by years in the workforce, with education and race as covariates. The population measured was male high-school dropouts, aged between 14 and 17 years when first measured. Below is the first 10 rows of the data.

library(brolgar)
suppressPackageStartupMessages(library(dplyr))
slice(wages, 1:10) %>% knitr::kable()
id ln_wages xp ged xp_since_ged black hispanic high_grade unemploy_rate
31 1.491 0.015 1 0.015 0 1 8 3.21
31 1.433 0.715 1 0.715 0 1 8 3.21
31 1.469 1.734 1 1.734 0 1 8 3.21
31 1.749 2.773 1 2.773 0 1 8 3.30
31 1.931 3.927 1 3.927 0 1 8 2.89
31 1.709 4.946 1 4.946 0 1 8 2.49
31 2.086 5.965 1 5.965 0 1 8 2.60
31 2.129 6.984 1 6.984 0 1 8 4.80
36 1.982 0.315 1 0.315 0 0 9 4.89
36 1.798 0.983 1 0.983 0 0 9 7.40

To create a tsibble of the data we ask, “which variables identify…”:

  1. The key, the individual, who would have repeated measurements.
  2. The index, the time component.
  3. The regularity of the time interval (index).

Together, time index and key uniquely identify an observation with repeated measurements

From this, we can say that:

  1. The key is the variable id - the subject id, from 1-888.
  2. The index is the variable xp the experience in years an individual has.
  3. The data is irregular since the experience is a fraction of year that is not an integer.

We can use this information to create a tsibble of this data using as_tsibble

#> # A tsibble: 6,402 x 9 [!]
#> # Key:       id [888]
#>       id ln_wages    xp   ged xp_since_ged black hispanic high_grade
#>    <int>    <dbl> <dbl> <int>        <dbl> <int>    <int>      <int>
#>  1    31     1.49 0.015     1        0.015     0        1          8
#>  2    31     1.43 0.715     1        0.715     0        1          8
#>  3    31     1.47 1.73      1        1.73      0        1          8
#>  4    31     1.75 2.77      1        2.77      0        1          8
#>  5    31     1.93 3.93      1        3.93      0        1          8
#>  6    31     1.71 4.95      1        4.95      0        1          8
#>  7    31     2.09 5.96      1        5.96      0        1          8
#>  8    31     2.13 6.98      1        6.98      0        1          8
#>  9    36     1.98 0.315     1        0.315     0        0          9
#> 10    36     1.80 0.983     1        0.983     0        0          9
#> # … with 6,392 more rows, and 1 more variable: unemploy_rate <dbl>

Note that regular = FALSE, since we have an irregular time series

Note the following information printed at the top of wages

# A tsibble: 6,402 x 9 [!]
# Key:       id [888]
...

This says:

  • We have 6402 rows,
  • with 9 columns.

The ! at the top means that there is no regular spacing between series

The “key” variable is then listed - id, of which there 888.

example: heights data

The heights data is a little simpler than the wages data, and contains the average male heights in 144 countries from 1810-1989, with a smaller number of countries from 1500-1800.

It contains four variables:

  • country
  • continent
  • year
  • height_cm

To create a tsibble of the data we ask, “which variables identify…”:

  1. The key, the individual, who would have repeated measurements.
  2. The index, the time component.
  3. The regularity of the time interval (index).

In this case:

  • The individual is not a person, but a country
  • The time is year
  • The year is not regular because there are not measurements at a fixed year point.

This data is already a tsibble object, we can create a tsibble with the following code:

example: gapminder

The gapminder R package contains a dataset of a subset of the gapminder study (link). This contains data on life expectancy, GDP per capita, and population by country.

Let’s identify

  1. The key, the individual, who would have repeated measurements.
  2. The index, the time component.
  3. The regularity of the time interval (index).

This is in fact very similar to the heights dataset:

  1. The key is the country
  2. The index is the year

To identify if the year is regular, we can do a bit of data exploration using index_summary()

This shows us that the year is every five - so now we know that this is a regular longitudinal dataset, and can be encoded like so:

example: PISA data

The PISA study measures school students around the world on a series of math, reading, and science scores. A subset of the data looks like so:

Let’s identify

  1. The key, the individual, who would have repeated measurements.
  2. The index, the time component.
  3. The regularity of the time interval (index).

Here it looks like the key is the student_id, which is nested within school_id and country,

And the index is year, so we would write the following

as_tsibble(pisa, 
           key = c(country, school_id, student_id),
           index = year)

Unfortunately, we get this error:

Error: A valid tsibble must have distinct rows identified by key and index.
Please use `duplicates()` to check the duplicated rows.
Run `rlang::last_error()` to see where the error occurred.

This is a somewhat confusing error - we can check duplicates like so:

One thing to keep in mind here is that individual students are not measured repeatedly, but schools are. This means that we really shouldn’t include student_id in the tsibble, since they have no repeated measurements.

Understanding a bit more about the PISA data, the school_id and student_id are not unique across time. The id codes represent unique schools and students for a given year in a country and school. This is still interesting information, but illustrates the importance of understanding what is the longitudinal element in the data.

In this case, the longitudinal element is the country within a given year.

We can cast this as a tsibble, but we need to aggregate the data to each year and country. In doing so, it is important that we provide some summary statistics of each of the scores - we want to include the mean, and minimum and maximum of the math, reading, and science scores, so that we do not lose the information of the individuals.

The code below does this, first grouping by year and country, and then calculating the weighted mean for math, reading, and science. This can be done using the student weight variable stu_wgt, to get the survey weighted mean. The minimum and maximum are then calculated.

We can assess the regularity of the year like so:

We can now convert this into a tsibble:

Conclusion

This idea of longitudinal data is core to brolgar. Understanding what longitudinal data is, and how this can be linked to a time series representation of data helps us understand our data structure, and gives us access to more flexible tools. Other vignettes in the package will further show why the time series tsibble is useful.