Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/conig/corx

corx: Create and Format Correlation Matrices
https://github.com/conig/corx

Last synced: 3 months ago
JSON representation

corx: Create and Format Correlation Matrices

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# corx

[![R-CMD-check](https://github.com/conig/corx/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/conig/corx/actions/workflows/R-CMD-check.yaml)
[![downloads](https://cranlogs.r-pkg.org/badges/grand-total/corx)](https://cran.r-project.org/package=corx)

'corx' aims to be a Swiss Army knife for correlation matrices. Formatting correlation matrices for academic tables can be challenging. 'corx' does all the heavy lifting for you. It runs the correlations, and stores all relevant results in a list. Results can be formatted into data.frames which can then easily be rendered into tables in a variety of formats.

## Installation

You can install the released version of corx from [CRAN](https://CRAN.R-project.org) with:

``` r
install.packages("corx")
```

To try features in development, you can install corx from github

```r
remotes::install_github("conig/corx@devel")
```

## Example

## Basic usage

The simplest way to use corx is to supply it with a data.frame, which houses numeric variables.

```{r example}
library(corx)
x <- corx(mtcars)
x
```

## Partial correlations

To calculate correlations controlling for other variables, use the 'z' argument.

```{r}
x <- corx(mtcars, z = wt, caption = "Correlations controlling for weight")
x
```

## Asymmetric correlation matrices

Sometimes you only want the relationships for a subset of variables. Asymmetric matrices are useful in these instances. The arguments 'x' and 'y' can be used to achieve this. 'x' sets row variables, 'y' sets column variables.

```{r}
x <- corx(mtcars, x = c(mpg, wt))
x
```

```{r}
x <- corx(mtcars,
x = c(mpg, wt),
y = c(hp, gear, am))
x
```

## Changing formatting

Users can further customise the table for publication. For instance, the numbers of significance stars can be changed, the area above the diagonal omitted, and captions and notes added.

```{r}
x <- corx(mtcars[,1:5],
stars = c(0.05),
triangle = "lower",
caption = "An example correlation matrix")
x
```

## Adding descriptive statistics

We can also add in descriptive statistics easily.

```{r}
x <- corx(mtcars[,1:5],
stars = c(0.05, 0.01, 0.001),
triangle = "lower",
caption = "An example correlation matrix",
describe = c(M = mean, SD = sd, kurtosis))
x
```

To add descriptive columns describe can be set to any combination of the following values: c("mean","sd","median","iqr","var","skewness","kurtosis").

Alternatively, you can pass a list of named functions:

```{r}
x <- corx(mtcars[,1:8], describe = list(Mean = function(x) mean(x),
SD = function(x) sd(x)))
x
```

## Making tables

Corx objects can be passed directly to papaja::apa_table(), or knitr::kable().

```{r}
corx(mtcars[, 1:5], triangle = "lower", describe = c(mean, sd)) |>
knitr::kable(caption = "My correlation matrix")
```

## Making plots

### Correlation matrices

There are many useful functions for plotting correlation matrices. 'corx' contains a plot function which uses the 'ggcorrplot' package.

```{r, fig.width = 15}
plot(x)
```

### Multidimensional scaling

Multidimensional scaling enables similarities between variables to be converted to 2D distances. This lets us visualise how variables cluster together.

```{r out.width = "50%", fig.asp=1}
plot_mds(x)
```

We can see that variables in mtcars cluster together in two separate groups.
If we want to highlight this we can request two clusters to be marked.

```{r out.width = "50%", fig.asp=1}
plot_mds(x, 2)
```

You can see that miles per gallon, the number of cylinders, the displacement rate, and the weight of the car are all closely related.