How To Calculate A Correlation Matrix

The correlation (r) is a measure of the linear relationship between two variables. For example, leg length and torso length are highly correlated; height and weight are less highly correlated, and height and name length (in letters) are uncorrelated.

A perfect positive correlation: r = 1. (When one goes up the other goes up) A perfect negative correlation: r = -1 (When one goes up, the other goes down) No correlation: r = 0 (There is no linear relationship)

A correlation matrix is a matrix of many correlations.

Computing a Correlation Matrix with R

Step 1

Get the data. If your data is in Excel, the easiest method is to save it as a .csv file (In Excel 7, click "File", then "Save as," then "other formats." Then in "Save as type," scroll down to CSV (comma separated values). Each row should have data on one subject, and each column should be one variable.

Step 2

Read the data into R using read.csv. For instance, if your data is in "c:\mydisk\mydir\data.csv" enter mydata <- read.csv ("c:/mydisk/mydir/data.csv").

Step 3

Calculate the correlation matrix using cor(). For example: cor(mydata). Or, you can store the correlation matrix as an object for later use, using: cormat <- cor(mydata).

Computing a Correlation Matrix with SAS

Step 1

Get the data. SAS can read data in many formats. If you store your data in Excel, have one subject on each row and one variable in each column

Step 2

Read the data into SAS. You can use the IMPORT wizard to get your data. Click on "File," then "Import data," then choose a data type using the drop-down menu. Click "Next" and navigate to your data, then click "Finish."

Step 3

Calculate the correlation matrix. If your data is saved in SAS as mydata, with variables VAR1, VAR2 and VAR3, then type: PROC CORR data = mydata; VAR var1 var2 var3; RUN;

TL;DR (Too Long; Didn't Read)

In both SAS and R, there are options for different types of correlations (e,.g Pearson's, Spearman's). Remember that correlations only find linear relationships. If the relationship between two correlations is not linear, correlations are not a good choice. To get more help with R, start R, then type ?cor.

Warning

If the second reference below (R Help) does not work, then start R and type ?cor.

Cite This Article

MLA

Flom, Peter. "How To Calculate A Correlation Matrix" sciencing.com, https://www.sciencing.com/calculate-correlation-matrix-6716780/. 24 April 2017.

APA

Flom, Peter. (2017, April 24). How To Calculate A Correlation Matrix. sciencing.com. Retrieved from https://www.sciencing.com/calculate-correlation-matrix-6716780/

Chicago

Flom, Peter. How To Calculate A Correlation Matrix last modified March 24, 2022. https://www.sciencing.com/calculate-correlation-matrix-6716780/

Recommended