Variance-Covariance Matrix in R (corpcor, covmat)

The following tutorial demonstrates how to calculate a variance-covariance matrix in R, along with shrinkage estimate of covariance and the calculation of a covariance into a correlation matrix.

The purpose of a variance-covariance matrix is to illustrate the variance of a particular variable (diagonals) while covariance illustrates the covariances between the exhaustive combinations of variables.
 

Why do we use variance-covariance matrices?

A variance-covariance matrix is particularly useful when it comes to analysing the volatility between elements of a group of data. For instance, a variance-covariance matrix has particular applications when it comes to analysing portfolio returns. If several assets with a high covariance are included in a portfolio, then this represents high risk. This indicates that several assets with high volatility move together, which is what investors would typically want to avoid.

On the other hand, selecting assets that show negative covariance allows for greater diversification of the portfolio, since the volatility of the assets do not tend to move together.

You might find the following Investopedia article useful in gaining more information on the financial aspect.
 

1. First, we install our libraries.

#Install Packages
install.packages("corpcor")
install.packages("tseries")

 

2. We then load the corpcor and tseries libraries into the environment.

#Load libraries
library(corpcor)
library(tseries)

 

3. For the purpose of this variance-covariance matrix, we define five different arrays with ten variables each.

array1 = c(13.5202517215525, 11.7561504771971, 3.44172913523973, 39.4287548747563, 8.76795440915058, 2.63512586865416, 6.74913659657108, 16.7466592662137, 12.2776432279652, 37.9759102485588)
 
array2 = c(12.1707972432546, 38.7142125699233, -2.43887610598041, 35.704154205164, 14.1500846763855, 14.6774960953104, 33.4000743445071, 20.9143399677937, 29.0236990846909, 39.6723338442633)
 
array3 = c(1.98160082285607, 25.9602198351201, 21.3342004335339, 40.5670804718192, 40.1849122722379, -8.37226091164161, 23.8065847362045, 28.7914304797224, 7.84284443458559, 28.3099441098979)
 
array4 = c(22.7959726528399, 29.0441051618296, 32.5325969208927, 15.6328044689712, 40.3577825526274, 47.4132732611622, 33.6186807664897, 4.59958158396621, -12.4250046633045, 9.52981108986157)
 
array5 = c(34.0114097491181, 25.0431546674253, 32.8528325950202, 41.3511620109053, 7.28977614745891, 52.644236354013, 27.1683038355368, 19.157439667575, 25.5653815077762, 22.2443948949601)

 

4. We then combine our five arrays into a data frame entitled combinedarrays.

combinedarrays  <- data.frame(array1, array2, array3, array4, array5)
 
range.names = c("Range1", "Range2", "Range3", "Range4", "Range5")
 
names(er) = range.names

 

5. We then calculate a covariance matrix for our new data frame.

covmat = matrix(c(cov(combinedarrays)),
nrow=5, ncol=5)
 
dimnames(covmat) = list(range.names, range.names)
 
#Covariance matrix
> #Covariance matrix
> covmat
            Range1    Range2     Range3     Range4      Range5
Range1  171.126789 111.41344  102.65568 -116.66931   -4.612287
Range2  111.413439 193.72248   72.66239  -97.85822  -21.381876
Range3  102.655679  72.66239  255.02100  -26.31452 -118.764281
Range4 -116.669310 -97.85822  -26.31452  329.10739   54.852427
Range5   -4.612287 -21.38188 -118.76428   54.85243  155.055790

 

6. Additionally, we can calculate our shrinkage estimate of covariance as follows:

#Shrinkage estimate of covariance
> #Shrinkage estimate of covariance
> cov.shrink(covmat)
Estimating optimal shrinkage intensity lambda.var (variance vector): 1 
 
Estimating optimal shrinkage intensity lambda (correlation matrix): 0.2212 
 
          Range1    Range2    Range3    Range4    Range5
Range1 12984.592  8956.804  6363.595 -9776.477 -5694.602
Range2  8956.804 12984.592  5422.063 -9153.088 -5570.285
Range3  6363.595  5422.063 12984.592 -4871.270 -9933.736
Range4 -9776.477 -9153.088 -4871.270 12984.592  4210.960
Range5 -5694.602 -5570.285 -9933.736  4210.960 12984.592
attr(,"lambda")
[1] 0.2211572
attr(,"lambda.estimated")
[1] TRUE
attr(,"class")
[1] "shrinkage"
attr(,"lambda.var")
[1] 1
attr(,"lambda.var.estimated")
[1] TRUE

 

7. In order to transform our covariance matrix into a correlation matrix, we do so using the cov2cor command:

#Transform covariance to correlation matrix
cov2cor(covmat)
            Range1     Range2      Range3      Range4      Range5
Range1  1.00000000  0.6119114  0.49140097 -0.49161917 -0.02831479
Range2  0.61191143  1.0000000  0.32691263 -0.38755959 -0.12337063
Range3  0.49140097  0.3269126  1.00000000 -0.09083196 -0.59724686
Range4 -0.49161917 -0.3875596 -0.09083196  1.00000000  0.24281921
Range5 -0.02831479 -0.1233706 -0.59724686  0.24281921  1.00000000