riskiem
riskiem

Reputation: 307

How to compute the mean of a list of matrices in R

I have a data.table which has following columns: Months, Return, ExcessReturn, Beta, Momentum, MktCap at a monthly frequency across a bunch of stocks. I want to calculate monthly correlation for all these variables grouped by month and then get a mean of the monthly correlation. So far I have this:

all_spearman_corr <- Sub_Sample_Spearman %>%
split(.$Month) %>% map(select, -c(Month)) %>% map(cor,method = "spearman", use = "complete.obs")

It gives me monthly correlation.

all_spearman_corr[["196308"]] <- structure(c(1, 1, 0.08264061, 0.08264061, 0.08043907, 0.05978233, 
                       0.078441391, 0.078441391, 0.201316452, 1, 1, 0.08264061, 0.08264061, 
                       0.08043907, 0.05978233, 0.078441391, 0.078441391, 0.201316452, 
                       0.08264061, 0.08264061, 1, 1, 0.99924668, 0.11975039, -0.387153796, 
                       -0.387153796, 0.229128338, 0.08264061, 0.08264061, 1, 1, 0.99924668, 
                       0.11975039, -0.387153796, -0.387153796, 0.229128338, 0.08043907, 
                       0.08043907, 0.99924668, 0.99924668, 1, 0.1174954, -0.388342201, 
                       -0.388342201, 0.229212869, 0.05978233, 0.05978233, 0.11975039, 
                       0.11975039, 0.1174954, 1, 0.016575506, 0.016575506, -0.032427527, 
                       0.07844139, 0.07844139, -0.3871538, -0.3871538, -0.3883422, 0.01657551, 
                       1, 1, 0.003028194, 0.07844139, 0.07844139, -0.3871538, -0.3871538, 
                       -0.3883422, 0.01657551, 1, 1, 0.003028194, 0.20131645, 0.20131645, 
                       0.22912834, 0.22912834, 0.22921287, -0.03242753, 0.003028194, 
                       0.003028194, 1), .Dim = c(9L, 9L), .Dimnames = list(c("Return", 
                                                                             "ExcessReturn", "MktCapFirm", "Size", "MktCapStock", "Momentum", 
                                                                             "BM", "lnBM", "Beta"), c("Return", "ExcessReturn", "MktCapFirm", 
                                                                                                      "Size", "MktCapStock", "Momentum", "BM", "lnBM", "Beta")))
all_spearman_corr[["196307"]] <- structure(c(1, 1, 0.1033246, 0.1033246, 0.1034092, 0.04182368, 
            -0.10575399, -0.10575399, -0.08070454, 1, 1, 0.1033246, 0.1033246, 
            0.1034092, 0.04182368, -0.10575399, -0.10575399, -0.08070454, 
            0.10332464, 0.10332464, 1, 1, 0.9999975, 0.21854458, -0.3871538, 
            -0.3871538, 0.2459102, 0.10332464, 0.10332464, 1, 1, 0.9999975, 
            0.21854458, -0.3871538, -0.3871538, 0.2459102, 0.10340918, 0.10340918, 
            0.9999975, 0.9999975, 1, 0.21895231, -0.38706181, -0.38706181, 
            0.24593257, 0.04182368, 0.04182368, 0.2185446, 0.2185446, 0.2189523, 
            1, -0.09428919, -0.09428919, 0.04835413, -0.10575399, -0.10575399, 
            -0.3871538, -0.3871538, -0.3870618, -0.09428919, 1, 1, -0.06238128, 
            -0.10575399, -0.10575399, -0.3871538, -0.3871538, -0.3870618, 
            -0.09428919, 1, 1, -0.06238128, -0.08070454, -0.08070454, 0.2459102, 
            0.2459102, 0.2459326, 0.04835413, -0.06238128, -0.06238128, 1
), .Dim = c(9L, 9L), .Dimnames = list(c("Return", "ExcessReturn", 
                                        "MktCapFirm", "Size", "MktCapStock", "Momentum", "BM", "nBM", 
                                        "Beta"), c("Return", "ExcessReturn", "MktCapFirm", "Size", "MktCapStock", 
                                                   "Momentum", "BM", "nBM", "Beta")))

What I want is to be able to mean these lists and get a correlation matrix that has the mean values.

Can anyone please help?

Thanks!

Upvotes: 1

Views: 41

Answers (2)

ThomasIsCoding
ThomasIsCoding

Reputation: 101317

Besides the Reduce approach by @stefan, you can use apply + simplify2array as well to calculate the mean, e.g.,

apply(simplify2array(all_spearman_corr),1:2,mean)

which gives

> apply(simplify2array(all_spearman_corr),1:2,mean)
                  Return ExcessReturn  MktCapFirm        Size MktCapStock
Return        1.00000000   1.00000000  0.09298262  0.09298262  0.09192412
ExcessReturn  1.00000000   1.00000000  0.09298262  0.09298262  0.09192412
MktCapFirm    0.09298260   0.09298260  1.00000000  1.00000000  0.99962209
Size          0.09298260   0.09298260  1.00000000  1.00000000  0.99962209
MktCapStock   0.09192414   0.09192414  0.99962209  0.99962209  1.00000000
Momentum      0.05080300   0.05080300  0.16914748  0.16914748  0.16822386
BM           -0.01365630  -0.01365630 -0.38715380 -0.38715380 -0.38770201
lnBM         -0.01365630  -0.01365630 -0.38715380 -0.38715380 -0.38770201
Beta          0.06030596   0.06030596  0.23751927  0.23751927  0.23757272
                 Momentum          BM        lnBM        Beta
Return        0.050803005 -0.01365630 -0.01365630  0.06030596
ExcessReturn  0.050803005 -0.01365630 -0.01365630  0.06030596
MktCapFirm    0.169147495 -0.38715380 -0.38715380  0.23751927
Size          0.169147495 -0.38715380 -0.38715380  0.23751927
MktCapStock   0.168223850 -0.38770200 -0.38770200  0.23757274
Momentum      1.000000000 -0.03885684 -0.03885684  0.00796330
BM           -0.038856842  1.00000000  1.00000000 -0.02967654
lnBM         -0.038856842  1.00000000  1.00000000 -0.02967654
Beta          0.007963302 -0.02967654 -0.02967654  1.00000000

Upvotes: 1

stefan
stefan

Reputation: 124038

This could be achieved via Reduce like so:

Reduce(`+`, all_spearman_corr) / length(all_spearman_corr)

Output:

#>                   Return ExcessReturn  MktCapFirm        Size MktCapStock
#> Return        1.00000000   1.00000000  0.09298262  0.09298262  0.09192412
#> ExcessReturn  1.00000000   1.00000000  0.09298262  0.09298262  0.09192412
#> MktCapFirm    0.09298260   0.09298260  1.00000000  1.00000000  0.99962209
#> Size          0.09298260   0.09298260  1.00000000  1.00000000  0.99962209
#> MktCapStock   0.09192414   0.09192414  0.99962209  0.99962209  1.00000000
#> Momentum      0.05080300   0.05080300  0.16914748  0.16914748  0.16822386
#> BM           -0.01365630  -0.01365630 -0.38715380 -0.38715380 -0.38770201
#> lnBM         -0.01365630  -0.01365630 -0.38715380 -0.38715380 -0.38770201
#> Beta          0.06030596   0.06030596  0.23751927  0.23751927  0.23757272
#>                  Momentum          BM        lnBM        Beta
#> Return        0.050803005 -0.01365630 -0.01365630  0.06030596
#> ExcessReturn  0.050803005 -0.01365630 -0.01365630  0.06030596
#> MktCapFirm    0.169147495 -0.38715380 -0.38715380  0.23751927
#> Size          0.169147495 -0.38715380 -0.38715380  0.23751927
#> MktCapStock   0.168223850 -0.38770200 -0.38770200  0.23757274
#> Momentum      1.000000000 -0.03885684 -0.03885684  0.00796330
#> BM           -0.038856842  1.00000000  1.00000000 -0.02967654
#> lnBM         -0.038856842  1.00000000  1.00000000 -0.02967654
#> Beta          0.007963302 -0.02967654 -0.02967654  1.00000000

Data:

all_spearman_corr <- list(`196308` = structure(c(1, 1, 0.08264061, 0.08264061, 0.08043907, 
0.05978233, 0.078441391, 0.078441391, 0.201316452, 1, 1, 0.08264061, 
0.08264061, 0.08043907, 0.05978233, 0.078441391, 0.078441391, 
0.201316452, 0.08264061, 0.08264061, 1, 1, 0.99924668, 0.11975039, 
-0.387153796, -0.387153796, 0.229128338, 0.08264061, 0.08264061, 
1, 1, 0.99924668, 0.11975039, -0.387153796, -0.387153796, 0.229128338, 
0.08043907, 0.08043907, 0.99924668, 0.99924668, 1, 0.1174954, 
-0.388342201, -0.388342201, 0.229212869, 0.05978233, 0.05978233, 
0.11975039, 0.11975039, 0.1174954, 1, 0.016575506, 0.016575506, 
-0.032427527, 0.07844139, 0.07844139, -0.3871538, -0.3871538, 
-0.3883422, 0.01657551, 1, 1, 0.003028194, 0.07844139, 0.07844139, 
-0.3871538, -0.3871538, -0.3883422, 0.01657551, 1, 1, 0.003028194, 
0.20131645, 0.20131645, 0.22912834, 0.22912834, 0.22921287, -0.03242753, 
0.003028194, 0.003028194, 1), .Dim = c(9L, 9L), .Dimnames = list(
    c("Return", "ExcessReturn", "MktCapFirm", "Size", "MktCapStock", 
    "Momentum", "BM", "lnBM", "Beta"), c("Return", "ExcessReturn", 
    "MktCapFirm", "Size", "MktCapStock", "Momentum", "BM", "lnBM", 
    "Beta"))), `196307` = structure(c(1, 1, 0.1033246, 0.1033246, 
0.1034092, 0.04182368, -0.10575399, -0.10575399, -0.08070454, 
1, 1, 0.1033246, 0.1033246, 0.1034092, 0.04182368, -0.10575399, 
-0.10575399, -0.08070454, 0.10332464, 0.10332464, 1, 1, 0.9999975, 
0.21854458, -0.3871538, -0.3871538, 0.2459102, 0.10332464, 0.10332464, 
1, 1, 0.9999975, 0.21854458, -0.3871538, -0.3871538, 0.2459102, 
0.10340918, 0.10340918, 0.9999975, 0.9999975, 1, 0.21895231, 
-0.38706181, -0.38706181, 0.24593257, 0.04182368, 0.04182368, 
0.2185446, 0.2185446, 0.2189523, 1, -0.09428919, -0.09428919, 
0.04835413, -0.10575399, -0.10575399, -0.3871538, -0.3871538, 
-0.3870618, -0.09428919, 1, 1, -0.06238128, -0.10575399, -0.10575399, 
-0.3871538, -0.3871538, -0.3870618, -0.09428919, 1, 1, -0.06238128, 
-0.08070454, -0.08070454, 0.2459102, 0.2459102, 0.2459326, 0.04835413, 
-0.06238128, -0.06238128, 1), .Dim = c(9L, 9L), .Dimnames = list(
    c("Return", "ExcessReturn", "MktCapFirm", "Size", "MktCapStock", 
    "Momentum", "BM", "nBM", "Beta"), c("Return", "ExcessReturn", 
    "MktCapFirm", "Size", "MktCapStock", "Momentum", "BM", "nBM", 
    "Beta"))))

Upvotes: 1

Related Questions