我有一个回归结果列表,我有兴趣提取回归器和预测器的名称。
这就是我所追求的:
set.seed(1)
DF <- data.frame(A=rnorm(50, 100, 3),
B=rnorm(50, 100, 3),
C=rnorm(50, 100, 3),
D=rnorm(50, 100, 3),
E=rnorm(50, 100, 3))
results <- combn(names(DF), 2, function(x){coefficients(lm(DF[, x]))}, simplify = FALSE)
vars <- combn(names(DF), 2)
names(results) <- vars[1 , ] # adding names to identify variables in the reggression
results
效果很好,它会打印回归变量和预测变量名称。但是,当我尝试使用自己的数据进行操作时,提取名称(在代码vars <- combn(names(results), 2)
的行)遇到了问题。
我正在使用的代码如下;
stock_pairs <- stock_pairs %>%
select(-date)
results <- combn(names(stock_pairs), 2, function(x){coefficients(lm(stock_pairs[, x]))}, simplify = FALSE)
vars <- combn(names(results), 2)
names(results) <- vars[1 , ]
results
我还试图使用管道函数将其变成一个dplyr
代码(但这是我的问题的第二要点)
回归函数基本上只是将所有预测变量相互回归A ~ B
,A ~ C
,B ~ A
,B ~ C
等。
数据:
stock_pairs <- structure(list(GOOG = c(0, 0.000966293343200162, 0.0090074689946936,
0.0151611841458238, 0.000620039683950682, -0.00230855238019536,
0.00386928627438225, -0.00192035844891083, 0.00188326451911598,
-0.00405586949742516, 0.00181292672304858, -0.00484382246202279,
0.00354037308171213, 0.0175953734989809, 0.0055502242302219,
0.0142210335668011, -0.00422103609685294, -0.0106799438315541,
-0.0258252585374839, -0.0069164111246091, -0.0013751729942476,
0.00355661832483592, 0.00369990917801844, -0.000187122775150381,
0.00700109680736293, 0.0017457942601596, 0.00145863654650583,
0.00506396977762857, 0.00682221150659847, 0.00147591583552512,
-0.00179334573742445, 0.00630501394679104, 0.00473304727141334,
0.00432597066006709, -0.00108271447302099, 0.000685891874792682,
-0.0032410276123664, 0.00077206863395908, -0.00734653052455343,
0.0145077378423174, -0.00553464169675315, -0.00186778223914187,
-0.00156921916235626, 0.00497677580671881, 0.00415050505430148,
0.00395448443856544, 0.00543425501117094, 0.00271197654339779,
9.46297231927052e-05, 0.00186672824315268, 0.00186325006063216,
0.00392729760214958, -0.00437510513521429, -0.0213724603510628,
-0.00104815524107156, -0.0145828572831499, -0.00386030464579232,
0.00621813941639168, 0.00171902897804797, 0.0126973803235555,
0.000108276456993617, -0.0023358612987251, 0.0107787581329626,
-0.00475756493131297, -0.00379360835991813, -0.00425479979044851,
-0.00388493668650555, 7.27500926061874e-05, -0.00167468127684088,
0.00117745801292737, -0.000922408247664938, 0.0163907316117439,
-0.000418133928147298, 0.00165969002944316, 0.00409558738740454,
0.00182804112566795, 0.0229442438387077, 0.0109968233375556,
-0.000653667690068243, 0.00288665595111591, 0.0356288032981064,
0.00726962327222607, 0.00423179835683304, 0.011500089978888,
0.00497122143035681, -0.00487411382868475, 0.0077037745038584,
-0.00228238970650258, -0.00364325503403967, 0.00195758540620744,
0.00173929360269284, 0.00519986838061685, 0.00629760702941299,
-0.0251057468316146, 0.0114820708187541, 0.00404454889556885,
0.00836947213168581, 0.00736248700903966, 0.0064503631675052,
0.0151522316966413, 0.00198864888564169, 0.00452927475124973,
-0.0113566356102381, 0.00216380267077167, 0.00890584152305572,
0.00824799205252212, -0.00725419429076609, 0.00446485815096191,
0.00251479861852849, -0.034743057161651, -0.0073227811860945,
0.0110743100187701, -0.00277289266073761, -0.00892736991756667,
-0.00268846918857436, 0.0185441002026491, -0.00706500842259779,
0.00923528816595437, -0.00246275699415173, 0.00884188230793016,
-0.0138907124081958, -0.0265391214237854, 0.0140914978048769,
-0.024432423098802, -0.00992058396707802, -0.0110987103267631,
0.0143726931645815, -0.00552137339080005, 0.0130393073035135,
0.0110535023695418, 0.00138796722419316, 0.0146646985062731,
0.00352192180778849, 0.00927943728324645, -0.00269194001111632,
0.0124870458123842, 0.00567064396433728, -0.00282613331193697,
0.00491478265529781, 0.00759763641799992, -0.030700909824989,
-0.00305507111467409, -0.0145706746378703, 0.00793342020250434,
-0.0117841663549175, 0.000354603429208211, -0.000472810307896916,
-0.00727063121510231, 0.00465541412834804, 0.00150750894821727,
-0.00276918211733244, -0.00420606667123238, -0.0171138995867216,
0.00785018000240295, 0.00901442966270229, -0.000487846993745437,
0.00512666306114793, -0.0173895172545288, -0.000340347497366451,
-0.00441308579631302, 0.0196910604957548, 0.00249501719600745,
-0.00618952666410085, -0.00586775239741761, -0.00227361631004656,
0.00815216775440654, 0.00894728321364058, 0.0104447521038483,
-0.00212076784858353, -0.00952956344623036, -0.000689573682472715,
0.00873510077963697, -0.0101480233431214, 0.0027808217796724,
0.00321305916639546, 0.00323488354354484, -0.0107301770682653,
-0.00522381928844402, -0.00576474858279657, 0.00741506112260322,
0.0105429611887869, 0.000933456059866791, -0.00421282196611187,
-0.00817529073432247, 0.00421492754128763, 0.0210027301976658,
0.00529044156704342, 0.0100702252291663, -0.00610755512854431,
0.00473032393847275, -0.00639968781511331, 0.0190260197387787,
0.00916443577551919, -0.00193263994783713, -0.00451377874922349,
0.0169742069719745, -0.00143644489039719, 0.00187101610644631,
0.00234145580328989, 0.000181428097379014, 0.000634768965093619,
-0.00845618256995646, 0.00380199677755158, -0.0201882508757583,
0.00215572728084805, 0.00287060443660233, -0.000791431205522348,
0.0469102152089025, -0.00212144668952346, -0.000462170874852467,
0.00867721167679869, 7.79647812427984e-05, 0.00670539252301603,
-0.00639335632893446, 0.00721625492493018, 0.00628989447098761,
-0.00829508361522969, -0.00309815971198043, -0.00225915323417209,
0.000243694408419296, -0.00497338677203654, 0.0112886858765094,
-0.0130729471341859, -0.000696964389748216, 0.0156954076554658,
0.00141995342247034, 0.00447856984669354, 0.0129845690117649,
-0.00647115153598449, -0.0248917537438116, -0.000244729752632558,
-0.0110653825407706, -0.01143949566898, 0.00645768695885337,
0.0130763263469312, 0.0122482255332756, 0.00591883147976654,
0.00389763210387064, -0.000595697529222388, 0.000124939336939348,
0.00817327014048158, 0.0142335555705874, 0.0120955034020904,
-0.00601538329268131, -0.00536620738330303, -0.00124021290857934,
-0.0033054862351345, -0.00319341656639125, -0.00699870864818039,
-0.00117280025851457, -0.00166145441754464, 0.0176190745048117,
0.0162798873357712, 0.00361481341644541, 0.0144658823286398,
0.00426401617325703, -0.000614432528867465, -0.00330488288624405,
0.00263574760373552, 0.0150286876171976, -0.000445628830240409,
0.0090694047211501, -0.00193648484678383, 0.00680986253879923,
0.015959776122234, 0.0121766347418437, -0.00490957763468695,
0.0052514288269984, 0.00466282302228719, -0.000221151482665393,
-0.010165665286132, 0.00535647489934816, -0.00191645487677716,
-0.0489656748249572, -0.0517714654940585, 0.0232176004002621,
-0.0300795942548336, -0.0459179600241953, 0.0355649782352119,
0.0135521782074945, 0.000152121638962785, 0.0165900455021265,
0.0183590611125606, 0.00483449230614706, 0.00697226818469494,
0.00802245272292764, -0.00424709814954704, 0.0180535512034148,
0.0149394207046846, -0.0225115698652217, -0.0121998246248623,
-0.0323909747248025, 0.00875061319135878, 0.011070011387602,
0.00377861719450381, 0.013226441747136, 0.0146358778041906, 0.0297829911844085,
0.0038372894403516, -0.0228700622506183, 0.0098966089575715,
7.82629516796855e-05, -0.0121210216175194, -0.0321291174315187,
-0.00192032485354177, -0.00624144154745484, -0.0390711668783695,
-0.0265728812161387, 0.0305019399817086, -0.0467555912677214,
-0.00053738246268583, 0.0267455630714486, -0.0248460651984959,
0.00687172455951917, 0.0115083481213191, 0.00260117919018553,
-0.0204150694039244, 0.00831656276670031, 0.0158179077177691,
-0.0113765968654414, 0.0122195540545793, -0.00314290818600742,
0.00842666493183759, 0.0342624949678155, -0.00193834674683357,
0.0144646843803585, -0.0136441821208007, -0.00514856723527396,
-0.0454895810598272, 0.00117581488381995, 0.0183003969142755,
-0.00965181912751075, -0.0124258274039955, 0.0194493157805032,
-0.0125433242896095, -0.000644533011653594, 0.0236408868998192,
0.00625782165778023, -0.000834642216172873, 0.0270062563080953,
0.0135852500069056, 0.000628522277721281, 0.00176481858930644,
-0.01924413155939, 0.00235080163789439, -0.00294400592257485,
-0.0114036353428134, 0.012321070118775, -0.00916577436704232,
0.00926764676404835, -0.000416827799785395, -0.00332262216681353,
-0.0143637598544043, 0.00702980454293423, 0.0159702581707224,
0.0313113959132592, 0.0175231385391787, 0.000324706545908579,
-0.00244232944228895, -0.0115184974926099, -0.00266401018826983,
0.00810360925934281, 0.00822277153203789, -0.00398389965413244,
0.0151560730197307, 0.000121520756011192, 0.018231376494048,
-0.00461231296486365, 0.00152265479691731, -0.0104662025208958,
-0.00188493098755512, -0.0269016397917698, -0.00566148069976443,
-0.0130308906251889, 0.00923276888251753, 0.00128263369547176,
0.010530064245453, -0.0220332595716296, 0.0191999330529817, 0.0140434587491539,
0.01210012447633, -0.00104910342766088, 0.000919096282388174,
0.0253117038839774, 0.00450193920290815), MSFT = c(0, -0.00448443657913182,
0, 0.00863046535802072, -0.0031877818108354, -0.000319308149288885,
0.009061315099402, -0.00922095798733338, 0.00143646162649006,
-0.0027150509684046, -0.000479859776626437, -0.00320528339142426,
0.00703791046493648, 0.00350034450928517, 0.00885532682127305,
0.00251571745641834, 0.00922224836877406, 0.023223044645141,
-0.00993076468548846, -0.00739700418945854, -0.0166891199304723,
-0.00646954144415619, 0.00804113723566055, -0.000628348441947572,
-0.00330527371936493, -0.00141985286791404, 0.0113029662502009,
-0.000936931335008999, 0.0111871815604482, 0.00372381147472705,
-0.000619779003988285, -0.000155000816233592, 0.00154878527737718,
-0.00201382063237854, -0.00201786835903327, 0.00403168899141174,
0, -0.00605352562961187, -0.00389993337445584, 0.014893336865151,
-0.0144244673191536, 0.00374226519088352, 0.000311232178676073,
0.00202081866865865, 0.00911966958224594, -0.00400861079711641,
0.00308494404033772, -0.0033939641039023, -0.00464681280433937,
0.00526480497490707, -0.00170025950428913, 0.00355182137714609,
0.000924410060477825, -0.0111507289325984, 0.0126896630984911,
-0.00246334422637035, 0.0016942994210158, 0.00184488815453254,
0.00291439027536369, 0.00275304414585069, 0.0036593236042856,
0.00227999816744775, -0.00471799099843951, 0.00274237318393239,
-0.00258993287646799, 0.00258993287646804, -0.000761038803227279,
-0.00228655073134709, -0.0007632372878373, -0.00382534191831436,
-0.00430172659217217, 0.00812706851048645, -0.00137534072302547,
-0.00536690275035425, 0.00704755193977413, 0.0136469339043457,
0.0168748612620552, 0.00575876898672483, -0.00132612600429195,
0.00646587531993426, 0.00277913348612604, 0.0137815422794146,
-0.0015862398658009, -0.0031794841763678, -0.00391628255600088,
0.00275735368290847, -0.000869859545408918, 0.00144948444557356,
0.00390315765736886, -0.0123396719216871, -0.00116920602137925,
0.000731000107042607, 0.0199352874513185, -0.0281998345440238,
0.00340259605207264, -0.000295285153058362, 0.0111648432808033,
0.00335480078384897, 0.00130927675576207, 0.0122845252778685,
0.00487164598824013, 0.00641186676361199, -0.00812855996116566,
0.00371586428358892, 0.0234044153760704, 0.00722022691791822,
0.00331481862549888, -0.00179399776862265, -0.00609691267751434,
-0.0229151666772291, -0.00770875221995588, 0.012390784157577,
-0.00539329770785667, -0.00527921628223077, 0.00142957678643742,
0.0123519656288482, -0.0136385569023304, 0.0051362307692754,
-0.000142252937075809, 0.0134306051511453, -0.00959508286334283,
-0.0188929037832966, 0.00848880935431377, -0.0189463372007687,
0.00640371188254955, -0.0110868860964024, 0.013260721610129,
-0.00741017644735752, 0.0128959062023207, 0.00745862192835243,
0.000142822049432454, 0.0164379467081533, 0.008675957781112,
0.0139747808963296, 0.00780127036293536, -0.000681679496794985,
0.00761061265046872, 0.0048623104046833, -0.00581046334013042,
-0.00257810281715621, 0.00798427425517367, -0.00188886893409198,
-0.0120915733306292, -0.00164165524221632, -0.00466594514768348,
-0.00165182670854323, -0.0044186853846604, -0.00152346006069832,
0.00731885945221052, -0.00385979962921363, 0.00537227085032541,
-0.00440592335226198, -0.0147347803478365, 0.0151485672422689,
0.0149225222165899, 0.000273193169382178, 0.00585560408071217,
-0.0171177453050601, 0.0012421831207741, -0.0047013346658123,
0.0139015862874112, -0.0060324043351581, -0.00041258028275084,
0.001786708002461, 0.000137471892944776, 0.0030161395258545,
0.0130560705402659, 0.010216429149033, -0.0111626759511146, -0.00447321403528829,
-0.00285675553849776, 0.0127251026526999, -0.00485428748731666,
0.0104882375601675, -0.00107068900946104, 0.00707177775167953,
-0.00586749594286826, 0.0071962544968942, -0.00199363860407965,
0.00371833558391663, -0.00664978548803454, -0.00978894835631906,
0.00269149271358557, -0.0155756758195009, 0, 0.00802127517907632,
0.000270788939499675, 0.0083580747859803, 0.00160969619814918,
-0.00470204475096702, 0.00577373783746314, 0.0169922799765607,
0.000394877435741089, 0.00380845752229486, 0, 0.00170257415318251,
0.00911830361595964, 0.0047862134668417, 0.00206259771535313,
-0.000773014887861776, 0.000257842564615007, 0.00385801560983773,
0.0114854469802238, 0.000253838742997417, 0.000380553753063459,
-0.00292094257177387, 0.00165193513541848, 0.0621472155259475,
0.000954010377675484, -0.00849953683352181, 0, 0.0104050245659863,
0.00107009876380798, 0.00391440765588459, -0.00237056780214372,
0.0034354446980739, -0.00557367464749349, -0.00261954113394801,
0.000715057169888855, 0.00142877529593013, -0.00780274241192142,
0.00264771613495412, -0.00966192068481085, 0.00157647841879166,
0.0143159910373732, -0.00731279691902325, 0.00180316947518587,
0.00729976680459873, 0.0119704140751333, -0.0183098610936127,
0.00990994871972893, 0.00106873394890668, -0.0384709564336874,
0.006270275172777, 0.0144798372306631, -0.00350941986305591,
0.0200428031123919, 0.0126336633478348, 0.00409814829978884,
-0.00269114196711805, -0.00776288063234142, 0.0251848944512366,
-0.00542638786235339, -0.00638750222759231, -0.00361843528288274,
-0.000233801338877657, 0.00011694880372342, -0.00128726058718338,
0.00362341616070755, 0.000116674055900829, -0.00210204166548149,
0.00478159508238976, 0.00464312431917998, 0.00876287100250005,
0.0123218977348076, 0.00101995077814875, -0.000679821082406791,
-0.00454446847274321, 0.00295622112102081, 0.0171099057542572,
-0.0140491974151797, 0.0200578085708235, -0.000443862065815849,
-0.00111052907635793, 0.0177308694254967, 0.00316052283835867,
-0.000870843095754137, 0.00553897918444317, 0.0185637371657065,
-0.00148948757310026, -0.0126435179273973, 0.0241822405789106,
-0.00792513441694771, -0.026662633055078, -0.0420574758405392,
0.0371424872070333, -0.0190124456853426, -0.0526980086788817,
0.0366113071114783, 0.0107156785578195, 0.00782308189976248,
0.0155368900117701, 0.0201675772262095, -0.00714837163229612,
0.00779565748285155, -0.01335462357558, 0.00261992080178707,
0.0250833369200573, 0.014355274231672, -0.0128680417308169, -0.00457515012926131,
-0.00985970312600966, 0.00215176156751076, 0.00632059760212504,
-0.00342319572233186, 0.00576991112150937, 0.00605446552601996,
0.0220986537636955, 0.00237956337185577, -0.024689954901131,
-0.00594935167947709, 0.00351015845679432, 0.00444957362983464,
-0.0182414879686812, 0.00258040981441113, -0.00700395281350326,
-0.0295187960423049, -0.0294986971130717, 0.07297668442269, -0.0470482132187286,
-0.000894602240740077, 0.0208133332406197, -0.030593678735704,
0.0133537722583279, 0.0287869044796119, 0.000541365121750152,
-0.0235484916508829, 0.00596682925724781, 0.0229794715109096,
-0.0110426151259939, 0.0185510168732124, -0.00535733441063491,
0.011642247093165, 0.0199755152898676, 0.00384392744744338, -0.00342767584397944,
-0.0116164708195488, 0.00367741392456311, -0.0236653371455795,
-0.00873649183424129, 0.0209044919307222, 0.0164144403787166,
-0.0242961473360355, 0.0157016300818546, -0.0158084805633841,
0.00597075821950369, 0.0115205109654499, 0.0110775780632697,
-0.00427020838454821, 0.0117252363228081, 0.00995643986285439,
-0.00214718461076823, 0.00337199701387068, -0.00726899644087648,
0.00257665687361524, -0.0100347605606538, 0.00186975227490389,
0.0127862848028228, -0.00102509492344187, 0.0118272583450698,
-0.00355390531384903, 0.000508496479703965, -0.00356469282755686,
0.00954510704306233, -0.00111230107926868, 0.0195368136374428,
0.0086931003271777, 0.00510159057992079, 0.00293136813386835,
-0.0158335514110117, 0.00740707571247093, -0.00572326409292907,
0.00256962978629528, -0.00455085875541014, 0.00563604607810889,
-0.0128009788882362, 0.00726411480465206, 0, 0.00996409348796012,
-0.00719183428672066, -0.00724384203653823, -0.0203226810139742,
0.00698846209282747, -0.0156650642727563, 0.011112884015082,
-0.000202758063995867, 0.0140975145012278, -0.00964539755413164,
0.00714251798760332, 0.013936139923188, 0.00679766169300971,
0.00264749871439198, -0.00137187670594381, 0.0214394295752503,
0.0118310509690659), PYPL = c(0, 0.0184620628397354, 0.00146236924108236,
0.00945346976136589, -0.00120697656630977, -0.00775949539459829,
0.00485666320265259, 0.00676009334412324, 0.00096192889766934,
-0.00796427542580281, 0.00193652933761496, -0.00193652933761492,
0.0101254241416472, -0.00168047096755979, 0.00813596275729721,
-0.00597587546208727, -0.00504745699713511, -0.0300866523732897,
-0.0102334349909073, -0.00200909158489034, -0.011885283414977,
0.00684502583240118, 0.000252570413049994, 0.00378172633275641,
0.00702104170479174, 0.0212616412017009, -0.00122381604221455,
-0.00614177063204399, 0.0144344035197199, 0.0108710097358283,
0.00598875920902506, 0.00666517574984522, -0.00451797220157314,
0.0111388995183583, -0.000235757225270152, 0.00258978376376401,
0.0126170133132919, -0.0100339426605879, -0.015123138122063,
0.0101860409308651, 0.00798126302458378, 0.00606060137209028,
-0.00372526632735758, 0.000466417929780725, -0.00233422141465939,
0.00303341736550281, 0.00162970586646191, -0.00934809309551385,
-0.0115717907527259, 0.00686476859167967, 0.000235947633646175,
0.0159104893057197, 0.00185524167649167, -0.0121213839845026,
0.0128162074136705, -0.00115834603843466, -0.000231783935197969,
-0.00115993513202701, 0.00162356516593598, 0.00208357514509734,
0.00691403974931194, -0.0120149549342033, -0.0091068696048065,
0.00210896382540119, 0.00397145715315184, 0.0002331973406861,
0.00325806653371251, 0.000232268557206355, -0.008397530299755,
-0.00469587614856294, 0.00141103032100251, 0.0128431191883685,
-0.000231999078241979, 0.00486166028600898, 0.00896660816105572,
0.0027429045768343, 0.010445122092404, 0.00988992113547828, -0.00673249857551726,
0.0598693821932703, 0.0120165666880083, -0.00377913521832387,
0.0312686826570464, -0.000611787524045433, -0.00224650354870088,
0.00794218621340272, -0.00264042001602986, 0.00101642453121532,
0.0115141154585913, -0.00180922751513814, -0.00666202120775162,
0.00806946085490738, 0.0113875968367071, -0.0259598240831578,
0.00122253478959261, 0.00568525389191521, 0.00766443856494305,
0.00720725835308372, 0.0181858737984891, -0.00235338412552255,
0.00509204003791296, -0.0025427495362865, 0.0222725968709022,
0.0180322055760025, 0.00674918197492855, 0.00521803276040481,
-0.00746266516104601, 0.00839158304286067, 0.00997792858694406,
-0.0183695116298046, -0.0234948658009345, -0.00191899884337286,
-0.00307809006753691, 0.00748633861666685, -0.00864311528013517,
0.0208082338258768, -0.00777623643530068, 0.019235007335235,
0.00614466135940412, 0.00850287924627083, -0.0109189569719347,
-0.0189730868611923, 0.0309957415067582, -0.0179951248064838,
0.00466897868131791, -0.0150180975909301, 0.032380983142686,
-0.0210962668130106, 0.00930758749664759, 0.0178133073060895,
-0.00328169856821502, 0.0321652209814254, 0.0235922342425999,
-0.0128630660595967, 0.00732096685664533, 0.0236839110021178,
-0.0103999064637866, 0.0126023809009401, -0.00713321650878759,
-0.00153511330786365, -0.00547765833851456, 0.00905607222144149,
0.0228696862470362, -0.0159199168890433, -0.0110404858175448,
0.013402541898369, -0.00371435513747927, -0.0119109970004533,
0.00750349707004981, 0.0128293116822373, -0.00235094988507599,
0.00318922642435513, -0.0280415693230514, 0.000344666552225726,
0.0174214480022264, 0.0106105921796232, 0.0100017836604793, -0.0167255945022858,
0.00252670900028914, -0.000673123547183481, 0.0239532073524598,
-0.0130699355178891, -0.000832969651102348, -0.000500108366678166,
0.00929463952743957, 0.00789865899938714, 0.0123800573255584,
-0.00145808045273382, -0.00357318882348074, -0.00309621369403008,
-0.000489723289687629, 0.0123337815972159, -0.0141316246458776,
0.0244012753190934, -0.00127792349659722, -0.00834675779473922,
0.00321855757684076, 0.00416864473854421, 0.00669748966132541,
0.0143592841975132, 0.0141558870041015, -0.00170056474856485,
0.00693866022551298, -0.024577407183598, -0.0088580301505327,
0.00617730636083474, 0.00942955266968751, 0.00156297313439604,
0.00233992776867935, 0.000623068551129738, -0.00327535183381125,
0.0278843911161644, 0.0034883328732758, 0.0027215014051108, -0.00287294359931515,
0.0260065172310254, 0.015808321007306, -0.00290862614496894,
-0.0198580619782734, -0.00955373767788597, 0.00881068941601015,
-0.000148717565529973, 0.0538402481454036, -0.0166232138790836,
0.00585668403790334, 0.0114707484871469, 0.00449563858789879,
-0.00393256141356647, 0.00126579019783741, 0.0196234255979964,
-0.00248380155580308, -0.00179764969514018, 0.0156553459385953,
0.018227724366768, -0.00442500213252082, 0.0048262997233955,
-0.00900118957729184, -0.0014855834047082, 0.000540481040190317,
0.00619445839197335, -0.0143322898754238, 0.0565226473000432,
-0.0171343762434906, -0.00485590562979563, 0.022890777241289,
-0.00257495962878505, 0.01280919268495, -0.00933456069744328,
-0.00064248541648135, -0.0601346531766288, 0.0332961748429735,
-0.00569424809366549, -0.0592229080191324, 0.00323550965574558,
0.0268826227657283, 0.00749173287080639, -0.010641273239136,
0.00519832890168865, 0.0044925811184855, 0.00636374773950121,
0.00229195924116165, 0.0185449909905725, -0.0043717628829543,
-0.0109465784034377, -0.00673403218134407, 0.00027019322425269,
-0.00175779916149745, 0.00512957402111909, 0.00429933505253463,
-0.00564666755719276, -0.00744296008271578, 0.00298376732274375,
0.0386528504438922, -0.000260581179597198, 0.0253503120162064,
0.00443748430222871, 0.00176945187189044, 0.00227044742240497,
0.0047762407645139, 0.00985722617454492, -0.00149109123980655,
0.025171726371193, 0.00568287596116685, 0.0107926939307549, 0.00166853811603053,
0.00273495618798877, -0.0059552344408656, 0.00749776816537099,
0.0130751735620171, -0.0134309162407433, -0.00630615462973968,
0.0182145900202838, -0.0845849407365838, -0.023618597741534,
-0.0246583040500145, 0.0129668736686054, -0.00118997835659152,
-0.0442232303031104, 0.0330484961868748, 0.00706526972258773,
-0.00412643513974404, 0.0302176962669526, 0.0131779753323986,
0.00095752089173579, -0.0168569261005177, -0.00716283586007486,
0.0154337413794284, 0.025288913743346, -0.0042757107392795, 0.00138531603387548,
-0.00062938252289629, -0.00974389215587193, 0.00292050232443921,
0.00253257063566024, 0.00202151675875945, 0.00365347178036477,
0.00439182586837372, 0.0104621204559089, -0.00136384624755598,
-0.00497514723270223, 0.0380450885141113, -0.00892214755075856,
-0.00497780412354378, -0.0228989559931776, 0.00694961826706318,
0.00222359570033448, -0.0310802336920518, -0.0273564930515777,
0.0323085571665331, -0.0340107376102604, -0.0165161980311326,
0.0107336218297795, -0.0115332753549928, -0.00588396998300951,
0.0029463126231071, 0.0286053651221246, -0.0409843983349875,
0.017846847025672, 0.021056166375764, -0.00378369553249098, 0.0124709203442269,
-0.00245591805612495, 0.00683572662416443, 0.028260082138884,
0.00386602725909497, -0.00824896586639275, -0.0119942979750522,
-0.00662681147385944, -0.0381830218340478, -0.0171493887892137,
0.0270628928834726, -0.0253077178884603, 0.00645429168726988,
0.00267697926346729, -0.0420424271880723, 0.023695749813927,
0.00732506013100558, 0.00914845635631215, 0.0139639612845809,
0.0168929699741391, 0.0279147439562234, 0.000126207463556482,
-0.00493639395352392, -0.0130269659142621, 0.000385566482602046,
0.0174515859216275, 0.0200031543913518, 0.00567761334046005,
-0.00778402630921787, 0.00568955135016534, 0.00578001960347495,
-0.00726117266689266, -0.00284490261954652, 0.0107189468924269,
0.00574330418136301, 0.0142755872189329, 0.0157314649335775,
0.00565976032110805, 0.00187943198891381, -0.0230304818838061,
0.0127678966709853, -0.00534979595350054, 0.0133800827069971,
0.00293616992676724, 0.0039794528636768, -0.00351045295857999,
0.00630990657034431, -0.0156134067937391, 0.0167775396715457,
0.000232712893473642, -0.00993634898062279, -0.0350283963434255,
0.00158042703152518, -0.0202473573692753, 0.022189141924639,
0.00953242813918886, 0.00741810071748474, -0.00741810071748478,
0.015727704808662, 0.0158333925898904, 0.00336877813600932, -0.000696113713167083,
0.000927989869727821, 0.0328458885724555, -0.0128727955563112
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-385L), .Names = c("GOOG", "MSFT", "PYPL"))
编辑:@Gurmeet Singh指出的缺乏专心。以下作品。
stock_pairs <- stock_returns_daily %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "daily",
type = "log",
col_rename = "returns") %>%
spread(key = symbol, value = returns)
set.seed(1)
stock_pairs <- stock_pairs %>%
select(-date)
results <- combn(names(stock_pairs), 2, function(x){coefficients(lm(stock_pairs[, x]))}, simplify = FALSE)
vars <- combn(names(stock_pairs), 2)
names(results) <- vars[1 , ]
results
答案 0 :(得分:1)
这是解决问题第二部分的一种方法。我使用了您的combn
语句,该语句返回一个列表,然后将其通过管道传递到purrr::map_dfr
中,该语句在一个列表上进行操作,并返回将所有行绑定到一个数据帧中的每组输出。这样可以方便地进行大量回归,然后整理所有模型的结果。
library(tidyverse)
combn(names(stock_pairs), 2, simplify = F) %>%
map_dfr(function(vars) {
lm(stock_pairs[, vars]) %>%
broom::tidy() %>%
mutate(vars = vars)
}) %>%
select(vars, term, estimate)
#> vars term estimate
#> 1 GOOG (Intercept) -5.572112e-05
#> 2 MSFT MSFT 7.871129e-01
#> 3 GOOG (Intercept) 1.259858e-04
#> 4 PYPL PYPL 4.667828e-01
#> 5 MSFT (Intercept) 5.652354e-04
#> 6 PYPL PYPL 4.284295e-01
由reprex package(v0.2.0)于2018-07-15创建。