How to Calculate Covariance and Correlation in R Programming

In this article, we will learn about how to calculate two important statistics – covariance and correlation in R programming.

Both covariance and correlation are indicators of relationship between two variables, in terms of how they move with respect tom each other. If they move in the same direction, there is a positive relationship, but if they move in the opposite direction (when one goes up, the other goes down), then there is a negative relationship. Learn more about covariance and correlation and their formulas.

While covariance indicates the direction of relationship between two variables, correlation indicates both the direction and the strength of the relationship. Correlation is actually a translation of covariance into a unit-less standardized measure that we can understand (-1.0 to 1.0).

Let’s take two vectors containing stock prices of two stocks over the past 10 days.

AAPL <- c(92.04, 93.59, 94.4, 95.6, 95.89, 94.99, 95.53, 95.94, 96.68, 96.98, 97.42, 96.87, 98.79, 98.78, 99.83, 99.87, 99.96, 99.43, 98.66, 97.34)

MSFT <- c(48.43, 49.44, 50.54, 51.17, 51.16, 51.17, 51.38, 51.38, 52.3, 52.59, 53.21, 53.51, 53.74, 53.7, 53.96, 53.09, 55.91, 55.8, 56.57, 56.73)

Covariance in R Programming

In R programming, covariance can be calculated using the cov() function. This function takes three parameters: the two vectors and a method to calculate the covariance. The default method is Pearson. The three methods available are "pearson", "kendall", and "spearman".

Applying this formula to our two stocks:

result_cov <- cov(AAPL, MSFT, method="spearman")

result_cov

[1] 29.92105

We have a positive covariance of 29.92.

Correlation in R Programming

We can calculate correlation of these two vectors using the cor() function in R programming. It takes the same three parameters as the cov() function.

result_cor <- cor(AAPL, MSFT, method="spearman")

result_cor

[1] 0.8555307

We have a positive correlation of 0.85 which indicates a very strong relationship between the two stocks.

Converting Covariance to Correlation

R also provides a function to convert covariance to correlation. This function cov2cor() function takes only one input. This method, however, doesn’t work with vectors. It converts the covariance matrix into a correlation matrix of values. The matrix must be a square matrix. In most cases you won’t need to use it as you can directly calculate both the statistics from your source data.

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book includes PDFs, explanations, instructions, data files, and R code for all examples.

Get the Bundle for $39 (Regular $57)
JOIN 30,000 DATA PROFESSIONALS

Free Guides - Getting Started with R and Python

Enter your name and email address below and we will email you the guides for R programming and Python.

Data Science in Finance: 9-Book Bundle

Data Science in Finance Book Bundle

Master R and Python for financial data science with our comprehensive bundle of 9 ebooks.

What's Included:

  • Getting Started with R
  • R Programming for Data Science
  • Data Visualization with R
  • Financial Time Series Analysis with R
  • Quantitative Trading Strategies with R
  • Derivatives with R
  • Credit Risk Modelling With R
  • Python for Data Science
  • Machine Learning in Finance using Python

Each book comes with PDFs, detailed explanations, step-by-step instructions, data files, and complete downloadable R code for all examples.