corrr

repository·main·Indexed 20 days ago

https://github.com/tidymodels/corrr

An R package for exploring correlations using tidy data principles. It transforms correlation matrices into correlation data frames (cor_df), allowing for seamless integration with the tidyverse for manipulation and visualization. Key features include the correlate() function for creating correlation data frames, tools for reshaping and transforming data such as shave(), rearrange(), focus(), and stretch(), and visualization functions like rplot() and network_plot().

Tokens
849
Snippets
5
Records
5
Agent score
20%

What's inside corrr

  1. How corrr works with correlation data frames (cor_df)

    main

    Unlike base R's cor() which returns a correlation matrix, corrr focuses on creating and working with data frames of correlations (cor_df). This allows you to use tidyverse tools (like dplyr, tidyr, and ggplot2) to explore correlations.

    When you call correlate(), it returns a tbl with the additional class cor_df. The structure includes:

    • A term column (representing the variable name).
    • Correlation values for other variables.
    • Standardized variances (the matrix diagonal) are set to NA so they can be easily ignored in calculations or visualizations.

    Note: As of version 0.4.3, the first column is named term. In previous versions, it was named rowname.

    # Example of the resulting structure
    x <- correlate(d)
    class(x)
    #> [1] "cor_df" "tbl_df" "tbl" "data.frame"
  2. Install corrr

    main

    You can install corrr from CRAN for the latest stable release, or from GitHub for the latest development version.

    CRAN installation:

    install.packages("corrr")

    GitHub installation:

    # install.packages("remotes") 
    remotes::install_github("tidymodels/corrr")
  3. Visualize correlations with rplot() and network_plot()

    main

    You can visualize correlation structures using rplot() or network_plot().

    • rplot(x): Uses shapes to represent correlation values.
    • network_plot(x, min_cor = ...): Visualizes correlations as a network, where edges are drawn based on a minimum correlation threshold (min_cor).
    # Network plot example
    datasets::airquality %>%
      correlate() %>%
      network_plot(min_cor = .2)
  4. Use correlate() to create a correlation data frame

    main

    The correlate() function is the entry point for the corrr API. It acts similarly to the base R cor() function but defaults to pairwise deletion and returns a cor_df object instead of a matrix.

    It also supports database tables; correlate() will automatically push calculations to the database, collect the results in R, and return a cor_df object.

    library(corrr)
    # x will be a cor_df object
    x <- correlate(d)
  5. Transform and manipulate cor_df objects

    main

    After calling correlate(), you can use several functions to modify the cor_df structure within a pipeline:

    Internal changes (returns cor_df)

    • shave(): Sets the upper or lower triangle of the correlation table to NA.
    • rearrange(): Reorders columns and rows based on correlation strengths.

    Reshape structure (returns tbl or cor_df)

    • focus(): Selects specific columns and rows to focus on.
    • stretch(): Converts the data into a long format.

    Output and Visualizations

    • fashion(): Formats the correlations for pretty printing in the console.
    • rplot(): Creates a plot where correlations are represented by shapes.
    • network_plot(): Creates a network visualization of the correlations.
    # Example pipeline: focus, rearrange, shave, and fashion
    x <- datasets::mtcars %>%
           correlate() %>%
           focus(-cyl, -vs, mirror = TRUE) %>%
           rearrange() %>%
           shave()
    
    fashion(x)