CommencerCommencer gratuitement

Correlation matrix

What if you want to evaluate the relationship between multiple time series? The most common tool to use is a correlation matrix, which is a table showing correlation coefficients between pairs of variables. Several types of correlations exist but the most used ones are:

  • Pearson correlation: measures the linear relationship between 2 variables
  • Spearman rank correlation: measures the statistical dependency between the ranking of 2 variables (not necessarily linear)

The latter is used when there is no assumption made on the distribution of the data. All this is achieved in R using the function cor(). You can use the method argument to select the desired correlation type. "pearson" is the default method, but you can specify "spearman" as well.

In this exercise, you will calculate the correlation matrix of the data provided in the dataset my_data containing the returns for 5 stocks: ExxonMobile, Citigroup, Microsoft, Dow Chemical and Yahoo.

Cet exercice fait partie du cours

Visualizing Time Series Data in R

Afficher le cours

Instructions

  • Calculate the correlation matrix between the 5 return times series using the Pearson method
  • Calculate the correlation matrix between the 5 return times series using the Spearman method

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Create correlation matrix using Pearson method


# Create correlation matrix using Spearman method

Modifier et exécuter le code