我的想法是建立一个简单的for
估算OLS
。这是我的数据。
data=structure(c(NA, NA, NA, 0.364282642384808, -3.01119851776463,
-8.66579826331935, -3.65240790618631, -2.25772100857938, -1.32788331062106,
-3.93453431873128, 1.61851595577217, -0.0835810964347417, 2.95763072440725,
6.73459494189829, 6.7746614478806, 10.04066940684, 6.27227040071725,
14.56115309833, 15.0810480320631, 11.5063029741632, 6.80750487347931,
-1.18833072016655, -5.79184160479652, -6.02697184281143, -0.909063160559839,
7.1570192501252, 7.25446483355088, 4.60134038275206, 1.60503179253466,
2.58786679843029, 3.19993398488766, 4.01612468480688, 2.39049742365533,
3.57305840142054, 5.28113597392619, 4.6193383395236, 9.81570376258964,
7.67053263166053, 8.7462971767163, -2.41101583519382, -5.72144301967393,
-3.34962429953772, 2.42469686950135, 6.88605537277944, 2.3304084911685,
1.64473176724599, 2.096376463845, 3.13301905274572, 6.4935608854793,
-1.86209170829409, 0.969247579474029, 0.228136448905346, 7.30576738513059,
5.72526458055773, 6.70822912575443, 5.79347633054322, 9.53916273996298,
7.03489385261224, 8.83071554382293, 6.35608662968355, 7.49570813361414,
10.37816202928, -6.51787198449831, -9.32547734768995, -17.5503959550318,
-5.77635109284904, -12.8389156697705, -7.84899871564035, -7.28868533949256,
-2.06863126318557, -6.80742307126607, -2.55789582062076, 1.99805564554254,
2.93018799594101, -1.95382266713474, -9.38935251294901, -1.80342802394376,
-3.89266430829278, 3.91169092161552, -1.05977724359359, 5.72213942249839,
5.93973186475869, 3.73136750684378, -2.43841298366497, -7.43642386549835,
-7.55480288130688, -8.06420698959456, -9.23742969086161, -5.26098891655298,
-0.360621818814131, 4.27609325342668, -0.202264121414657, -0.739790826736957,
-7.88395645160394, -11.1613743624087, -14.9077940939956, -2.72760839139119,
2.89510238654891, -0.0264076206382313, -6.00045133715453, -3.58206296383663,
9.90965977455895, 9.39892106396294, 8.8500742386703, 1.03004327160894,
-3.39139346236546, -9.45297995984866, -6.71754724732234, 1.07856171317046,
5.86275416364693, 7.06192113166803, 3.54834901654426, 10.4151183234618,
8.42649396178409, 1.37215941378174, -0.0959081870615819, -4.14146697690086,
5.45774132871072, 0.223119681519051, 0, -6.25895663105755, -7.34996687943651,
-2.05362102615002, 2.44037956419783, 0.755596920772753, -5.71923441693524,
-12.6550626632576, -12.0927223707412, -7.1044123544374, -4.41694182999976,
-2.46514623415841, -2.89484145252672, -1.07496393286225, 1.51846109153433,
9.79837511377407, 13.1724396478228, 11.7638714073689, 7.75715439394167,
2.22971449546023, 1.06575344772809, -7.95668538743413, -3.38527597974284,
-4.96296878836012, -6.30387499070715, -2.63245107781987, 0.538790612129311,
7.20385490322102, 0.785855160264248, -4.07327342173986, -9.19404535084919,
-7.85306631326661, -8.40195847478381, 6.76518882540079, 20.6044483431751,
27.3376364755518, 16.1950373814053, 5.05297216901036, 6.27092973657019,
0.26593751035297, -5.50084864000119, -9.32908406147996, 1.06569212227401,
5.53500950831648, 4.83724572553634, -0.201759108367135, -0.215557373390091,
0.106993204250799, 1.24439442613878, -0.823381993220522, -0.154582509452801,
-0.707272103536809, -0.750059996605621, -1.63317292685484, -3.97351005316506,
-1.94637375483417, -1.58806068447527, -1.83603210646961, -5.88746009143946,
-1.98437987405322, 0.667994656613068, 3.32686055849901, -1.07920100252718,
-3.29642432801327, -1.7308231755863, -0.158526372350117, 1.77637675120452,
0.69273231705148, 2.23096071532342, 0.694227095544093, 1.088686144766,
1.38211640569414, 1.65885963402312, 4.6014062772581, -0.116364540323033,
1.77382780449352, 0.305983492555839, -1.13705870323273, -1.73499737648262,
-3.30793271880242, -0.751234198131975, -0.795139830134708, -4.53643385672044,
-7.45247812161643, -8.97687022091052, 1.64834018202804, 3.2191176895484,
4.02245558153151, -1.16766371252042, -0.447267358966251, -1.21963830943122,
-1.15359574347951, 0.963862883776869, 3.25319828757088, -2.39992279960699,
-4.12163514399823, -1.90534640281392, 2.78430274536804, 2.59174694319815,
-2.03619623396279, -2.4324139141488, 0.884541140483925, 0.418249651921705,
2.28537908942337, -2.7344162155999, -1.78212198491062, -2.33222611800354,
1.65554036309926, 2.18366538205943, 1.67446390265718, 2.81681294535392,
2.97820464882219, 2.2858974878044, 0.0432640314403656, 2.35121048901689,
-0.755634498802799, -0.410961681565347, -4.29551925868059, -1.33625737104313,
0.943018059528512, 1.88295709348439, 1.49172550710409, 0.978402254843047,
1.42235310313777, 3.91914606208345, 5.28314584575617, 5.10122307869,
3.77881596936092, 3.07312894929215, 2.29296833894587, 3.24166264400179,
2.12263652776999, -1.1286952595831, -0.74105148659504, -1.55670242261213,
5.2509895915329, 2.12250977552117, 1.80365467630413, 0.798540025499015,
0.378396196333455, -2.33620973303546, -4.26936625532945, -1.95877284189547,
1.77329245651049, 1.01295182388557, 2.88936841049213, 0.88841536326148,
2.03553984999003, -3.1032473376593, -3.44980743173678, -6.74926487149525,
-7.02400101528008, -5.65598277237218, -2.94091069139391, -0.0328178844786748,
-0.496463722957785, -2.4845307388898, -5.10144588653887, -1.17648415795863,
1.91298814782697, 2.76261110928593, -4.08423218492677, -4.57481047338545,
-0.561573878563557, 3.85578309665615, -0.548618645046986, -5.24202364693075,
-8.7712809791652, -7.09500250779816, -7.16354480740695, -7.58114653887925,
-3.08012101944444, 2.62113229598139, 1.32855538393817, 1.50117870753859,
-2.44460010581697, 1.64526730805699, 0.19918882436955, -0.887628423879775,
-5.61830936031906, -5.74638631321538, -2.71493060161974, -0.919722903920501,
1.42866366807392, -1.43341008537605, 5.43387005503757, 5.20560882134319,
8.33395390226683, 1.20868743993362, 1.31279661693898, -0.543345372299548,
3.91216412765538, 2.91417355648912, -0.738587264937856, -1.25145426621802,
-1.18610585344508, -2.2199109797597, -6.58033155080165, -6.12161627978961,
-2.52038211449749, -1.54605765735112, -1.20920457650286, -2.11305387385244,
-3.45676157376053, -4.54493164337923, -2.72383606121055, 0.406820194550235,
-0.502671495145151, -1.9626141135178, -0.934015342005537, -1.90383524994468,
-1.80417785007651, -1.84217313145933, -1.60993688383013, -2.00755436123629,
-1.97336641569503, 2.71333305601532, 4.33635366330812, 3.47841824109761,
-0.0595474412196779, 0.242429178426661, 0.35691662254952, -0.256458823876414,
0.206197904016008, 8.44696514602612, 11.1597494668917, 20.3150607031918,
16.9890510156044, 19.710190662601, 11.47069570089, 7.23973987166013,
2.20634392649682, -2.64193587528628, -12.3953259428904, -14.0201049207465,
-12.1636347846417, -0.94822837810937, 2.76168314604515, 1.41062447277515,
-0.580830991805406, -1.01337546264596, 2.89267978488107, 7.39731744877444,
6.26026588469873, 4.47844206160489, 5.02542152398134, 1.58643809888007,
-2.61797492116417, -5.80411178882365, -5.11336101058475, -1.929330497448,
-1.42146935414056, 2.17846419078666, -0.136264920048447, -4.20605649385517,
-4.17247692180288, -3.9662289073004, -1.17505172121692, -0.118825885906411,
1.42358825527131, 1.04010880759669, 2.79637685036114, 1.92794726910974,
3.52764344622771, 0.557781543671879, 2.42539533998659, -1.55427142047873,
-2.84292704267836, -2.94486862092241, 3.48300151513814, 1.88907437734536,
3.3787428808499, -2.93577841342194, -2.80440522649285, -2.67382007368339,
-1.02210107770819, -0.801747072894507, 1.59509838947793, 5.35153965643959,
6.79852032531655, 2.02014388244766, 0.0455656989308082, -0.110971774395111,
2.35724158282859, -1.84675332657165, -6.17660267814324, -5.70539655477842,
-5.68536468728731, -2.41204393983612, -2.34922406923097, -2.27702196442349,
-0.606792934514587, -2.59802482781321, -0.083785781428098, -2.54674666558995,
-0.0354606938814399, 1.07382686928066, 5.31286997599545, 5.4124109046708,
5.87897956467322, 4.03885001988615, 6.27540154338524, 1.28189951199185,
4.78602432495936, -1.9895793271432, 0.995476382589445, -5.73925473635222,
-1.97679865718356, -0.502644533875646, 3.9638607124968, 1.21516863398258,
2.09794715119042, 2.4782935920304, 8.51704165939852, 7.72301281389278,
2.50611946593408, -3.06050082510716, -4.2457899061869, 8.25131923979131,
9.72149346947972, 10.1390306625881, 1.72894540486061, 8.30912155176332,
5.06096939736216, 5.34930232987264, 0.7154777278366, 0.221294230463509,
-0.263374033482644, 0.671625296333, 1.54362436939559, 1.637706612339,
1.32441994000243, 1.01604845752641, 0.450073977294263, 0.357727708511124,
-0.781802352417116, -0.558416681834029, -0.862358355802773, -0.92420724843334,
-0.521845842083302, -0.987720816524806, -1.20932588988809, -1.18645635624453,
-0.901328907176357, -0.982231567722991, -0.199355302490889, 0.138448308013261,
0.636346180966418, 0.450661643970141, -0.0225548044904933, -0.248266428872879,
-0.394778618956315, -0.271280301737598, -0.226661368767456, -0.63217522993948,
-0.251566171958628, -0.161596187036706, -0.231086025115745, -0.116487898100992,
-0.115777265178085, 0.302560726368295, 0.503284442198426, 0.581463140124949,
0.271813325626891, 0.291569892434995, 0.209464285290695, -0.0116547446634696,
-0.0929998756175054, -0.0929998756175054, 0, -0.0560519563453932,
-0.140034180840565, -0.114341624579706, -0.0583375164133093,
0.0631601445525209, 0.0631601445525209, -0.521485682618894, -0.521485682618894,
-0.545403690196244, 0.0275554663206945, -0.25815444231907, -0.115542628336573,
0.132358246166775, 0.130814931653456, 0.329477413812618, 0.904777891604569,
0.993942080821464, 0.951306667035867, 0.940052106211731, 1.11748817546984,
1.10377102332677, 0.685349270194124, 0.845665072233803, 0.785087546250263,
0.831855887220012, 0.8012052996061, 0.903143527105632, 1.11840475449192,
1.03052017512997, 0.890829574181562, 0.82475429197657, 0.674638508362269,
0.706102261584585, 0.810235447979846, 0.975515717467723, 1.20210022067029,
1.26190020594807, 1.21824510799304, 1.18957861397164, 1.16054980164135,
1.04306257943316, 0.735010266918112, 0.544047190321395, 0.367747438098243,
0.471147816935136, 0.558565961090451, 0.673800728969565, 0.42338404560065,
0.397525748402838, 0.221007642370386, 0.339458602227216, 0.458315469000259,
0.589717489311226, 0.444232978225023, 0.264646594664608, 0, 0.468940112499605,
0.583172150728983, 0.884045705200354, 0.813294888597427, 0.829026844033853,
0.893242431394614, 0.89552066551819, 0.849525978069554, 0.719371374343281,
0.648792850044577, 0.691508701163168, 1.10714577668785, 1.15019617812904,
1.21912685905337, 1.1526733298995, 1.25017983296747, 1.52982915177884,
1.56101035102276, 1.55295688946586, 1.55295688946586, 1.56645097034289,
1.53586612545755, 1.5063453789421, 1.54620787089574, 1.54007825926861,
1.62967544547703, 1.61250182740058, 1.52595289543696, 1.27328906087516,
1.16965992135829, 1.49128327465062, 1.59397543583708, 1.36885523410477,
1.43254359299754, 1.30584614856402, 1.26137750581869, 1.19756862311013,
1.19163260058091, 1.21560551347202, 1.09969479309346, 1.24012360054562,
1.3443687144457, 1.61432482131922, 1.54258425387499, 1.47069283954526,
1.53128436060559, 1.55778678747667, 1.43270877557657, 1.47022272899227,
1.65057725613533, 1.69394488590238, 1.38909505925759, 0.976870941516861,
0.810346372709114, 0.887606913197669, 0.740777796151049, 0.739025614446476,
0.669485168883077, 0.710041667929695, 0.62096549544437, 0.638497995433318,
0.623627706105023, 0.664603323635804, 0.647244150299664, 0.558380676276671,
0.469360170559008, 0.484189932218393, 0.544736150894121, 0.34838810403941,
0.347913634790253, 0.226913836275888, 0.149495022303858, 0.0771035141940102,
0.175635744956804, 0.13952121706069, 0.10795317317871, 0.103016335194894,
0.0167262532817958, 0.0428968334643898, 0.126320826825155, 0.119233510482086,
0.104903298747394, 0.192948089362899, 0.147985149522478, 0.228896842826098,
0.252954267257532, 0.217227450852708, 0.210043833971008, 0.19576397793204,
0.238787463084811, 0.265400849305641, 0.218089271127875, 0.215838045453509,
0.150834884448692, 0.134222718096022, 0.172523311939221, 0.115064314284763,
0.0791038642537156, 0.0623574112023517, 0.100659834166139, 0.1916112050101,
0.217826804639332, 0.267801340100138, 0.217795974670176, 0.232107002492743,
0.172133058527346, 0.179248313581892, 0.203090870136968, 0.265007642680559,
0.336442734461029, 0.381524614784312, 0.395748560076736, 0.383841631360671,
0.423612405429896, 0.440627797675197, 0.453254293742722, 0.453094680381705,
0.448230401731076, 0.424507166836263, 0.426907146120148, 0.476640585958932,
0.498037727701095, 0.476942665732749, 0.482528092387091, 0.470999503657854,
0.352075731595369, 0.240161173961995, 0.197282573396995, 0.0722845401141869,
0.0699075006406646, 0.0674800219235383, 0.0698575963594017, -0.0698077751235759,
-0.0240553435044699, -0.0528991544161528, -0.0527265468221794,
-0.0742517747654103, -0.0430788313489305, 0.00718209121175484,
0.0191339606234964, 0.0239140697626761, -0.0262859693310968,
-0.0549383068898512, -0.143120606208269, -0.147932999582934,
-0.131263214215194, -0.128917504150228, -0.174271424539474, -0.159981530407038,
-0.152899053872435, -0.100503120432105, 0.0672639523241925, 0.144352879855036,
0.120439932284011, 0.23663759150141, 0.292626349553182, 0.336254846059214,
0.343919713218455, 0.351754949649985, 0.460556759198494, 0.484015847681718,
0.462435125264249, 0.528712214420035, 0.54582737300608, 0.516378803893391,
0.523130015801399, 0.523663764846205, 0.555213723499692, 0.550562369001661,
0.536310019680952, 0.53125692529401, 0.516906458536215, 0.54885555461972,
0.625160455330387, 0.647439423732399, 0.642722185908751, 0.572099989362984,
0.572309532363779, 0.577433865176014, 0.574829104329644, 0.61201578539376,
0.578027092344491, 0.607055170248993, 0.631309993890783, 0.682374746574221,
0.694572432932494, 0.711623963488339, 0.743391358088519, 0.760372451745628,
0.791828203288558, 0.808665511872642, 0.824887421286946, 0.789119657006543,
0.800666819103091, 0.761329340072603, 0.702301400571259, 0.65565627540678,
0.587022205406029, 0.554933314094663, 0.496193509312626, 0.493244032977458,
0.459027073945895, 0.400649464751801, 0.364081464600519, 0.276620807224148,
0.213485125927249, 0.155228070943125, 0.12600474375335, 0.0774744403384764,
0.0411331593390019, 0.00241838186072485, -0.038676077753963,
-0.0749214961585754, -0.0941818394463834, -0.120655288927085,
-0.139890142367616, -0.18792686902207, -0.161441523742845, -0.101140632222996,
-0.0698276936663467, -0.0481312703893844, -0.0288746419627062,
-0.0192456456412193, 0.0504748803139776, 0.0432749603021199,
0.0600848726248548, 0.0913127989197404, 0.103257880472096, 0.139184199696207,
0.155965744721209, 0.232057753060966, 0.222945705805788, 0.319472349384209,
0.325856390275758, 0.30325094021737, 0.603365168302283, 0.66373349138964,
0.733382376322766, 0.653902164789777, 0.70224234139129, 0.70922716638433,
0.675627721409811, 0.668626716233645, 0.105881327426416, 0.67189531493923,
0.0632207499327464, 0.22422131555937, 0.158071829703332, 0.0815086649976315,
0.0817077392039289, -0.0272661869347379, 0.0870207950156132,
0.0223601164017095, -0.0049798452207872, -0.00995746219363269,
0.00498170344134241, 0.0149506901012675, 0.0523606640759899,
0.0249103776935433, 0.0298946853636517, 0.109663074088395, 0.0348771339068188,
0.0149395359209992, 0.0721997096986282, -0.0273534496291861,
0.019898603828028, 0.0846511068508082, 0.109540366927763, 0.121992715868702,
0.126953086670589, 0.141793640313059, 0.0971106836251678, 0.142005294694947,
0.121974521063195, 0.114596416325696, 0.0995821336574831, 0.149440241001875,
0.107006916436903, 0.265937473370825, 0.166707092033458, 0.124357173661904,
0.156707672202372, 0.156660991068147, 0.166694668894674, 0.154271959300645,
0.151811979381145, 0.16679411453302, 0.1392800822833, 0.0846069349456524,
0.05729602036042, 0.0947089932558542, 0.0424093888659716, 0.0871961379602881,
0.0872547689970471, 0.0822749237762599, 0.0473527003445939, 0.012451006434322,
0.0448420324184129, 0.0324016327612142, 0.03739208680551, 0.0598251154592999,
0.0598295853053932, 0.0697959759135669, 0.0673158335694257, 0.0673409961989391,
0.0648493075910972, 0.0449023806095683, 0.0698220591213827, 0.0698272784925846,
0.0698272784925846, 0.0723238153494732, 0.072296793528448, 0.0747814074717246,
0.0548601803700421, 0.0871896290742136, 0.0797668400037477, 0.0573559699099668,
0.06983771996667, 0.079820515636797, 0.03739208680551, 0.062264343500118,
0.0622457645066854, 0.0672354603612924, 0.117092019117937, 0.089677417792422,
0.0523020433299548, 0.0398417393170591, 0.0149417652035133, 0.0373223973542558,
0.0149017564893716, 0.0124315341055059, -0.0049731689955923,
-0.047253911599765, -0.0273493764470611, -0.0373057169475999,
0.0124454356524195, -0.10192349400775, -0.0946241917077151, -0.116978510957089,
-0.079618181880492, NA, NA, NA), .Dim = c(444L, 2L), .Dimnames = list(
NULL, c("a", "b")), .Tsp = c(1980, 2016.91666666667, 12), class = c("mts",
"ts", "matrix"))
这是我的代码的一部分。我需要的是,一旦k=1
和i=1
,我将从summary
lm
提取线性系数并将其放入coef.matrix的第一行/列中。
for
的第二部分是k=4
和i=2
我将从p-value
的摘要中提取lm
并将其放入第二行矩阵coef.matrix
的第一列:
coef.matrix=matrix(0,4,16)
for (k in c(1,4)){
for(i in 1:2){
coef.matrix[i,1]=summary(lm(a ~ b,data))$coef[2,k]
}
}
看起来此代码仅考虑k=4
。这句话是对的:k in c(1,4)
??
编辑:如果我这样做,它不起作用:
coef.matrix=matrix(0,4,16)
for (k in c(1,4)){
for(i in 1:2){
coef.matrix[i,k]=summary(lm(a ~ b,data))$coef[2,k]
}
}
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
[1,] -2.387462 0 0 2.069463e-06 0 0 0 0 0 0 0 0 0 0 0 0
[2,] -2.387462 0 0 2.069463e-06 0 0 0 0 0 0 0 0 0 0 0 0
[3,] 0.000000 0 0 0.000000e+00 0 0 0 0 0 0 0 0 0 0 0 0
[4,] 0.000000 0 0 0.000000e+00 0 0 0 0 0 0 0 0 0 0 0 0
我希望coef.matrix[1,1] = -2.387462
和coef.matrix[2,1] = 2.069463e-06
,其余的都应为零。
答案 0 :(得分:0)
coef.matrix[i,1]=summary(lm(a ~ b,data))$coef[2,k]
将1和4的值存储到coef.matrix的第一列。也许你想要:
coef.matrix[i,k]=summary(lm(a ~ b,data))$coef[2,k]
鉴于您的编辑,问题在于内部' for'环。试试这个:
i <- 1
for (k in c(1,4)){
coef.matrix[i,1]=summary(lm(a ~ b,data))$coef[2,k]
i <- i + 1
}