Calculate covariance matrix in R

3

Given an array whose elements are values of a given attribute in n positions in space. How to calculate the covariance matrix (nxn) of the values of this attribute in given positions?

    
asked by anonymous 28.06.2017 / 20:47

1 answer

1

The cov computes the covariance matrix between vectors:

m <- structure(c(0.768452329393413, 0.520393273867425, -2.09890502749191, 
-0.654528466570541, 0.919830179164542, 1.27460444798111, 1.07961551777992, 
-0.289916033636275, 1.15855442744553, 0.508462489658259, -0.0792310427562403, 
0.521811455261112, -0.0959720400431859, -0.349637076748802, -0.623314127996813, 
-1.19614534147188, 0.847785958379337, 0.0366301414309972, -0.235336873166317, 
0.904062012003621, 0.0018213668519001, -0.523018014086154, 1.14818660098542, 
-0.0690739661280247, 0.607636185846767, 0.187434031463705, -1.46043293060116, 
1.06255833869145, -2.36387220876329, 0.310577658076884, 1.39471110313637, 
1.22342525393594, -1.14380083312874, 0.440257827354724, -1.63456585330527, 
-0.214500835853314, -0.411697427268116, 0.436671960637827, -0.5254714253143, 
0.522741411533719, 0.0656469891893928, -0.344497090663013, 1.29287924459695, 
-0.183348135008822, -1.8475095235835, -0.36208432586153, 1.88503500093279, 
0.539769443231984, -0.291513706458498, -1.46009196414015, 1.09133008288539, 
-0.0874258541488817, -2.45182826273455, -2.08616997315259, -0.728580575694078, 
-0.375605093992269, 0.327776658112704, -0.837930695292596, -0.405610496341648, 
0.512589750376218, -0.827557418583916, 1.32415306656766, 1.23817207202085, 
0.313640070040018, 0.226985682916868, -0.578426494891533, -1.07656369792575, 
2.19587300679404, 0.797528302882954, 0.820203882841757, 1.54349236714106, 
-1.25021444211339, -0.179239252318575, 0.330079846792988, 0.397703631069944, 
-0.973869052681041, -0.430052555124803, 0.404691334801542, 0.214385731192446, 
0.286513167080837, 0.320391102476092, -0.556257733293155, 0.0934325159775297, 
-1.38254135440984, -0.590534059589543, 0.713923335021795, -0.144268684428018, 
-0.852335048928232, -0.199408549237512, 0.359851544015093, -1.21739555956348, 
-0.195512250189903, -0.112744559736738, 0.625804498175987, -1.57775986198347, 
1.20054913070658, -1.73886091923092, -0.369892489013922, 0.661666078266635, 
-1.79441936980968, -1.0116282744842, -0.585882545581284, 2.15323933805744, 
-1.5076232213321, 1.24363310435572, -0.248846191662788, 1.4015646439713, 
-1.42085074256961, -2.23819649929885, 0.135378170378313, 0.0538991487109531, 
-0.822828682914321, -1.36885371767029, 0.06777560839154, 0.985130708841452, 
-0.502323948730095, -0.115917838687705, 0.226246852077969, 0.237178313260646, 
-0.280566120702846, -0.950878757064271, -0.262738983028041, 0.758745108507647, 
1.23846255554868, 2.0814272665118, 1.80610909459879, -2.03882073180482, 
0.822316149311104, -0.70407229002402, 0.385178009929021, 0.00642742239202372, 
1.7899070169825, -0.594813416273945, 0.836592128731576, -0.152105102543435, 
-0.276066182919236, 1.02558836401032, 1.98810727160017, -2.52841323508544, 
0.642710557325235, 1.70460856449495, 0.403639756604342, -1.58395780865054, 
0.76233906676309, 0.213916020487889, -0.676869926369806, 1.0137575755379, 
-0.814741083224426, -0.376563071825111, -0.045196529340526), .Dim = c(50L, 
3L))

cov(m)
          [,1]       [,2]       [,3]
[1,] 0.9339247  0.1967204  0.1614836
[2,] 0.1967204  0.9526770 -0.2246604
[3,] 0.1614836 -0.2246604  1.2965366

Notice how the resulting matrix is symmetric, as expected. If I wanted the correlation matrix, it would be enough to run

cor(m)
          [,1]       [,2]       [,3]
[1,] 1.0000000  0.2085550  0.1467507
[2,] 0.2085550  1.0000000 -0.2021442
[3,] 0.1467507 -0.2021442  1.0000000
    
28.06.2017 / 22:40