Get startedGet started for free

Multivariate time series

1. Multivariate time series

We've seen before that as the number of time series grows, the visualization exercise becomes more complex. In what follows we're going to introduce new tools to analyze multivariate time series.

2. Stocks

Let's assume that you have a portfolio of 5 stocks, A to E. The returns of the first six months are shown in the slide. The weights of each stock in the portfolio changes over time, as you can see here. Imagine that you want to visualize the monthly weights of each stock in a portfolio.

3. Stacked chart

You can use a stacked chart which is a graph that is used to break down and compare parts of a whole. Your initial dataset looks like this. To create a stacked chart you use the barchart function. The parameters col defines the colors you want to attribute to each stock and the parameter main sets the title of your chart. Using the code shown on the slide, you will end up with the chart presented here. Each month the sum of weights adds up to 100% and the colors clearly identify each stock in the portfolio. This is all clean and very readable. As explained above,

4. Correlation matrix with numbers

in a multivariate analysis, the goal is to evaluate the relationship between variables. So what about the relationships between those stocks? It can be estimated using the correlation matrix which represents the correlation coefficients between pairs of stocks. Using the dataset of the 5 stocks return and the code on the slide you can produces the correlation matrix presented. Here the cor function creates the correlation matrix. The round argument simply sets the number of decimals. It's used to make the matrix more readable. This is not a chart properly speaking but a quick and easy way to visualize relationships between pairs of variables, variables being stocks return here. It is also possible to represents the relationships with a series of scatterplots.

5. Correlation matrix with scatterplots

Each scatterplot representing the relationship between a pair of stocks. Having 5 stocks means that you will have 10 different scatterplots. Using the same dataset as in the previous example and the code shown here, you can produce the matrix of scatterplots presented. In this piece of code the function pairs generates the matrix of scatterplots and the argument lower-dot-panel equals NULL erases the lower portion of the matrix to make it more readable. This chart is still perfectly readable but the representation of relationships between pairs of stocks can be made even more meaningful by using color, font size or shape to represent correlations. R offers many ways to achieve this.

6. corrplot()

In particular the 'corrplot' package can be used to create beautiful and meaningful charts. It also has a lot of options to customize the output. Let's look at an example. This is the chart version of the correlation matrix presented earlier using the 'corrplot' package. Here, we used numbers to represent the correlation matrix via the argument method equals "number" and we got rid of the lower part of the matrix using the argument type equals "upper". The size and color of those numbers are proportional to the correlation level: the darker the color the higher the correlation. This helps to instantly spot strong relationships.

7. Let's practice!

Now try it for yourself!