按功能滚动,按组滚动不起作用

时间:2019-02-14 09:40:43

标签: r group-by correlation rollapply

我想按组计算侧倾相关性,使用rollapply在组中找到一些问题

usmergetemp=structure(list(fundid = c(105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 105L, 
105L, 105L, 105L, 105L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 
2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 
2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 
2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 
2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 
2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 2700L, 
2700L, 2700L, 2700L, 2700L, 2700L, 2701L, 2701L, 2701L, 2701L, 
2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 
2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 
2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 
2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 
2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 
2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2701L, 2702L, 2702L, 
2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 
2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 
2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 2702L, 
2702L, 2702L, 2702L, 2702L, 2702L, 2702L), return = c(0.0486055777, 
0.0151975684, 0.0172155689, 0.0125091979, -0.074127907, -0.007849294, 
-0.011075949, 0.0184, 0.0259230165, 0.0413476263, 0.0235294118, 
0.0133823252, 0.0172981878, -0.0048583, 0.0179007323, 0.0439648281, 
0.0451761103, -0.005128205, -0.033873343, 0.0152439024, 0.0585585586, 
0.0602836879, -0.062207358, -0.004154582, -0.087371512, -0.016090105, 
-0.022894522, 0.0836820084, 0.0347490347, -0.110447761, -0.015939597, 
-0.020460358, -0.147954743, -0.249233912, -0.144217687, 0.0606677266, 
-0.060784314, -0.091858038, 0.0988505747, 0.1569037657, 0.0813743219, 
0, 0.0986622074, 0.0106544901, 0.063253012, -0.035410765, 0.046989721, 
0.04628331, -0.054959786, 0.0354609929, 0.0753424658, 0.0229299363, 
-0.079701121, -0.048714479, 0.0782361309, -0.04353562, 0.1131034483, 
0.0408921933, 0.0083333333, 0.0696576151, 0.0055187638, 0.0175631175, 
0.0183387271, 0.0349576271, -0.020470829, -0.008359457, -0.030558483, 
-0.067391304, -0.110722611, 0.129750983, -0.009280742, -0.005854801, 
0.0588928151, 0.0367074527, 0.0278969957, 0, -0.080375783, 0.020431328, 
-0.008898776, 0.0291806958, 0.0250817884, -0.008510638, 0.0246781116, 
0.0314136126, 0.0578680203, 0.0038387716, 0.0363288719, 0.0119926199, 
0.0218778487, -0.018733274, 0.0636363636, -0.024786325, 0.0622261174, 
0.0297029703, 0.0272435897, 0.0241809672, -0.044173648, 0.0517928287, 
-0.017424242, -0.021588281, 0.0102442868, 0.0319812793, -0.037792895, 
0.0314218382, -0.04112719, 0.02700556, 0.0146945089, -0.006859756, 
-0.021488872, 0.0611764706, 0.0051736881, 0.0044117647, 0.0109809663, 
-0.009413469, -0.002192982, -0.06959707, -0.04015748, 0.0631665299, 
0.0044032872, -0.029295329, -0.062805873, 0.001740644, 0.0695047785, 
0.0064987815, 0.008071025, -0.010408327, 0.0517799353, 0.0023076923, 
0.0038372985, -0.027522936, 0.0117924528, 0.0045547673, 0.0281469898, 
0.0266159696, 0.0177777778, 0.0247452693, 0.0205965909, 0.0048712596, 
0.0166204986, 0.0115803815, 0.0276094276, 0.0275229358, 0.0191326531, 
0.0104152434, 0.0427807487, -0.041025641, -0.005347594, -0.012096774, 
0.0156462585, -0.006028131, 0.0121293801, 0.0306258322, -0.014211886, 
-99, 0.001984127, -0.001967333, 0.002997003, -0.009960159, 0.0014713945, 
0.0101419878, 0.0180722892, 0.0105393614, 0.0137931034, 0.0145772595, 
-0.001783916, 0.0038797284, 0.0086956522, 0.0031226054, 0.0144508671, 
0.0018993352, -0.006853638, -0.000963391, 0.0115718419, 0.0156912157, 
0.0113744076, 0.0037488285, -0.000775832, -0.002851711, -0.004766444, 
0.0013421682, 0.0096525097, -0.001912046, -0.021388889, -0.003952569, 
0.0079365079, -0.034095219, -0.061728395, 0.0065789474, 0.0351449604, 
-0.034115139, -0.034216336, 0.0369213678, 0.0355951057, 0.0268528464, 
0.003068322, 0.0347003155, 0.0172764228, 0.0215054945, -0.004926108, 
0.0247524752, -0.004700329, -0.000978474, 0.0097943193, 0.0178622696, 
0.0105566219, -0.020892688, -0.002399363, 0.0303623898, -0.004752852, 
-99, 0.0009832842, 0.0049115914, 0.0078201369, -0.017458778, 
0.0013751004, 0.0069721116, 0.0197823937, 0.0126091174, 0.0220306513, 
0.0168697282, 0.0044339499, 0.0074487896, 0.0018484288, 0.0073800738, 
0.0228937729, 0.0125335721, -0.00868484, -0.011754069, 0.0118938701, 
0.0235081374, 0.0150176678, -0.011314186, -0.004762324, -0.023593466, 
-0.011152416, -0.00093985, 0.0225776105, 0.0055197792, -0.041207685, 
-0.007751938, 0.0068359375, -0.056256062, -0.101747174, -0.02173913, 
0.0357953557, -0.055555556, -0.058823529, 0.0546875, 0.0641975309, 
0.0440835267, 0.0015054246, 0.0529279279, 0.022459893, 0.0292887029, 
-0.012195122, 0.0349794239, 0.0065286035, -0.015984016, 0.0152284264, 
0.034, 0.0106382979, -0.044019139, -0.017546157, 0.0474716202, 
-0.018719212, -99, 0, 0.0126705653, 0.0125120308, -0.025665399, 
0.0009982361, 0.0049164208, 0.0215264188, 0.0162835249, 0.0292177191, 
0.0201465201, 0.0086808093, 0.0126126126, -0.005338078, 0.011627907, 
0.0309460654, 0.0231560892, -0.011282682, -0.020547945, 0.0104895105, 
0.0311418685, 0.019295302, -0.027160494, -0.006768131, -0.045178106, 
-0.018198362, -0.002780352, 0.0371747212, 0.0116487455, -0.061662715, 
-0.011472275, 0.0058027079, -0.077884615, -0.142857143, -0.0486618
), sp500monthly = c(0.0254667713486416, 0.000453157630725398, 
0.0110958104592496, 0.0121556527379414, -0.0309169161411509, 
8.65962277825094e-05, 0.00508587879799083, 0.0212741930323481, 
0.0245662985125095, 0.0315080029615542, 0.0164666567278202, 0.0126157828526599, 
0.0140590427350398, -0.0218461760335282, 0.00997998289683055, 
0.0432906906024242, 0.0325492287099349, -0.0178163222021676, 
-0.0319818783168025, 0.0128635715315568, 0.0357940083432995, 
0.0148223383003112, -0.0440434172248144, -0.00862850903396861, 
-0.0611634319359364, -0.0347611927726245, -0.00595962361452984, 
0.0475466979131989, 0.0106741816575771, -0.0859623849028036, 
-0.00985937109375001, 0.01219046453238, -0.0907914337790846, 
-0.16942452376742, -0.0748490425806452, 0.00782157689705398, 
-0.0856573429283144, -0.109931197571492, 0.0854044616224947, 
0.0939250798621647, 0.0530814462553855, 0.000195826530303034, 
0.0741417270167166, 0.0335601892404949, 0.0357233457884587, -0.0197620008604155, 
0.0573640619813736, 0.0177705711884004, -0.0369742461549474, 
0.0285136889405313, 0.0587964260318918, 0.0147592298837911, -0.0819758419103345, 
-0.0538824420264151, 0.0687778499115523, -0.0474491840402872, 
0.0875511029440201, 0.0368559943970765, -0.00229024979894321, 
0.0653000404898547, 0.0226455739800868, 0.0319565640529522, -0.00104731320381857, 
0.0284953804437951, -0.0135009527669306, -0.0182574612656971, 
-0.021474425791952, -0.0567911074635976, -0.0717619883037601, 
0.10772303853581, -0.00505871519358725, 0.00853276394814406, 
0.0435830622185063, 0.0405894641308417, 0.0313323145305306, -0.00749745270927038, 
-0.0626507256331776, 0.0395549821345915, 0.0125975741261544, 
0.0197633696801482, 0.024236153696477, -0.0197894098782274, 0.0028467170173434, 
0.00706823046386451, 0.0504280965195785, 0.0110606491952592, 
0.0359877235169561, 0.0180857678592523, 0.0207628117210461, -0.0149993016360628, 
0.049462079815225, -0.0312980190338669, 0.0297495231772391, 0.0445957526180061, 
0.0280494716351865, 0.0235627915504928, -0.0355829056751626, 
0.0431170299765953, 0.00693216560793575, 0.00620078896505283, 
0.021030280012996, 0.0190583316589206, -0.0150798305819199, 0.0376552954897351, 
-0.0155138372230638, 0.0232014607867723, 0.0245335887603648, 
-0.00418858787792042, -0.0310408057904702, 0.0548925110145539, 
-0.0173961069137563, 0.00852081973012475, 0.0104913823933168, 
-0.0210116723759005, 0.0197420296967215, -0.0625808181672028, 
-0.0264428315732271, 0.0829831177603941, 0.000504869260724128, 
-0.0175301851763144, -0.0507353219729464, -0.00412836043029907, 
0.0659911145773651, 0.00269939848087319, 0.0153246023575726, 
0.000910921120978125, 0.0356098011252544, -0.00121924313604804, 
-0.00123445084432539, -0.0194256792795575, 0.0341745221875704, 
0.0182007621968951, 0.0178843581714645, 0.0371981603372791, -0.0003891971880845, 
0.00909120854931822, 0.0115762513913413, 0.00481377509085545, 
0.0193488261180306, 0.000546432811085573, 0.0193029785332437, 
0.0221881353303497, 0.0280826276644586, 0.00983163048260405, 
0.0561787044441331, -0.0389473720618969, -0.0268844986248251, 
0.00271877513164354, 0.0216083419652919, 0.00484243602418655, 
0.0360215562213673, 0.0302632114660546, 0.00429428710266144, 
0.0254667713486416, 0.000453157630725398, 0.0110958104592496, 
0.0121556527379414, -0.0309169161411509, 8.65962277825094e-05, 
0.00508587879799083, 0.0212741930323481, 0.0245662985125095, 
0.0315080029615542, 0.0164666567278202, 0.0126157828526599, 0.0140590427350398, 
-0.0218461760335282, 0.00997998289683055, 0.0432906906024242, 
0.0325492287099349, -0.0178163222021676, -0.0319818783168025, 
0.0128635715315568, 0.0357940083432995, 0.0148223383003112, -0.0440434172248144, 
-0.00862850903396861, -0.0611634319359364, -0.0347611927726245, 
-0.00595962361452984, 0.0475466979131989, 0.0106741816575771, 
-0.0859623849028036, -0.00985937109375001, 0.01219046453238, 
-0.0907914337790846, -0.16942452376742, -0.0748490425806452, 
0.00782157689705398, -0.0856573429283144, -0.109931197571492, 
0.0854044616224947, 0.0939250798621647, 0.0530814462553855, 0.000195826530303034, 
0.0741417270167166, 0.0335601892404949, 0.0357233457884587, -0.0197620008604155, 
0.0573640619813736, 0.0177705711884004, -0.0369742461549474, 
0.0285136889405313, 0.0587964260318918, 0.0147592298837911, -0.0819758419103345, 
-0.0538824420264151, 0.0687778499115523, -0.0474491840402872, 
0.0254667713486416, 0.000453157630725398, 0.0110958104592496, 
0.0121556527379414, -0.0309169161411509, 8.65962277825094e-05, 
0.00508587879799083, 0.0212741930323481, 0.0245662985125095, 
0.0315080029615542, 0.0164666567278202, 0.0126157828526599, 0.0140590427350398, 
-0.0218461760335282, 0.00997998289683055, 0.0432906906024242, 
0.0325492287099349, -0.0178163222021676, -0.0319818783168025, 
0.0128635715315568, 0.0357940083432995, 0.0148223383003112, -0.0440434172248144, 
-0.00862850903396861, -0.0611634319359364, -0.0347611927726245, 
-0.00595962361452984, 0.0475466979131989, 0.0106741816575771, 
-0.0859623849028036, -0.00985937109375001, 0.01219046453238, 
-0.0907914337790846, -0.16942452376742, -0.0748490425806452, 
0.00782157689705398, -0.0856573429283144, -0.109931197571492, 
0.0854044616224947, 0.0939250798621647, 0.0530814462553855, 0.000195826530303034, 
0.0741417270167166, 0.0335601892404949, 0.0357233457884587, -0.0197620008604155, 
0.0573640619813736, 0.0177705711884004, -0.0369742461549474, 
0.0285136889405313, 0.0587964260318918, 0.0147592298837911, -0.0819758419103345, 
-0.0538824420264151, 0.0687778499115523, -0.0474491840402872, 
0.0254667713486416, 0.000453157630725398, 0.0110958104592496, 
0.0121556527379414, -0.0309169161411509, 8.65962277825094e-05, 
0.00508587879799083, 0.0212741930323481, 0.0245662985125095, 
0.0315080029615542, 0.0164666567278202, 0.0126157828526599, 0.0140590427350398, 
-0.0218461760335282, 0.00997998289683055, 0.0432906906024242, 
0.0325492287099349, -0.0178163222021676, -0.0319818783168025, 
0.0128635715315568, 0.0357940083432995, 0.0148223383003112, -0.0440434172248144, 
-0.00862850903396861, -0.0611634319359364, -0.0347611927726245, 
-0.00595962361452984, 0.0475466979131989, 0.0106741816575771, 
-0.0859623849028036, -0.00985937109375001, 0.01219046453238, 
-0.0907914337790846, -0.16942452376742, -0.0748490425806452)), row.names = c(6403L, 
6404L, 6405L, 6406L, 6407L, 6408L, 6409L, 6410L, 6411L, 6412L, 
6413L, 6414L, 6415L, 6416L, 6417L, 6418L, 6419L, 6420L, 6421L, 
6422L, 6423L, 6424L, 6425L, 6426L, 6427L, 6428L, 6429L, 6430L, 
6431L, 6432L, 6433L, 6434L, 6435L, 6436L, 6437L, 6438L, 6439L, 
6440L, 6441L, 6442L, 6443L, 6444L, 6445L, 6446L, 6447L, 6448L, 
6449L, 6450L, 6451L, 6452L, 6453L, 6454L, 6455L, 6456L, 6457L, 
6458L, 6459L, 6460L, 6461L, 6462L, 6463L, 6464L, 6465L, 6466L, 
6467L, 6468L, 6469L, 6470L, 6471L, 6472L, 6473L, 6474L, 6475L, 
6476L, 6477L, 6478L, 6479L, 6480L, 6481L, 6482L, 6483L, 6484L, 
6485L, 6486L, 6487L, 6488L, 6489L, 6490L, 6491L, 6492L, 6493L, 
6494L, 6495L, 6496L, 6497L, 6498L, 6499L, 6500L, 6501L, 6502L, 
6503L, 6504L, 6505L, 6506L, 6507L, 6508L, 6509L, 6510L, 6511L, 
6512L, 6513L, 6514L, 6515L, 6516L, 6517L, 6518L, 6519L, 6520L, 
6521L, 6522L, 6523L, 6524L, 6525L, 6526L, 6527L, 6528L, 6529L, 
6530L, 6531L, 6532L, 6533L, 6534L, 6535L, 6536L, 6537L, 6538L, 
6539L, 6540L, 6541L, 6542L, 6543L, 6544L, 6545L, 6546L, 6547L, 
6548L, 6549L, 6550L, 6551L, 6552L, 6553L, 6554L, 6555L, 145721L, 
145722L, 145723L, 145724L, 145725L, 145726L, 145727L, 145728L, 
145729L, 145730L, 145731L, 145732L, 145733L, 145734L, 145735L, 
145736L, 145737L, 145738L, 145739L, 145740L, 145741L, 145742L, 
145743L, 145744L, 145745L, 145746L, 145747L, 145748L, 145749L, 
145750L, 145751L, 145752L, 145753L, 145754L, 145755L, 145756L, 
145757L, 145758L, 145759L, 145760L, 145761L, 145762L, 145763L, 
145764L, 145765L, 145766L, 145767L, 145768L, 145769L, 145770L, 
145771L, 145772L, 145773L, 145774L, 145775L, 145776L, 145777L, 
145778L, 145779L, 145780L, 145781L, 145782L, 145783L, 145784L, 
145785L, 145786L, 145787L, 145788L, 145789L, 145790L, 145791L, 
145792L, 145793L, 145794L, 145795L, 145796L, 145797L, 145798L, 
145799L, 145800L, 145801L, 145802L, 145803L, 145804L, 145805L, 
145806L, 145807L, 145808L, 145809L, 145810L, 145811L, 145812L, 
145813L, 145814L, 145815L, 145816L, 145817L, 145818L, 145819L, 
145820L, 145821L, 145822L, 145823L, 145824L, 145825L, 145826L, 
145827L, 145828L, 145829L, 145830L, 145831L, 145832L, 145833L, 
145834L, 145835L, 145836L, 145837L, 145838L, 145839L, 145840L, 
145841L, 145842L, 145843L, 145844L, 145845L, 145846L, 145847L, 
145848L, 145849L, 145850L, 145851L, 145852L, 145853L, 145854L, 
145855L, 145856L, 145857L, 145858L, 145859L, 145860L, 145861L, 
145862L, 145863L, 145864L, 145865L, 145866L, 145867L), class = "data.frame")

我尝试使用分组依据来计算每个基金的相关性

usmergetemp%>%group_by(usmergetemp$fundid)
           %>%mutate(
             rollapplyr(usmergetemp, 3, function(usmergetemp) {
                cor(usmergetemp[,2], usmergetemp[,3])
               }, by.column = FALSE,fill=NA)
           )

它们具有相同的行,应该可以工作,但是不能

  

mutate_impl(.data, dots)中的错误:列

rollapplyr(usmergetemp, 3, function(usmergetemp) {cor(usmergetemp[, 2], usmergetemp[, 3])},by.column = FALSE, fill = NA) 
     

长度必须为153(组的大小)或一个,而不是300

我该如何解决此问题…… 谢谢您的帮助

1 个答案:

答案 0 :(得分:0)

请考虑我上面的评论。

针对您的特定问题,我们可以执行以下操作:

library(dplyr)
library(zoo)
usmergetemp %>%
    group_by(fundid) %>%
    mutate(cor = rollapplyr(
        data = cbind(return, sp500monthly),
        width = 3,
        FUN = function(w) cor(w[, 1], w[, 2]),
        by.column = FALSE,
        fill = NA))
## A tibble: 300 x 4
## Groups:   fundid [4]
#   fundid   return sp500monthly     cor
#    <int>    <dbl>        <dbl>   <dbl>
# 1    105  0.0486     0.0255     NA
# 2    105  0.0152     0.000453   NA
# 3    105  0.0172     0.0111      0.927
# 4    105  0.0125     0.0122     -0.163
# 5    105 -0.0741    -0.0309      0.998
# 6    105 -0.00785    0.0000866   0.999
# 7    105 -0.0111     0.00509     0.985
# 8    105  0.0184     0.0213      0.947
# 9    105  0.0259     0.0246      0.999
#10    105  0.0413     0.0315      1.000
## ... with 290 more rows

或者您可以使用tidyr::nest

library(tidyverse)
usmergetemp %>%
    group_by(fundid) %>%
    nest() %>%
    mutate(data = map(data, ~bind_cols(.x, cor = rollapplyr(
        data = .x,
        width = 3,
        FUN = function(w) cor(w[, 1], w[, 2]),
        by.column = FALSE,
        fill = NA)))) %>%
    unnest()