knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.align='center', dpi = 92, fig.retina = 2 ) options(tibble.print_min = 4L, tibble.print_max = 4L)
When working with time series, groupdata2
allows us to quickly divide them into groups / windows.
library(groupdata2) library(dplyr) # %>% require(ggplot2, quietly = TRUE) # Attach if installed library(knitr) # kable
We will use the austres
dataset for this vignette. It contains numbers (in thousands) of Australian residents measured quarterly from March 1971 to March 1994.
Let's load the data and take a look at the first values.
timeSeriesFrame <- data.frame('residents' = austres) # Show structure of data frame str(timeSeriesFrame) # Show head of data timeSeriesFrame %>% head(12) %>% kable()
A visualization of the data. We see that the number of residents increases quite linearly with time.
# Plot of time series ggplot(timeSeriesFrame, aes(seq_along(residents), residents)) + geom_point() + labs(x = 'Measurement', y = 'Residents') + theme_light()
Let's say, that instead of having four measures per year, we want 1 measure every 3 years.
We can do this by making groups of 12 elements each with the greedy
method and use the means of each group as our measurements.
When using the greedy
method, we specify the desired group size. Every group, except the last, is guaranteed to have this size. The last group gets the elements that are left, i.e. it might be smaller or of the same size as the other groups.
ts = timeSeriesFrame %>% # Group data group(n = 12, method = 'greedy') %>% # Find means of each group dplyr::summarise(mean = mean(residents)) # Show new data ts %>% kable()
A visualization of the data.
# Plot of time series ggplot(ts, aes(.groups, mean)) + geom_point() + labs(x = 'Groups', y = 'Mean n of residents') + theme_light()
This procedure has left us with fewer datapoints, which could be useful if we had a very large data frame
to start with, or if we just wanted to describe the change in residents every 3rd year (or every year for that matter, by simply changing n
to 4
).
If we wanted to know which group had the largest increase in residents, we could find the range (difference between the max and min value) within each group instead of taking the mean.
ts <- timeSeriesFrame %>% # Group data group(n = 12, method = 'greedy') %>% # Find range of each group dplyr::summarise(range = diff(range(residents))) # Show new data ts %>% kable()
For the fun of it, let's say we want to make staircased groups inside the greedy groups, we just created.
When using the staircase
method we specify step size. Every group is 1 step larger than the previous group (e.g. with a step size of 5, group sizes would be 5,10,15,...).
By creating subgroups for every greedy group, the group size will "start over" for each greedy group.
When using the staircase method, the last group might not have the size of the second last group + step size. We want to make sure that it does have such size, so we use the helper tool %staircase%
to find a step size with a remainder of 0.
main_group_size = 12 # Loop through a list ranging from 1-30 for (step_size in c(1:30)){ # If the remainder is 0 if(main_group_size %staircase% step_size == 0){ # Print the step size print(step_size) } }
So our step size could be 2, 4 or 12. We pick a step size of 2, because it will yield the most subgroups for the example.
Now we will first make the greedy groups like before, then we will create subgroups with the staircase method.
In order not to overwrite the .groups
column from the first use of group()
, we will use the col_name
argument in group()
. As group()
groups the data frame
by the generated grouping factor (what a sentence!), a second call to it will create subgroups within the initial groups. Note that versions previous to v1.3.0
did not detect these groupings, why you had to run the second group()
call inside dplyr
's do()
function.
ts <- timeSeriesFrame %>% # Group data group(n = 12, method = 'greedy') %>% # Create subgroups group(n = 2, method = 'staircase', col_name = '.subgroups') # Show head of new data ts %>% head(24) %>% kable()
We can get the means of each subgroup. To do this, we first group by .groups
and then .subgroups
. Then, we find the mean number of residents for each subgroup. If we had just grouped by .subgroups
, we would have taken the mean of all the data points in each subgroup level. This would have left us with (in this case) 3 means, instead of 1 per subgroup level per main group level.
Now that we are at it, we might as well find the ranges for each subgroup as well.
ts_means <- ts %>% # Group by first .groups, then .subgroups group_by(.groups, .subgroups) %>% # Find the mean and range of each subgroup dplyr::summarise(mean = mean(residents), range = diff(range(residents))) # Show head of new data ts_means %>% head(9) %>% kable()
The differences in range follows the differences in number of measurements per subgroup.
Here is a visualization of the means per subgroup:
# Plot of time series ggplot(ts_means, aes(seq_along(mean), mean)) + geom_point() + labs(x = 'Subgroup', y = 'Mean n of residents') + theme_light()
Well done, you made it to the end of this introduction to groupdata2
! If you want to know more about the various methods and arguments, you can read the Description of groupdata2.
If you have any questions or comments to this vignette (tutorial) or groupdata2
, please send them to me at
r-pkgs@ludvigolsen.dk, or open an issue on the github page https://github.com/LudvigOlsen/groupdata2 so I can make improvements.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.