When you are presented with longitudinal data, it is useful to summarise the data into a format where you have one row per key. That means one row per unique identifier of the data - if you aren’t sure what this means, see the vignette, “Longitudinal Data Structures”.

So, say for example you wanted to find features in the wages data, which looks like this:

You can return a dataset that has one row per key, with say the minimum value for ln_wages, for each key:

#> # A tibble: 888 x 2
#>       id   min
#>    <int> <dbl>
#>  1    31 1.43 
#>  2    36 1.80 
#>  3    53 1.54 
#>  4   122 0.763
#>  5   134 2.00 
#>  6   145 1.48 
#>  7   155 1.54 
#>  8   173 1.56 
#>  9   206 2.03 
#> 10   207 1.58 
#> # … with 878 more rows

This then allows us to summarise these kinds of data, to say for example find the distribution of minimum values:

library(ggplot2)
ggplot(wages_min,
       aes(x = min)) + 
  geom_density()

We call these summaries features of the data.

This vignette discusses how to calculate these features of the data.

Calculating features

We can calculate features of longitudinal data using the features function (from fabletools, made available in brolgar).

features works by specifying the data, the variable to summarise, and the feature to calculate:

features(<DATA>, <VARIABLE>, <FEATURE>)

or with the pipe:

<DATA> %>% features(<VARIABLE>, <FEATURE>)

As an example, we can calculate a five number summary (minimum, 25th quantile, median, mean, 75th quantile, and maximum) of the data using feat_five_num, like so:

Here we are taking the wages data, piping it to features, and then telling it to summarise the ln_wages variable, using feat_five_num.

There are several handy functions for calculating features of the data that brolgar provides. These all start with feat_.

You can, for example, find those whose values only increase or decrease with feat_monotonic:

These could then be used to identify individuals who only increase like so:

They could then be joined back to the data

And these could be plotted:

To get a sense of the data and where ti came from, we could create a plot with gghighlight to highlight those that only increase, by using gghighlight(increase) - since increase is a logical, this tells gghighlight to highlight those that are TRUE.

You can explore the available features, see the function References

Creating your own Features

To create your own features or summaries to pass to features, you provide a named list of functions. For example:

These are then passed to features like so:

Inside brolgar, the features are created with the following syntax:

Here the functions b_ are functions with a default of na.rm = TRUE, and in the cases of quantiles, they use type = 8, and names = FALSE.

Accessing sets of features

If you want to run many or all features from a package on your data you can collect them all with feature_set. For example:

library(fabletools)
feat_brolgar <- feature_set(pkgs = "brolgar")
length(feat_brolgar)
#> [1] 6

You could then run these like so:

For more information see ?fabletools::feature_set

Registering a feature in a package

If you create features in your own package and want to make them accessible with feature_set, do the following.

Functions can be registered via fabletools::register_feature(). To register features in a package, I create a file called zzz.R, and use the .onLoad(...) function to set this up on loading the package: