Posted in correlation, Data visualisation, python

## Correlation matrix – A useful tool to visualise relatedness between multiple variables

Correlations are a great way to show if the variables are associated with each other. The strength of correlation is often represented by the correlation coefficient, where values range between -1.0 and 1.0. A positive value indicates positive association while a negative value indicates negative association. While there are no fixed definition, a correlation value larger than 0.7 or smaller than -0.7 is usually considered as a strong correlation. To quickly visualise the correlation between multiple variables, correlation matrixes can be used. In my opinion, the Python programming language allows individuals to most quickly plot these matrices, which is what I will elaborate in this blog entry.

First, we load the dataset and packages into python, where I have assigned headers to be variables (ab30d, ab60d, ab90d and tcell7d) and the first column to be the subject ID column. We will assign this dataframe as “test_1”

```# Import packages
import csv
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

# Import data with 10 subjects into python. In this case only the top 2 rows will be shown in the output
```

Output file is as follows:

Next, we can visualise the correlation coefficient across all variables using the command:

```corrM = test_1.corr()
corrM
```

Output file showing all the correlation coefficient values is as follows:

We can also determine the p-values of the correlation by using the commands, as follows:

```from scipy import stats
from scipy.stats import pearsonr
pvals = pd.DataFrame([[pearsonr(test_1[c], test_1[y])[1] for y in test_1.columns] for c in test_1.columns],
columns=test_1.columns, index=test_1.columns)
pvals
```

Output file is as follows:

Finally, to plot the correlation matrix with the correlation coefficient values within, we execute the following command:

```corr = test_1.corr()