Chapter 10 Summarizing data within subgroups

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

# A tibble: 2 x 3
  sex      bmi waist.circ
  <fct>  <dbl>      <dbl>
1 Female  17.6       63.6
2 Male    17.7       62.5
# A tibble: 4 x 5
  bmi.cat          mean    sd median skew_1
  <fct>           <dbl> <dbl>  <dbl>  <dbl>
1 1 Underweight    54.9  7.63   53.9  0.136
2 2 Normal weight  61.0  9.10   59.2  0.193
3 3 Overweight     71.1 11.8    72   -0.075
4 4 Obese          79.9 15.0    79.9 -0.003

While patients in the heavier groups generally had higher waist circumferences, this is not inevitably the case.

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

10.2 Using the by function to summarize groups numerically

We can summarize our data numerically in multiple ways, but to use the favstats or Hmisc::describe tools to each individual BMI subgroup separately, we might consider applying the by function.

nyfs1$bmi.cat: 1 Underweight
  min   Q1 median   Q3  max mean   sd  n missing
 42.5 49.2   53.9 62.4 68.5 54.9 7.63 42       0
-------------------------------------------------------- 
nyfs1$bmi.cat: 2 Normal weight
  min   Q1 median Q3  max mean  sd   n missing
 44.1 53.8   59.2 68 85.5   61 9.1 926       0
-------------------------------------------------------- 
nyfs1$bmi.cat: 3 Overweight
  min   Q1 median   Q3  max mean   sd   n missing
 49.3 60.8     72 80.6 98.3 71.1 11.8 237       0
-------------------------------------------------------- 
nyfs1$bmi.cat: 4 Obese
  min   Q1 median   Q3 max mean sd   n missing
 52.1 66.7   79.9 91.6 112 79.9 15 211       0

As shown below, we could do this in pieces with dplyr, but the by approach can be faster for this sort of thing.

# A tibble: 4 x 10
  bmi.cat           min    Q1 median    Q3   max  mean    sd     n missing
  <fct>           <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl> <int>   <int>
1 1 Underweight    42.5  49.2   53.9  62.4  68.5  54.9  7.63    42       0
2 2 Normal weight  44.1  53.8   59.2  68    85.5  61.0  9.10   926       0
3 3 Overweight     49.3  60.8   72    80.6  98.3  71.1 11.8    237       0
4 4 Obese          52.1  66.6   79.9  91.6 112.   79.9 15.0    211       0

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.

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.1 Augmenting the Boxplot with the Sample Mean

Often, we want to augment such a plot, perhaps with the sample mean within each category, so as to highlight skew (in terms of whether the mean is meaningfully different from the median.)

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 overlap, consider the comparison of ages across sex.

In this case, the overlap in the notches suggests that the median ages in the population of interest don’t necessarily differ by sex.

10.4 Using Multiple Histograms to Make Comparisons

We can make an array of histograms to describe multiple groups of data, using ggplot2 and the notion of faceting our plot.

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 very new and attractive 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.38

And here’s a ridgeline plot for the triceps skinfold. 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.33

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.33

References

Grolemund, Garrett, and Hadley Wickham. 2017. 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.