How to create a covariance matrix in R?

R ProgrammingServer Side ProgrammingProgramming

To create a covariance matrix, we first need to find the correlation matrix and a vector of standard deviations is also required. The correlation matrix can be found by using cor function with matrix object. For example, if we have matrix M then the correlation matrix can be found as cor(M). Now we can use this matrix to find the covariance matrix but we should make sure that we have the vector of standard deviations.

Example1

Live Demo

> M1<-matrix(rnorm(25,5,1),ncol=5)
> M1

Output

       [,1]       [,2]    [,3]     [,4]    [,5]
[1,] 5.446881 3.918951 4.306415 4.446757 5.438095
[2,] 5.174487 4.401736 4.755313 5.341615 5.470727
[3,] 4.042656 6.303782 4.939945 4.847339 4.698141
[4,] 3.931823 6.870019 5.196559 6.692013 3.689279
[5,] 4.800770 4.841470 4.356387 3.508081 3.913045

Example

> cor(M1)

Output

      [,1]         [,2]      [,3]       [,4]     [,5]
[1,] 1.0000000 -0.9908635 -0.8197813 -0.5066590 0.7190219
[2,] -0.9908635 1.0000000 0.8695808 0.6121477  -0.7017947
[3,] -0.8197813 0.8695808 1.0000000 0.8684189  -0.3766508
[4,] -0.5066590 0.6121477 0.8684189 1.0000000  -0.1766397
[5,] 0.7190219 -0.7017947 -0.3766508 -0.1766397 1.0000000

Example

> SD_M1<-rnorm(5)
> M1_Covariance<-(SD_M1%*%t(SD_M1))*cor(M1)
> M1_Covariance

Output

       [,1]        [,2]     [,3]      [,4]     [,5]
[1,] 0.4916487 0.5972772 0.3299963 0.4854532 0.4018740
[2,] 0.5972772 0.7390424 0.4291690 0.7191096 0.4809114
[3,] 0.3299963 0.4291690 0.3295850 0.6812666 0.1723625
[4,] 0.4854532 0.7191096 0.6812666 1.8672751 0.1924034
[5,] 0.4018740 0.4809114 0.1723625 0.1924034 0.6353903

Example2

Live Demo

> M2<-matrix(rnorm(36,5,3),ncol=6)
> M2

Output

    [,1]            [,2]    [,3]     [,4]     [,5]      [,6]
[1,] 3.97345156 2.215491 3.112134 4.6181951 3.6277630 12.322117
[2,] 8.87011312 4.857979 9.067974 6.7924669 5.4123406 3.062662
[3,] 8.70716339 4.905574 5.092897 7.1700455 8.1998048 2.790839
[4,] 2.79704783 8.857838 3.804275 8.0486804 1.8110960 3.789724
[5,] 4.37050111 4.220881 7.835320 -0.9182616 0.5487265 5.676240
[6,] 0.01411185 4.138786 1.736414 6.8982956 4.4729650 6.719265

Example

> cor(M2)

Output

        [,1]        [,2]       [,3]       [,4]      [,5]       [,6]
[1,] 1.00000000 -0.05099798 0.72101949 0.0606742 0.5573581 -0.4634089
[2,] -0.05099798 1.00000000 0.02406821 0.4332765 -0.1929059 -0.6648619
[3,] 0.72101949 0.02406821 1.00000000 -0.3981214 -0.0489995 -0.4858796
[4,] 0.06067420 0.43327648 -0.39812145 1.0000000 0.5885335 -0.2935423
[5,] 0.55735812 -0.19290585 -0.04899950 0.5885335 1.0000000 -0.2820521
[6,] -0.46340892 -0.66486188 -0.48587961 -0.2935423 -0.2820521 1.0000000

Example

> SD_M2<-rnorm(6)
> M2_Covariance<-round((SD_M2%*%t(SD_M2))*cor(M2),2)
> M2_Covariance

Output

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 0.02 0.00 0.01 0.00 -0.17 -0.10
[2,] 0.00 0.00 0.00 0.00 0.00 -0.01
[3,] 0.01 0.00 0.00 0.00 0.01 -0.04
[4,] 0.00 0.00 0.00 0.01 -0.09 -0.03
[5,] -0.17 0.00 0.01 -0.09 4.10 0.80
[6,] -0.10 -0.01 -0.04 -0.03 0.80 1.95

Example3

Live Demo

> M3<-matrix(runif(36,2,5),ncol=6)
> M3

Output

      [,1]       [,2]     [,3]     [,4]    [,5]     [,6]
[1,] 3.532356 3.325595 2.533484 3.858067 4.209765 4.520970
[2,] 2.836041 4.624075 2.739955 4.294261 2.815741 3.037129
[3,] 2.171741 2.277840 3.939984 3.464941 4.209270 3.565684
[4,] 3.363305 2.623304 2.398647 2.180268 3.740313 3.967784
[5,] 4.637331 2.672298 4.518063 4.851971 2.905769 2.946685
[6,] 3.855836 4.070287 3.698472 4.741746 3.726380 2.603361

Example

> cor(M3)

Output

        [,1]      [,2]       [,3]       [,4]      [,5]     [,6]
[1,] 1.00000000 0.02856209 0.3130817 0.4599297 -0.3979636 -0.2620906
[2,] 0.02856209 1.00000000 -0.3159665 0.4775255 -0.4282192 -0.4020013
[3,] 0.31308166 -0.31596655 1.0000000 0.5990245 -0.2107199 -0.6214000
[4,] 0.45992975 0.47752549 0.5990245 1.0000000 -0.4594787 -0.6584400
[5,] -0.39796357 -0.42821922 -0.2107199 -0.4594787 1.0000000 0.6207340
[6,] -0.26209064 -0.40200135 -0.6214000 -0.6584400 0.6207340 1.0000000

Example

> SD_M3<-runif(6,2,3)
> M3_Covariance<-round((SD_M3%*%t(SD_M3))*cor(M3),4)
> M3_Covariance

Output

[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 8.2937 0.1818 2.7005 3.6199 -3.2048 -1.9410
[2,] 0.1818 4.8857 -2.0918 2.8846 -2.6467 -2.2850
[3,] 2.7005 -2.0918 8.9705 4.9032 -1.7648 -4.7860
[4,] 3.6199 2.8846 4.9032 7.4690 -3.5114 -4.6274
[5,] -3.2048 -2.6467 -1.7648 -3.5114 7.8193 4.4635
[6,] -1.9410 -2.2850 -4.7860 -4.6274 4.4635 6.6127

Example4

Live Demo

> M4<-matrix(rpois(64,10),ncol=8)
> M4

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 2 12 6 6 20 6 5 13
[2,] 11 14 11 10 13 8 14 9
[3,] 7 10 10 10 13 6 9 13
[4,] 6 15 12 11 9 7 14 11
[5,] 9 14 18 14 13 7 15 15
[6,] 12 12 6 14 9 11 6 5
[7,] 11 9 12 9 10 8 12 3
[8,] 12 15 9 15 8 10 4 9

Example

> round(cor(M4),2)

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 1.00 0.06 0.15 0.69 -0.75 0.80 0.06 -0.63
[2,] 0.06 1.00 0.20 0.48 -0.20 0.20 0.12 0.37
[3,] 0.15 0.20 1.00 0.27 -0.16 -0.32 0.82 0.33
[4,] 0.69 0.48 0.27 1.00 -0.73 0.68 -0.03 -0.08
[5,] -0.75 -0.20 -0.16 -0.73 1.00 -0.68 -0.10 0.54
[6,] 0.80 0.20 -0.32 0.68 -0.68 1.00 -0.37 -0.67
[7,] 0.06 0.12 0.82 -0.03 -0.10 -0.37 1.00 0.15
[8,] -0.63 0.37 0.33 -0.08 0.54 -0.67 0.15 1.00

Example

> SD_M4<-rpois(8,2)
> M4_Covariance<-round((SD_M4%*%t(SD_M4))*cor(M4),2)
> M4_Covariance

Output

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,] 4.00 0.12 1.17 1.39 -1.51 3.20 0.24 -2.52
[2,] 0.12 1.00 0.82 0.48 -0.20 0.39 0.24 0.74
[3,] 1.17 0.82 16.00 1.10 -0.63 -2.54 6.56 2.66
[4,] 1.39 0.48 1.10 1.00 -0.73 1.36 -0.06 -0.15
[5,] -1.51 -0.20 -0.63 -0.73 1.00 -1.35 -0.20 1.09
[6,] 3.20 0.39 -2.54 1.36 -1.35 4.00 -1.50 -2.70
[7,] 0.24 0.24 6.56 -0.06 -0.20 -1.50 4.00 0.58
[8,] -2.52 0.74 2.66 -0.15 1.09 -2.70 0.58 4.00
raja
Published on 19-Nov-2020 05:12:09
Advertisements