Chapter 10 Summarizing data within subgroups

10.1 Using dplyr and summarise to build a tibble of summary information

Suppose we want to understand how the subjects whose diet involved consuming much more than usual yesterday compare to those who consumer their usual amount, or to those who consumed much less than usual, in terms of the energy they consumed, as well as the protein. We might start by looking at the medians and means.

Warning: funs() is soft deprecated as of dplyr 0.8.0
Please use a list of either functions or lambdas: 

  # Simple named list: 
  list(mean = mean, median = median)

  # Auto named with `tibble::lst()`: 
  tibble::lst(mean, median)

  # Using lambdas
  list(~ mean(., trim = .2), ~ median(., na.rm = TRUE))
This warning is displayed once per session.
# A tibble: 4 x 5
  diet_yesterday      energy_median protein_median energy_mean protein_mean
  <chr>                       <dbl>          <dbl>       <dbl>        <dbl>
1 1_Much more than u~          2098           69.4       2150.         75.1
2 2_Usual                      1794           61.3       1858.         67.0
3 3_Much less than u~          1643           53.9       1779.         60.1
4 <NA>                         4348          155.        4348         155. 

Perhaps we should restrict ourselves to the people who were not missing the diet_yesterday category, and look now at their sugar and water consumption.

# A tibble: 3 x 5
  diet_yesterday         energy protein sugar water
  <chr>                   <dbl>   <dbl> <dbl> <dbl>
1 1_Much more than usual   2098    69.4  137.  500 
2 2_Usual                  1794    61.3  114.  385.
3 3_Much less than usual   1643    53.9  115.  311.

It looks like the children in the “Much more than usual” category consumed more energy, protein, sugar and water than the children in the other two categories. Let’s draw a picture of this.

We can see that there is considerable overlap in these distributions, regardless of what we’re measuring.

10.2 Another Example

Suppose now that we ask a different question. Do kids in larger categories of BMI have larger waist circumferences?

# A tibble: 5 x 5
  bmi_cat        mean     sd median skew_1
  <chr>         <dbl>  <dbl>  <dbl>  <dbl>
1 1_Underweight  55.2   7.58   54.5   0.09
2 2_Normal       NA   NaN      NA    NA   
3 3_Overweight   72.3  11.9    74    -0.14
4 4_Obese        NA   NaN      NA    NA   
5 <NA>           NA   NaN      NA    NA   

Oops. Looks like we need to filter for cases with complete data on both BMI category and waist circumference in order to get meaningful results. We should add a count, too.

# A tibble: 4 x 6
  bmi_cat       count  mean    sd median skew_1
  <chr>         <int> <dbl> <dbl>  <dbl>  <dbl>
1 1_Underweight    41  55.2  7.58   54.5   0.09
2 2_Normal        917  61.2  9.35   59.5   0.19
3 3_Overweight    258  72.3 11.9    74    -0.14
4 4_Obese         294  85.6 17.1    86.8  -0.07

Or, we could use something like favstats from the mosaic package, which automatically accounts for missing data, and omits it when calculating summary statistics within each group.

        bmi_cat  min   Q1 median   Q3   max mean    sd   n missing
1 1_Underweight 42.5 49.3   54.5 62.4  68.5 55.2  7.58  41       0
2      2_Normal 44.1 53.9   59.5 68.4  89.2 61.2  9.35 917       3
3  3_Overweight 49.3 62.3   74.0 81.2 105.3 72.3 11.94 258       0
4       4_Obese 52.1 72.7   86.8 96.8 144.7 85.6 17.11 294       1

While patients in the heavier groups generally had higher waist circumferences, the standard deviations suggest there may be some meaningful overlap. Let’s draw the picture, in this case a comparison boxplot accompanying a violin plot.

The data transformation with dplyr cheat sheet found under the Help menu in RStudio is a great resource. And, of course, for more details, visit Grolemund and Wickham (2019).

10.3 Boxplots to Relate an Outcome to a Categorical Predictor

Boxplots are much more useful when comparing samples of data. For instance, consider this comparison boxplot describing the triceps skinfold results across the four levels of BMI category.

Warning: Removed 21 rows containing non-finite values (stat_boxplot).

Again, we probably want to omit those missing values (both in bmi_cat and triceps_skinfold) and also eliminate the repetitive legend (guides) on the right.

As always, the boxplot shows the five-number summary (minimum, 25th percentile, median, 75th percentile and maximum) in addition to highlighting candidate outliers.

10.3.2 Adding Notches to a Boxplot

Notches are used in boxplots to help visually assess whether the medians of the distributions across the various groups actually differ to a statistically detectable extent. Think of them as confidence regions around the medians. If the notches do not overlap, as in this situation, this provides some evidence that the medians in the populations represented by these samples may be different.

There is no overlap between the notches for each of the four categories, so we might reasonably conclude that the true median triceps skinfold values across the four categories are statistically significantly different.

For an example where the notches do overlap, consider the comparison of plank times by BMI category.

The overlap in the notches (for instance between Underweight and Normal) suggests that the median plank times in the population of interest don’t necessarily differ in a meaningful way by BMI category, other than perhaps the Obese group which may have a shorter time.

These data are somewhat right skewed. Would a logarithmic transformation in the plot help us see the patterns more clearly?

10.7 A Ridgeline Plot

Some people don’t like violin plots - for example, see https://simplystatistics.org/2017/07/13/the-joy-of-no-more-violin-plots/. A relatively new alternative plot is available. This shows the distribution of several groups simultaneously, especially when you have lots of subgroup categories, and is called a ridgeline plot8.

Picking joint bandwidth of 3.47

And here’s a ridgeline plot for the triceps skinfolds. We’ll start by sorting the subgroups by the median value of our outcome (triceps skinfold) in this case, though it turns out not to matter. We’ll also add some color.

Picking joint bandwidth of 1.37

For one last example, we’ll look at age by BMI category, so that sorting the BMI subgroups by the median matters, and we’ll try an alternate color scheme, and a theme specially designed for the ridgeline plot.

Picking joint bandwidth of 1.15

References

Grolemund, Garrett, and Hadley Wickham. 2019. R for Data Science. O’Reilly. http://r4ds.had.co.nz/.


  1. These were originally called joy plots, and the tools were contained in the ggjoy package but that name and package has been deprecated in favor of ggridges.