brolgar helps you browse over longitudinal data graphically and analytically in R, by providing tools to:
This helps you go from the “plate of spaghetti” plot on the left, to “interesting observations” plot on the left.
To efficiently look at your longitudinal data, we assume it is a time series, with irregular time periods between measurements. This might seem strange, (that’s OK!), so remember these two things:
Together, the index and key uniquely identify an observation.
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
So in the
wages data, we have the following setup:
wages <- as_tsibble(x = wages, key = id, index = xp, regular = FALSE)
as_tsibble() takes wages, and a
index, and we state the
regular = FALSE (since there are not regular time periods between measurements).
This is done using
as_tsibble, which turns out data into a
tsibble object - a powerful data abstraction made available in the
tsibble package by Earo Wang, if you would like to learn more about
tsibble, see the official package documentation or read the paper.
If you want to learn more about what longitudinal data as a time series, you can read more in the vignette, “Longitudinal Data Structures”.
Exploring longitudinal data can be challenging when there are many individuals. It is difficult to look at all of them!
You often get a “plate of spaghetti” plot, with many lines plotted on top of each other. You can avoid the spaghetti by looking at a random subset of the data using tools in
dplyr, you can use
sample_n() to sample
n observations, or
sample_frac() to look at a
fraction of observations.
set.seed(2019-7-15-1300) wages %>% sample_n_keys(size = 5) %>% ggplot(aes(x = xp, y = ln_wages, group = id)) + geom_line()
And what if you want to create many of these plots?
brolgar provides some clever facets to help make it easier to explore your data.
facet_sample() allows you to specify the number of keys per facet, and the number of facets with
n_facets. It splits the data into 12 facets with 5 per facet by default:
set.seed(2019-07-23-1937) ggplot(wages, aes(x = xp, y = ln_wages, group = id)) + geom_line() + facet_sample()
You can see other facets (e.g.,
facet_strata) and data visualisations you can make in brolgar in the Visualisation Gallery.
You can extract
features of longitudinal data using the
features function, from
fabletools. These features all begin with
feat_. You can, for example, find those whose values only increase or decrease with
wages %>% features(ln_wages, feat_monotonic) #> # A tibble: 888 x 5 #> id increase decrease unvary monotonic #> <int> <lgl> <lgl> <lgl> <lgl> #> 1 31 FALSE FALSE FALSE FALSE #> 2 36 FALSE FALSE FALSE FALSE #> 3 53 FALSE FALSE FALSE FALSE #> 4 122 FALSE FALSE FALSE FALSE #> 5 134 FALSE FALSE FALSE FALSE #> 6 145 FALSE FALSE FALSE FALSE #> 7 155 FALSE FALSE FALSE FALSE #> 8 173 FALSE FALSE FALSE FALSE #> 9 206 TRUE FALSE FALSE TRUE #> 10 207 FALSE FALSE FALSE FALSE #> # … with 878 more rows
You can read more about creating and using features in the Finding Features vignette.
You can also use
n_obs() inside features to return the number of observations for each key:
wages %>% features(id, n_obs) #> # A tibble: 888 x 2 #> id n_obs #> <int> <int> #> 1 31 8 #> 2 36 10 #> 3 53 8 #> 4 122 10 #> 5 134 12 #> 6 145 9 #> 7 155 11 #> 8 173 6 #> 9 206 3 #> 10 207 11 #> # … with 878 more rows
This returns a dataframe, with one row per key, and the number of observations for each key.
This could be further summarised to get a sense of the patterns of the number of observations:
brolgar provides other useful functions to explore your data, which you can read about in the exploratory modelling and Identify Interesting Observations vignettes. As a taster, here are some of the figures you can produce:
Please note that the
brolgar project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.
This version of brolgar was been forked from tprvan/brolgar, and has undergone breaking changes to the API.
Thank you to Mitchell O’Hara-Wild and Earo Wang for many useful discussions on the implementation of brolgar, as it was heavily inspired by the
feasts package from the
tidyverts. I would also like to thank Tania Prvan for her valuable early contributions to the project, as well as Stuart Lee for helpful discussions.