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.
This exercise is part of the course
Visualizing Time Series Data in R
Exercise 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
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create correlation matrix using Pearson method
# Create correlation matrix using Spearman method