简体   繁体   English

如何计算 R 中矩阵列表的平均值

[英]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.我有一个 data.table,其中包含以下列:月份、回报、ExcessReturn、Beta、Momentum、MktCap,每月频率跨越一堆股票。 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!谢谢!

This could be achieved via Reduce like so:这可以像这样通过Reduce来实现:

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

Output: 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"))))

Besides the Reduce approach by @stefan , you can use apply + simplify2array as well to calculate the mean, eg,除了@stefanReduce方法,您还可以使用apply + simplify2array来计算平均值,例如,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM