library(dplyr)
library(corrr)
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

corrr is a package for exploring correlations in R. It makes it possible to easily perform routine tasks when exploring correlation matrices such as ignoring the diagonal, focusing on the correlations of certain variables against others, or rearranging and visualizing the matrix in terms of the strength of the correlations.

Using corrr

Using corrr starts with correlate(), which acts like the base correlation function cor(). It differs by defaulting to pairwise deletion, and returning a correlation data frame (cor_df) of the following structure:

To work with further, let's create a correlation data frame using correlate() from the mtcars data that comes with R:

library(corrr)
d <- correlate(mtcars, quiet = TRUE)
d

Why a correlation data frame?

At first, a correlation data frame might seem like an unnecessary complexity compared to the traditional matrix. However, the purpose of corrr is to help use explore these correlations, not to do mathematical or statistical operations. Thus, by having the correlations in a data frame, we can make use of packages that help us work with data frames like dplyr, tidyr, ggplot2, and focus on using data pipelines. Lets look at some examples:

library(dplyr)

# Filter rows to occasions in which cyl has a correlation of .7 or more with
# another variable.
d %>% filter(cyl > .7)

# Select the mpg, cyl and disp columns (and term)
d %>% select(term, mpg, cyl, disp)

# Combine above in a single pipeline
d %>%
  filter(cyl > .7) %>% 
  select(term, mpg, cyl, disp)

Furthermore, by having the diagonal set to missing, we don't need to put in extra effort to ignore them when summarizing the correlations. For example:

# Compute mean of each column
library(purrr)
d %>% 
  select(-term) %>% 
  map_dbl(~ mean(., na.rm = TRUE))

API

As the above section suggests, the corrr API is designed with data pipelines in mind (e.g., to use %>% from the magrittr package). After correlate(), the primary corrr functions take a cor_df as their first argument, and return a cor_df or tbl (or output like a plot). These functions serve one of three purposes:

Internal changes (cor_df out):

Reshape structure (tbl or cor_df out):

Output/visualizations (console/plot out):

By combing these functions in data pipelines, it's possible to easily explore your correlations.

For example, lets focus on the correlations of mpg and cyl with all the others:

d %>% focus(mpg, cyl)

Or maybe we want to focus in on a few variables (mirrored in rows too) and print the correlations without an upper triangle and fashioned to look nice:

d %>%
  focus(mpg:drat, mirror = TRUE) %>%  # Focus only on mpg:drat
  shave() %>% # Remove the upper triangle
  fashion()   # Print in nice format 

Alternatively, we can visualize these correlations (let's clear the lower triangle for a change):

d %>%
  focus(mpg:drat, mirror = TRUE) %>%
  shave(upper = FALSE) %>%
  rplot()     # Plot

Perhaps we'd like to rearrange the correlations so that the plot becomes easier to interpret. In this case, we can add rearrange() into our pipeline before shaving one of the triangles (we'll take correlation sign into account with absolute = FALSE).

d %>%
  focus(mpg:drat, mirror = TRUE) %>%
  rearrange(absolute = FALSE) %>% 
  shave() %>%
  rplot()

Other Resources

For other resources about how to use corrr, you'll find plenty of posts explaining functions at blogR, or keep up to date with these on Twitter by following \@drsimonj.



tidymodels/corrr documentation built on Jan. 27, 2024, 8:41 a.m.