Create a Covariance Matrix in R

Your goal

You need to create a covariance matrix in R.

Step-by-step tutorial

Use the cov function from the stats package to calculate the covariance matrix.

> library(stats)
> precip.df <- read.csv("precip-central-park.csv")
> head(precip.df)
  YEAR  JAN  FEB  MAR  APR  MAY  JUN  JUL  AUG  SEP  OCT  NOV  DEC ANNUAL
1 1869 2.53 6.87 4.61 1.39 4.15 4.40 3.20 1.76 2.81 6.48 2.03 5.02  45.25
2 1870 4.41 2.83 3.33 5.11 1.83 2.82 3.76 3.07 2.52 4.97 2.42 2.18  39.25
3 1871 2.07 2.72 5.54 3.03 4.04 7.05 5.57 5.60 2.34 7.50 3.56 2.24  51.26
4 1872 1.88 1.29 3.74 2.29 2.68 2.93 7.83 6.29 2.95 3.35 4.08 3.18  42.49
5 1873 5.34 3.80 2.09 4.16 3.69 1.28 4.61 9.56 3.14 2.73 4.63 2.96  47.99
6 1874 5.33 2.04 2.12 8.77 2.24 2.78 5.06 2.43 8.24 1.70 2.30 2.82  45.83
> cov(precip.df)
               YEAR         JAN         FEB         MAR         APR         MAY
YEAR   1912.6666667  2.17800000 -6.41893333  9.02066667 16.59746667 20.13446667
JAN       2.1780000  2.66414659 -0.16160554 -0.31084844  0.35374316  0.09757978
FEB      -6.4189333 -0.16160554  2.10653972 -0.06749681 -0.23404794  0.06286891
MAR       9.0206667 -0.31084844 -0.06749681  3.44369993  0.78367503 -0.08013588
APR      16.5974667  0.35374316 -0.23404794  0.78367503  3.79338331  0.33102181
MAY      20.1344667  0.09757978  0.06286891 -0.08013588  0.33102181  3.93775242
JUN      17.3048000 -0.28019683  0.16910415  0.10035574  0.21781948  0.41292702
JUL       0.7145333 -0.53609712 -0.22040562 -0.23378648  0.22284411 -0.01788170
AUG       0.9732667  0.25413714 -0.04997809  0.20905129  0.42033306  0.31223132
SEP       9.2222000 -0.05035932 -0.14966524  0.08835672 -0.01120717  0.26686788
OCT       7.6376667  0.14560589  0.09988283  0.69210942  0.86275199 -0.40277118
NOV      13.1382000 -0.40639289 -0.16808381 -0.10377691 -0.05045828  0.03787582
DEC      16.5262000  0.10003468  0.11988281  0.43387841  0.88485425  0.50311515
ANNUAL  106.9951333  1.86948889  1.50602248  4.95675491  7.57376428  5.46620218
              JUN        JUL         AUG         SEP         OCT         NOV
YEAR   17.3048000  0.7145333  0.97326667  9.22220000  7.63766667 13.13820000
JAN    -0.2801968 -0.5360971  0.25413714 -0.05035932  0.14560589 -0.40639289
FEB     0.1691042 -0.2204056 -0.04997809 -0.14966524  0.09988283 -0.16808381
MAR     0.1003557 -0.2337865  0.20905129  0.08835672  0.69210942 -0.10377691
APR     0.2178195  0.2228441  0.42033306 -0.01120717  0.86275199 -0.05045828
MAY     0.4129270 -0.0178817  0.31223132  0.26686788 -0.40277118  0.03787582
JUN     4.0150082  0.6837666 -0.12882299 -0.16927791  0.68359355  0.07552130
JUL     0.6837666  5.0446241 -0.10460463  0.17879716 -0.24325793  0.57165806
AUG    -0.1288230 -0.1046046  7.28032114  0.48909595  0.82086730 -0.13107065
SEP    -0.1692779  0.1787972  0.48909595  6.99591392 -0.47782243 -0.30771509
OCT     0.6835936 -0.2432579  0.82086730 -0.47782243  6.38665319  0.25830370
NOV     0.0755213  0.5716581 -0.13107065 -0.30771509  0.25830370  4.38448170
DEC     0.5014491  0.3385167  0.21489125 -0.15720978  0.36111802 -0.11056563
ANNUAL  6.2817167  5.6836206  9.58122398  6.72085042  9.18428963  4.04516764
              DEC     ANNUAL
YEAR   16.5262000 106.995133
JAN     0.1000347   1.869489
FEB     0.1198828   1.506022
MAR     0.4338784   4.956755
APR     0.8848543   7.573764
MAY     0.5031151   5.466202
JUN     0.5014491   6.281717
JUL     0.3385167   5.683621
AUG     0.2148913   9.581224
SEP    -0.1572098   6.720850
OCT     0.3611180   9.184290
NOV    -0.1105656   4.045168
DEC     3.1070517   6.293792
ANNUAL  6.2937923  69.177980

For a smaller matrix, we can subset the data matrix to the first six months:

> cov(precip.df[2:7])
            JAN         FEB         MAR        APR         MAY        JUN
JAN  2.66414659 -0.16160554 -0.31084844  0.3537432  0.09757978 -0.2801968
FEB -0.16160554  2.10653972 -0.06749681 -0.2340479  0.06286891  0.1691042
MAR -0.31084844 -0.06749681  3.44369993  0.7836750 -0.08013588  0.1003557
APR  0.35374316 -0.23404794  0.78367503  3.7933833  0.33102181  0.2178195
MAY  0.09757978  0.06286891 -0.08013588  0.3310218  3.93775242  0.4129270
JUN -0.28019683  0.16910415  0.10035574  0.2178195  0.41292702  4.0150082