group/category 的相关矩阵

Correlation Matrix by group/category

我正在编写一个代码来为我公司的不同客户计算投资组合的波动性,但我被困在一个将在下面解释的过程中:

样本:

dat <- structure(list(CNPJ = c(101425781, 101825381, 101824382, 101823383, 
101343863, 101275931, 101733473, 101883323, 101730476, 101618588, 
101585621, 101884322, 101617589, 101298908, 101853353, 101266940, 
101569637, 101428778, 101260946, 101261945, 101567639, 101730476, 
101618588, 101585621, 101884322, 101617589, 101298908, 101853353, 
101266940, 101267939, 101628578, 101440766, 101435771, 101434772, 
101527679, 101438768, 101266940, 101266940, 101824382, 101823383, 
101343863, 101275931, 101733473, 101475731, 101233973, 101258948, 
101311895, 101274932, 101865341, 101692514, 101284922, 101357849, 
101694512, 101649557, 101384822, 101625581, 101325881, 101404802, 
101638568, 101773433, 101301905, 101637569, 101698508, 101558648, 
101762444, 101772434, 101770436, 101761445, 101771435, 101302904, 
101755451, 101762444, 101772434, 101770436, 101585621, 101884322, 
101617589, 101762444, 101462744, 101544662, 101334872, 101744462, 
101538668, 101521685, 101465741, 101323883, 101683523, 101682524, 
101820386, 101737469, 101742464, 101728478, 101489717, 101764442, 
101606600, 101679527, 101607599, 101493713, 101504702, 101694512, 
101649557, 101384822, 101625581, 101325881, 101404802, 101619587, 
101811395, 101677529, 101664542, 101729477, 101665541, 101663543, 
101622584, 101559647, 101794412, 101780426, 101825381, 101824382, 
101823383, 101343863, 101275931, 101733473, 101883323, 101730476, 
101794412, 101780426, 101730476, 101665541, 101663543, 101730476
), NOME_CLIENTE = c("André", "André", "André", "André", "André", 
"André", "André", "Leandro", "Leandro", "Leandro", "Leandro", 
"Leandro", "Leandro", "Leandro", "Leandro", "Leandro", "Thomaz", 
"Thomaz", "Thomaz", "Thomaz", "Thomaz", "Thomaz", "Thomaz", "Thomaz", 
"Thomaz", "Thomaz", "Thomaz", "Camile", "Camile", "Camile", "Camile", 
"Camile", "Lucia", "Lucia", "Lucia", "Lucia", "Lucia", "Lucia", 
"Lucia", "Lucia", "Lucia", "Lucia", "Lucia", "Lucia", "Lucia", 
"Tadeu", "Tadeu", "Tadeu", "Tadeu", "Tadeu", "Tadeu", "Tadeu", 
"Tadeu", "Tadeu", "Tadeu", "Tadeu", "Lucas", "Lucas", "Lucas", 
"Lucas", "Lucas", "Ana", "Ana", "Ana", "Fred", "Fred", "Fred", 
"Fred", "Fred", "Vania", "Vania", "Vania", "Vania", "Vania", 
"Rogerio", "Rogerio", "Rogerio", "Murilo", "Alex", "Alex", "Alex", 
"Alex", "Alex", "Alex", "Alex", "Adriano", "Adriano", "Adriano", 
"Adriano", "Adriano", "Adriano", "Laura", "Laura", "Laura", "Laura", 
"Laura", "Laura", "Laura", "Laura", "Marina", "Marina", "Marina", 
"Marina", "Marina", "Josi", "Josi", "Josi", "Josi", "Josi", "Oscar", 
"Oscar", "Oscar", "Oscar", "Oscar", "Matheus", "Matheus", "Matheus", 
"Matheus", "Matheus", "Matheus", "Carla", "Carla", "Vicente", 
"Vicente", "Vicente", "Vicente", "Vicente", "Vicente", "Vicente", 
"Vicente"), `Retorno do fechamento em 1 dia (de 05Jul21 até 06Jul21) Em moeda orig ajust p/ prov` = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0.00778361427364871, 0.0189589731235174, 0.0167292435435229, 
0.0209282798095956, 0.0175226514329552, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -0.178722146574728, 
-0.179207335986575, 0, 0, 0, 0, 0, 0, 0, 0, -0.178722146574728, 
-0.179207335986575, 0, 0, 0, 0), `Retorno do fechamento em 1 dia (de 02Jul21 até 05Jul21) Em moeda orig ajust p/ prov` = c(0.942365785340371, 
0.805477017638623, 0.802614427993831, 0.79957278976508, 0.343775841429306, 
0.770676273714344, 0.681504154090362, 0.36820554996666, 0.675119873812946, 
0.886332713707816, -0.196678715019516, 0.368162580161879, 0.0573179420825909, 
0.630863469814358, 0.0537324149263441, 0.053517096239375, 0.053270060561772, 
0.123570029245457, 0.00240310728258919, 0.000954475581238512, 
0.012390517804306, 0.675119873812946, 0.886332713707816, -0.196678715019516, 
0.368162580161879, 0.0573179420825909, 0.630863469814358, 0.0537324149263441, 
0.053517096239375, -0.175190593472507, 0.0513788099851809, 0.0571791188122006, 
-0.000460241699329345, -0.0055272630561376, -0.180391395588231, 
-0.000360705416824203, 0.053517096239375, 0.053517096239375, 
0.802614427993831, 0.79957278976508, 0.343775841429306, 0.770676273714344, 
0.681504154090362, -0.00212285012821667, 0.0140633886985597, 
0.0159728740982246, 0.0153047161802533, 0.0171541991221602, 0.0161614329044824, 
0.0239725273786462, 0.0170795990925399, 0.0146915856021224, 0.0208990009923582, 
0.0251777479206794, 0.0101244871984818, 0.083773042933899, 0.00613068441452924, 
0.0596999600020354, -0.000258351337834029, -0.0318828113449854, 
0.508815288958431, -0.0037543713915511, -0.0422787105890166, 
-0.00410938619097578, 0.0156755046191392, -0.0350934983543993, 
-0.0351224380210624, 0.0150984966239776, -0.0352719532202173, 
0.510709844456869, 0.0136689604914864, 0.0156755046191392, -0.0350934983543993, 
-0.0351224380210624, -0.196678715019516, 0.368162580161879, 0.0573179420825909, 
0.0156755046191392, -0.239597912332101, -0.152928001898545, -0.0235288461226446, 
-0.198399838154728, -0.462776459698944, -0.0380991642487061, 
-0.466886323647486, 0.00119402775453636, 0.14253430399549, 0.139307505560282, 
0.102990215236787, -0.218033586497768, -0.218882237186335, -0.0285126700873661, 
-0.0373568835129845, 0.0905571250768844, 0.0210312804483692, 
-0.0707320114997856, 0.0156884318130324, -0.351507907998894, 
-0.0933908677325235, 0.0208990009923582, 0.0251777479206794, 
0.0101244871984818, 0.083773042933899, 0.00613068441452924, 0.0596999600020354, 
-0.229178049994516, -0.0865143876580987, -0.10620687098708, -0.0552723868167959, 
-0.0814444899333466, -0.0519984678248875, -0.0520159507686913, 
-0.117729024987057, -0.0530218498170143, 0.158825809739938, 0.158491499496449, 
0.805477017638623, 0.802614427993831, 0.79957278976508, 0.343775841429306, 
0.770676273714344, 0.681504154090362, 0.36820554996666, 0.675119873812946, 
0.158825809739938, 0.158491499496449, 0.675119873812946, -0.0519984678248875, 
-0.0520159507686913, 0.675119873812946), `Retorno do fechamento em 1 dia (de 01Jul21 até 02Jul21) Em moeda orig ajust p/ prov` = c(0.979534778889501, 
1.00895916129957, 1.00597755536, 1.00293131490616, 1.19184508603212, 
0.892244829992705, 1.06031548002647, -0.242739015629923, 1.05267639410158, 
1.12581394059816, -1.26531522691948, -0.242730580339412, 0.829333663568832, 
0.709356947118067, -0.168167339415959, -0.167775623322086, -0.167328415955126, 
0.329745306589757, 0.27122778701596, 0.268143224639061, -0.124702736684412, 
1.05267639410158, 1.12581394059816, -1.26531522691948, -0.242730580339412, 
0.829333663568832, 0.709356947118067, -0.168167339415959, -0.167775623322086, 
-0.0913384802515793, -0.11147755831189, 0.0987762688964722, 0.0772892448367202, 
0.0721167476513074, 0.0718083483661758, 0.13765686780971, -0.167775623322086, 
-0.167775623322086, 1.00597755536, 1.00293131490616, 1.19184508603212, 
0.892244829992705, 1.06031548002647, -0.0107362609924166, 0.108200344766374, 
0.0167877662534011, 0.0166125437317532, 0.019362857528904, 0.0170939973031636, 
0.0149137784319464, 0.0175510795088485, 0.0219936013309052, 0.0153708668221952, 
0.0214045179745881, 0.218257983397052, 1.93124338729831, 0.0966225481533911, 
0.0660155585137545, 0.160651210353535, 0.281960610300303, 0.168294318973494, 
0.156532869732473, 0.0235352978052106, 0.15597043420712, 0.178403708923724, 
0.313215353344276, 0.313077565988351, 0.150220916111721, 0.313011823345732, 
0.167659097860451, 0.148871797682659, 0.178403708923724, 0.313215353344276, 
0.313077565988351, -1.26531522691948, -0.242730580339412, 0.829333663568832, 
0.178403708923724, 0.118057045074238, 0.057461309734208, 0.444099056039704, 
0.0440074078142061, 0.628042917742277, 1.91767980722943, 0.636043804843212, 
0.22732198212907, 0.648415319665219, 0.631001178953738, 1.45504575739324, 
0.0488470226628124, 0.0490176256789709, 0.420476676117687, 0.445707477410906, 
0.528412471248885, 0.0766849934734637, 0.135209854306595, 0.094540198915638, 
0.270248285050911, 0.480813477770425, 0.0153708668221952, 0.0214045179745881, 
0.218257983397052, 1.93124338729831, 0.0966225481533911, 0.0660155585137545, 
0.802627007942647, 0.363892160748946, 1.48838756485929, 0.340247581152653, 
0.470643488733913, 0.330754261813127, 0.331557817662542, 0.245268716025748, 
0.328566240204964, -0.0858315926052455, -0.0862059820974537, 
1.00895916129957, 1.00597755536, 1.00293131490616, 1.19184508603212, 
0.892244829992705, 1.06031548002647, -0.242739015629923, 1.05267639410158, 
-0.0858315926052455, -0.0862059820974537, 1.05267639410158, 0.330754261813127, 
0.331557817662542, 1.05267639410158), `Retorno do fechamento em 1 dia (de 30Jun21 até 01Jul21) Em moeda orig ajust p/ prov` = c(1.43870652973419, 
1.41092075518827, 1.40891626124358, 1.40683828485635, 1.29712666603155, 
1.28780756476772, 1.16027819913143, 1.15619966982194, 1.15166121959192, 
1.12679697540443, 1.11872740271792, 1.09681450958306, 0.267671114488621, 
0.222820941417012, 0.222602060603094, 0.22248505429161, 0.221691197657492, 
0.220383319538087, 0.214968588261399, 0.211758096520498, 0.194644135626731, 
1.15166121959192, 1.12679697540443, 1.11872740271792, 1.09681450958306, 
0.267671114488621, 0.222820941417012, 0.222602060603094, 0.22248505429161, 
0.1451038322557, 0.139518178730214, 0.13257357859402, 0.130725836606871, 
0.125449788902188, 0.124899058755545, 0.12112907224946, 0.22248505429161, 
0.22248505429161, 1.40891626124358, 1.40683828485635, 1.29712666603155, 
1.28780756476772, 1.16027819913143, 0.0179505830601556, 0.0179249640495982, 
0.0178968244654243, 0.0172540911080432, 0.0172192943864502, 0.0170852243172703, 
0.0170261320818099, 0.0169619641383179, 0.0169404602274881, 0.0168244730957667, 
0.016709737428755, -0.0211857909562241, -0.0216967600863427, 
-0.0229697961913189, -0.0257060649346386, -0.0259964172983018, 
-0.0275145673185762, -0.0289907728074468, -0.0292650344817957, 
-0.0294223166747543, -0.0296824166980514, -0.030167581007845, 
-0.0302402934721613, -0.0302878822367347, -0.03037016322196, 
-0.030424068972934, -0.0310724994960765, -0.0318300612889288, 
-0.030167581007845, -0.0302402934721613, -0.0302878822367347, 
1.11872740271792, 1.09681450958306, 0.267671114488621, -0.030167581007845, 
-0.0853457778248412, -0.0859374341416697, -0.0859406153722375, 
-0.0865554517076816, -0.0882744773662125, -0.0887361663444608, 
-0.0893140918378776, -0.0901054414498503, -0.0906405015484779, 
-0.0920847652196244, -0.0937861297643394, -0.0947105819250282, 
-0.0953150254645152, -0.110822802071198, -0.113156839324802, 
-0.115255077889742, -0.12083339088349, -0.122396185088292, -0.124390845849121, 
-0.130054255532741, -0.131201397198311, 0.0168244730957667, 0.016709737428755, 
-0.0211857909562241, -0.0216967600863427, -0.0229697961913189, 
-0.0257060649346386, -0.177713267203217, -0.179625764030789, 
-0.181550171964773, -0.184798425652843, -0.186204287638247, -0.186833808493247, 
-0.187116816960042, -0.18726764656094, -0.187312098569237, -0.188992797757237, 
-0.189383511951746, 1.41092075518827, 1.40891626124358, 1.40683828485635, 
1.29712666603155, 1.28780756476772, 1.16027819913143, 1.15619966982194, 
1.15166121959192, -0.188992797757237, -0.189383511951746, 1.15166121959192, 
-0.186833808493247, -0.187116816960042, 1.15166121959192), `Retorno do fechamento em 1 dia (de 29Jun21 até 30Jun21) Em moeda orig ajust p/ prov` = c(0.502124611193722, 
1.08873834833503, 1.0863304585655, 1.08408597388916, 1.19717331272113, 
0.643037106601696, 0.985535115250968, 1.25571238968405, 0.97833804320544, 
0.539720013330225, -0.0171822072843497, 1.25885415855009, -0.297552030588122, 
-0.526261176401022, 0.035477040000842, 0.0353613204424619, 0.0351307737219031, 
0.0288115119474242, 0.189729943122074, 0.186261452654435, 0.0205766682483954, 
0.97833804320544, 0.539720013330225, -0.0171822072843497, 1.25885415855009, 
-0.297552030588122, -0.526261176401022, 0.035477040000842, 0.0353613204424619, 
-0.11735292891899, 0.0925877280678833, 0.177379815249878, -0.136864399428305, 
-0.141660541066813, -0.140943691440043, -0.0966895738201856, 
0.0353613204424619, 0.0353613204424619, 1.0863304585655, 1.08408597388916, 
1.19717331272113, 0.643037106601696, 0.985535115250968, -0.0017996449059865, 
0.0312044658130617, 0.0138932151457993, 0.0152530168634257, 0.0200221114937449, 
0.0161169335115119, 0.0274898708084947, 0.0186181776371086, 0.0182820524059935, 
0.0239859622524818, 0.0205424532396137, 0.0759944194214768, -0.509912122015521, 
0.00486758381157415, 0.0288131310298922, 0.00580397354497109, 
-0.046727934932278, 0.153513796249172, 0.00224317063839408, -0.0344225619301142, 
0.00192268780665472, 0.0188015475941938, -0.0515577000442136, 
-0.0516044370669988, -0.00575247213419061, -0.051770751906588, 
0.152701738261385, -0.00723539169484866, 0.0188015475941938, 
-0.0515577000442136, -0.0516044370669988, -0.0171822072843497, 
1.25885415855009, -0.297552030588122, 0.0188015475941938, -0.000709731102688238, 
-0.125828953150631, -0.0583621415898961, -0.230176390323322, 
0.0246811907345545, -0.286447172857152, 0.0252581143286079, -0.000968543736235006, 
-0.00327709612975013, -0.000393667869502679, -0.233207584187767, 
-0.25298826967628, -0.254608426985214, -0.112315356545878, -0.0684287759213476, 
0.0235757685004501, -0.288741029726225, -0.101063827332837, -0.349614209790161, 
-0.127183163567679, -0.119651697877998, 0.0239859622524818, 0.0205424532396137, 
0.0759944194214768, -0.509912122015521, 0.00486758381157415, 
0.0288131310298922, -0.179443232809717, -0.0405482861424389, 
-0.336684563626477, 0.0314099312163307, -0.122985923007946, 0.028760595159838, 
0.0289238596451469, 0.00404918064305093, 0.0272430881523178, 
0.111774820288701, 0.11133259122289, 1.08873834833503, 1.0863304585655, 
1.08408597388916, 1.19717331272113, 0.643037106601696, 0.985535115250968, 
1.25571238968405, 0.97833804320544, 0.111774820288701, 0.11133259122289, 
0.97833804320544, 0.028760595159838, 0.0289238596451469, 0.97833804320544
), `Retorno do fechamento em 1 dia (de 28Jun21 até 29Jun21) Em moeda orig ajust p/ prov` = c(-0.198023707343964, 
0.165073225434753, 0.162118304069736, 0.159184927360911, 0.31501187786489, 
0.480946872812638, 0.100753906190221, -0.0653304397019383, 0.0962356276431819, 
-0.0881336358361295, -0.0533254209585721, -0.0677359893415996, 
-0.204503703935188, 0.0372313692423631, 0.19837873915094, 0.198078832727333, 
0.197325568842643, -0.322720231088169, 0.208725442644209, 0.205065749287314, 
0.167832192164497, 0.0962356276431819, -0.0881336358361295, -0.0533254209585721, 
-0.0677359893415996, -0.204503703935188, 0.0372313692423631, 
0.19837873915094, 0.198078832727333, 0.0620278860878898, 0.0595328980125487, 
-0.00318802449328359, 0.172116678731982, 0.166721905588929, 0.166025866019481, 
0.0555906986846821, 0.198078832727333, 0.198078832727333, 0.162118304069736, 
0.159184927360911, 0.31501187786489, 0.480946872812638, 0.100753906190221, 
0.0145019028423121, 0.0341210115948343, 0.0196030874576536, 0.0151630909385858, 
0.0171717618286493, 0.0166905201695045, 0.0386720310416422, 0.0170905304912594, 
0.0321437632010202, 0.0290415791823762, 0.0290159981886973, 0.0224570892896736, 
0.0550586113604368, 0.00599850245635025, 0.0569822685065446, 
0.00715276200935477, 0.0707630950273597, 0.0603664986556396, 
0.00360865669790655, 0.0138247363793198, 0.00322926134685986, 
0.00569701842323411, 0.0788849310993101, 0.0787162032793276, 
0.023551956837764, 0.0786520142355585, 0.0588474418691476, 0.0220792353502475, 
0.00569701842323411, 0.0788849310993101, 0.0787162032793276, 
-0.0533254209585721, -0.0677359893415996, -0.204503703935188, 
0.00569701842323411, -0.288288556748739, 0.0193903986655641, 
0.0118731610200484, -0.0138466287353367, -0.680894528341014, 
-0.325431222609041, -0.695109194475663, 0.000547890522284433, 
0.178201925700705, 0.17427996663173, 0.215817989919742, -0.0149371458064707, 
-0.0149838482684572, -0.0245679943873256, 0.0144083751365542, 
0.0886160698428284, 0.202712868485833, -0.0466606414192938, 0.218713049616781, 
-0.862754137233424, 0.035604577169579, 0.0290415791823762, 0.0290159981886973, 
0.0224570892896736, 0.0550586113604368, 0.00599850245635025, 
0.0569822685065446, -0.195995577814756, 0.0630952594292467, 0.0203639445317094, 
0.00807336509751622, 0.0235056655583321, 0.00701949902577326, 
0.00712929831934161, -0.057338921851624, 0.00565966965950793, 
0.271263730610372, 0.270847501633398, 0.165073225434753, 0.162118304069736, 
0.159184927360911, 0.31501187786489, 0.480946872812638, 0.100753906190221, 
-0.0653304397019383, 0.0962356276431819, 0.271263730610372, 0.270847501633398, 
0.0962356276431819, 0.00701949902577326, 0.00712929831934161, 
0.0962356276431819), `Retorno do fechamento em 1 dia (de 25Jun21 até 28Jun21) Em moeda orig ajust p/ prov` = c(-0.04575570274028, 
-0.324605952846468, -0.327796321016649, -0.33096953857239, 0.310205801906704, 
-0.414644634292927, -0.101955446643842, -0.792592311699991, -0.105553570938355, 
-0.426584116303275, 0.384709813624795, -0.793544384760025, 0.244644467056787, 
-0.113642475389497, -0.200582809884509, -0.200281002162228, -0.199668641380413, 
0.105843198252842, 0.05128885331942, 0.0487525394419208, -0.144266975257779, 
-0.105553570938355, -0.426584116303275, 0.384709813624795, -0.793544384760025, 
0.244644467056787, -0.113642475389497, -0.200582809884509, -0.200281002162228, 
-0.0204055452741159, 0.00692895773681812, -0.107229976947565, 
-0.021897017268202, -0.026910665746982, -0.0267514958977699, 
-0.0226308975470602, -0.200281002162228, -0.200281002162228, 
-0.327796321016649, -0.33096953857239, 0.310205801906704, -0.414644634292927, 
-0.101955446643842, 0.00656187421554932, 0.0711173595846049, 
0.016080117529782, 0.0149099576447043, 0.0224479716052883, 0.0160585161211202, 
0.0291353537249961, 0.0191598079254618, 0.0344285706887604, 0.0277284529147437, 
0.0324757711496204, 0.255325436228304, 0.491043761394394, -0.007351639578701, 
0.239665514345688, 0.0254665781540098, 0.246601338403707, 0.306360405738815, 
0.021799597197969, 0.0359414310878492, 0.0214967376450659, 0.2530865072913, 
0.273972895593033, 0.273773812295985, 0.216667755375965, 0.273744292098854, 
0.306647065735888, 0.215286427555839, 0.2530865072913, 0.273972895593033, 
0.273773812295985, 0.384709813624795, -0.793544384760025, 0.244644467056787, 
0.2530865072913, -0.111104031839204, 0.157537165796384, 0.192098881598213, 
0.0864217152411584, -0.0945294063967594, 0.521661975290044, -0.0958257593993039, 
-0.0476185044135491, 0.42623207118595, 0.405392333959753, 0.856841270979203, 
0.0956091435000417, 0.0959779299591901, 0.237147073858068, 0.219546595326392, 
0.196438972307078, -0.0241040951550531, 0.163301830616547, -0.000792533046478638, 
-0.264652587702585, 0.312989899975946, 0.0277284529147437, 0.0324757711496204, 
0.255325436228304, 0.491043761394394, -0.007351639578701, 0.239665514345688, 
0.428198364716081, 0.0950000918237492, 0.60943404041609, 0.238058063587232, 
0.161433682296774, 0.221308266009146, 0.222009271055867, 0.0339463511409122, 
0.219266381827765, -0.0260104882727319, -0.0264767391854548, 
-0.324605952846468, -0.327796321016649, -0.33096953857239, 0.310205801906704, 
-0.414644634292927, -0.101955446643842, -0.792592311699991, -0.105553570938355, 
-0.0260104882727319, -0.0264767391854548, -0.105553570938355, 
0.221308266009146, 0.222009271055867, -0.105553570938355), `Retorno do fechamento em 1 dia (de 24Jun21 até 25Jun21) Em moeda orig ajust p/ prov` = c(1.31500839142973, 
1.2538675655378, 1.2512416806203, 1.2483983013226, 0.923949156276649, 
1.53366348040436, 1.07913114661642, 0.981676446463098, 1.06919162481063, 
1.00356835209823, 0.0938292467253632, 0.981684204998601, 0.0335199194523739, 
-0.160056370532402, 0.625390207096643, 0.624604057702527, 0.622362618378247, 
0.361656406130351, -0.212902135081094, -0.213394554157276, 0.499070715704875, 
1.06919162481063, 1.00356835209823, 0.0938292467253632, 0.981684204998601, 
0.0335199194523739, -0.160056370532402, 0.625390207096643, 0.624604057702527, 
-0.0216325648580096, 0.212004635613994, 0.0234427629038692, 0.270697217638372, 
0.264927992066077, 0.264052262537007, 0.0876192654686747, 0.624604057702527, 
0.624604057702527, 1.2512416806203, 1.2483983013226, 0.923949156276649, 
1.53366348040436, 1.07913114661642, 0.0221270523979911, 0.0283598365058424, 
0.0168144269991899, 0.0146372844028519, 0.0276347756880568, 0.0168952665262623, 
0.0280305044725537, 0.0247853928158293, 0.0215260746699641, 0.0224150851863669, 
0.0331112487401697, -0.11593472909226, -1.49322215711436, -0.0149391249578912, 
0.0804914230684517, 0.118418995771208, -0.245298802747129, -0.184812617681018, 
0.114302423753543, -0.0609414437349187, 0.114008090167772, 0.109802194674558, 
-0.27181676514374, -0.271866577804758, 0.0921201453820686, -0.27202933197259, 
-0.188102052743488, 0.0906900539121125, 0.109802194674558, -0.27181676514374, 
-0.271866577804758, 0.0938292467253632, 0.981684204998601, 0.0335199194523739, 
0.109802194674558, -0.228354444789147, 0.00782648858148605, -0.175804663012968, 
-0.248384703172633, -0.95458747309749, -1.45392726271893, -0.968984441897192, 
-0.0592346845223801, -0.0362318211045931, -0.0311027364659822, 
-0.835893042494718, -0.274180758242437, -0.275284712279245, -0.219692738210142, 
0.115211368756718, -0.222768642652227, -0.06096580209487, -0.155160100985086, 
-0.104768661731214, 0.11422998886701, 0.109853295543871, 0.0224150851863669, 
0.0331112487401697, -0.11593472909226, -1.49322215711436, -0.0149391249578912, 
0.0804914230684517, -0.695003834425734, -0.208113736607629, -1.01258187478379, 
-0.00637666562397499, -0.255406660107838, -0.00643533894617576, 
-0.00635902624708251, 0.00232062830036739, -0.00783108380346675, 
0.157384331396315, 0.156946501556376, 1.2538675655378, 1.2512416806203, 
1.2483983013226, 0.923949156276649, 1.53366348040436, 1.07913114661642, 
0.981676446463098, 1.06919162481063, 0.157384331396315, 0.156946501556376, 
1.06919162481063, -0.00643533894617576, -0.00635902624708251, 
1.06919162481063)), row.names = c(NA, -130L), class = c("tbl_df", 
"tbl", "data.frame"))
Labels: 
"CNPJ" = Fund/Stock ID,
"NOME_CLIENTE" = Client Name,
Other columns = Return in Day, Return in Day-1, Return D-2,... Return D-n for a Year or aprox 260 days)

我需要做的是计算每个唯一 NOME_CLIENTE 的基金的相关矩阵。所以代码需要给我一个矩阵列表或类似的东西作为输出。 因此,对于客户“Andre”-> 他的 7 个基金的 Corr 矩阵; 客户“Leandro”-> 他的 10 个基金的 Corr 矩阵 等等...

我需要这种方式,因为之后我需要将一个值乘以每个客户矩阵以获得投资组合波动率。

有关投资组合波动率的更多信息: https://financetrain.com/analytical-approach-to-calculating-var-variance-covariance-method/

这将为您提供一个矩阵列表,每个客户一个:

library(RiskPortfolios)
library(tibble)

dat_clean <- tibble::as.tibble(dat)
names(dat_clean)[3:10] <- paste0('day', 8:1)

accounts <- unique(dat_clean$CNPJ)
clients <- unique(dat_clean$NOME_CLIENTE)

lcorrelations <-  vector(mode = "list", length = length(clients)) # list of correlations
names(lcorrelations) <- clients

for(i in clients) {
  returns_account_i <- t(as.matrix(dat_clean[dat_clean$NOME_CLIENTE == i, c(3:8)]))
  lcorrelations[[i]] <- RiskPortfolios::covEstimation(returns_account_i)
}

# correlation matrix for André
> print(lcorrelations$André)
          [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]
[1,] 0.3940826 0.3110904 0.3108791 0.3106319 0.2485066 0.2383790 0.2778012
[2,] 0.3110904 0.3052529 0.3049751 0.3046908 0.2842727 0.2151196 0.2735981
[3,] 0.3108791 0.3049751 0.3046987 0.3044156 0.2839959 0.2148306 0.2733327
[4,] 0.3106319 0.3046908 0.3044156 0.3041340 0.2837319 0.2145246 0.2730624
[5,] 0.2485066 0.2842727 0.2839959 0.2837319 0.3214527 0.1938825 0.2652854
[6,] 0.2383790 0.2151196 0.2148306 0.2145246 0.1938825 0.1852168 0.1878199
[7,] 0.2778012 0.2735981 0.2733327 0.2730624 0.2652854 0.1878199 0.2530376