correlation

:link: Methods for Correlation Analysis

311
41
R

output: github_document

correlation

library(ggplot2)
library(poorman)
library(correlation)

options(digits = 2)

knitr::opts_chunk$set(
  collapse = TRUE,
  dpi = 300,
  message = FALSE,
  warning = FALSE,
  fig.path = "man/figures/"
)

DOI total status

correlation is an easystats package focused on correlation analysis. It’s lightweight, easy to use, and allows for the computation of many different kinds of correlations, such as partial correlations, Bayesian correlations, multilevel correlations, polychoric correlations, biweight, percentage bend or Sheperd’s Pi correlations (types of robust correlation), distance correlation (a type of non-linear correlation) and more, also allowing for combinations between them (for instance, Bayesian partial multilevel correlation).

Citation

You can cite the package as follows:

Makowski, D., Ben-Shachar, M. S., Patil, I., & Lüdecke, D. (2020). Methods and algorithms for correlation analysis in R. Journal of Open Source Software,
5(51), 2306. https://doi.org/10.21105/joss.02306

Makowski, D., Wiernik, B. M., Patil, I., Lüdecke, D., & Ben-Shachar, M. S. (2022). correlation: Methods for correlation analysis [R package]. https://CRAN.R-project.org/package=correlation (Original work published 2020)

Installation

CRAN correlation status badge R-CMD-check
codecov

The correlation package is available on CRAN, while its latest development version is available on R-universe (from rOpenSci).

Type Source Command
Release CRAN install.packages("correlation")
Development R-universe install.packages("correlation", repos = "https://easystats.r-universe.dev")

Once you have downloaded the package, you can then load it using:

library("correlation")

Tip

Instead of library(correlation), use library(easystats).
This will make all features of the easystats-ecosystem available.

To stay updated, use easystats::install_latest().

Documentation

Check out package website for documentation.

Features

The correlation package can compute many different types of correlation,
including:

Pearson’s correlation

Spearman’s rank correlation

Kendall’s rank correlation

Biweight midcorrelation

Distance correlation

Percentage bend correlation

Shepherd’s Pi correlation

Blomqvist’s coefficient

Hoeffding’s D

Gamma correlation

Gaussian rank correlation

Point-Biserial and biserial correlation

Winsorized correlation

Polychoric correlation

Tetrachoric correlation

Multilevel correlation

An overview and description of these correlations types is available here. Moreover,
many of these correlation types are available as partial or within a
Bayesian framework.

Examples

The main function is correlation(), which builds on top of cor_test() and comes with a number of possible options.

Correlation details and matrix

results <- correlation(iris)
results

The output is not a square matrix, but a (tidy) dataframe with all correlations tests per row. One can also obtain a matrix using:

summary(results)

Note that one can also obtain the full, square and redundant matrix using:

summary(results, redundant = TRUE)
library(see)

results %>%
  summary(redundant = TRUE) %>%
  plot()

Correlation tests

The cor_test() function, for pairwise correlations, is also very convenient for making quick scatter plots.

plot(cor_test(iris, "Sepal.Width", "Sepal.Length"))

Grouped dataframes

The correlation() function also supports stratified correlations, all within the
tidyverse workflow!

iris %>%
  select(Species, Sepal.Length, Sepal.Width, Petal.Width) %>%
  group_by(Species) %>%
  correlation()

Bayesian Correlations

It is very easy to switch to a Bayesian framework.

correlation(iris, bayesian = TRUE)

Tetrachoric, Polychoric, Biserial, Biweight…

The correlation package also supports different types of methods, which can
deal with correlations between factors!

correlation(iris, include_factors = TRUE, method = "auto")

Partial Correlations

It also supports partial correlations (as well as Bayesian partial correlations).

iris %>%
  correlation(partial = TRUE) %>%
  summary()

Gaussian Graphical Models (GGMs)

Such partial correlations can also be represented as Gaussian Graphical
Models
(GGM), an increasingly popular tool in psychology. A GGM traditionally
include a set of variables depicted as circles (“nodes”), and a set of lines
that visualize relationships between them, which thickness represents the
strength of association (see Bhushan et al., 2019).

library(see) # for plotting
library(ggraph) # needs to be loaded

plot(correlation(mtcars, partial = TRUE)) +
  scale_edge_color_continuous(low = "#000004FF", high = "#FCFDBFFF")

Multilevel Correlations

It also provide some cutting-edge methods, such as Multilevel (partial)
correlations. These are are partial correlations based on linear mixed-effects
models that include the factors as random effects. They can be see as
correlations adjusted for some group (hierarchical) variability.

iris %>%
  correlation(partial = TRUE, multilevel = TRUE) %>%
  summary()

However, if the partial argument is set to FALSE, it will try to convert the
partial coefficient into regular ones.These can be converted back to full
correlations:

iris %>%
  correlation(partial = FALSE, multilevel = TRUE) %>%
  summary()

Contributing and Support

In case you want to file an issue or contribute in another way to the package, please follow this guide. For questions about the functionality, you may either contact us via email or also file an issue.

Code of Conduct

Please note that this project is released with a
Contributor Code of Conduct. By participating in this project you agree to abide by its terms.