Построение кривых из столбцов фрейма данных

#r #plot

#r #построение

Вопрос:

я столкнулся с проблемой при построении оценок ols на диаграмме рассеяния:

У меня есть этот фрейм данных: с 9 столбцами и 99 строками:

 structure(list(Y = c(-0.145442175, 0.291096141, 0.489923112, 
-2.038363166, 1.180430664, 0.188114666, 0.850922634, 1.172142766, 
-3.980837975, 0.285762444, 2.497040646, 0.658010994, -0.925171981, 
0.37076995, -1.108211119, -0.409242669, -1.234583525, -0.385841816, 
0.016744771, -0.584406288, 1.17224811, -0.746804388, -0.625028046, 
0.257871468, -2.735845346, 2.619304857, -0.406825232, 0.323665151, 
2.218951363, -0.821029648, -0.872854889, -2.663306158, -0.121976044, 
0.881566376, -1.972706678, -3.855576256, 2.927421113, 1.314753531, 
0.234296206, 0.828464757, -0.909318569, 0.616134903, -0.567630403, 
0.624571064, -0.414112923, 0.642200314, -0.309421266, 0.195312598, 
-0.519988256, 0, 0.081070175, 0.032446432, -0.534025032, -0.426783307, 
-0.38495511, -0.207900219, -1.953789746, -0.616924355, -0.783222881, 
-1.935420969, 0.638445535, 1.080925923, -1.598076681, 0.25063631, 
-0.697183766, 0.188971653, -0.415267389, -4.154506044, 1.163226552, 
0.036569698, -0.547147074, 1.11937374, 0.383311682, -0.875037781, 
-0.372684863, 0.306816004, -1.250561544, -1.042237738, -1.757788446, 
0.021079982, 1.844023775, 1.674645753, -0.428546132, -0.527705597, 
0.542202572, -0.621479123, -0.050415867, -0.122332943, 0.468553764, 
0.216998274, 3.088480781, 0.434099931, 2.114916704, -2.407018936, 
-0.127060127, 0.546756422, 0.263207486, 0.63453915, 0.76832746
), X = c(0.009476137, -0.0236354, 0.0094081, 0.11715252, 0.032324021, 
0.0461193, 0.050794971, 0.032372819, 0.202121874, 0.390821859, 
-0.124492596, -0.127305193, -0.22233597, -0.081113713, 0.09952616, 
0.22494711, 0.226621495, 0.411607624, 0.089200478, -0.013454832, 
-0.013547165, -0.232366214, 0.03140992, -0.026798837, -0.084556341, 
-0.091993172, -0.303730207, -0.236679148, -0.284235285, -0.355253166, 
-0.179645537, -0.01381843, -0.022950244, -0.050065976, -0.032018504, 
-0.087168055, -0.081865767, -0.253991077, -0.242882759, -0.150225053, 
-0.16596575, -0.156887247, -0.071795146, -0.100408802, -0.067307731, 
0.024006869, -0.019250912, -0.02399429, 0.038421097, 0.062320065, 
0.07187025, 0.024019462, 0.038421097, 0.033539309, 0.014351457, 
-0.009575137, 0.014343968, 0.028561284, 0.0404213, 0.026065697, 
-0.004700435, -0.072739794, -0.042217496, -0.05889531, -0.130522139, 
-0.136291869, -0.120099035, -0.091418565, -0.122040844, -0.124609029, 
-0.096255449, -0.190338762, -0.11611752, -0.055598423, -0.065293448, 
-0.038746326, -0.029090518, -0.067627348, -0.082097445, -0.215845836, 
-0.389993696, -0.264371785, -0.126530291, -0.111840985, -0.094952196, 
-0.136700196, -0.190968195, -0.156564122, -0.181077278, -0.15381292, 
-0.122020692, -0.107867301, -0.068642333, -0.034348677, -0.073289926, 
-0.063314884, -0.092537576, -0.165375956, -0.15042398), Null = c(-0.036795117836493, 
0.0120555676565338, -0.0366906491623935, -0.22323992930528, -0.0728300398338213, 
-0.0955073599141197, -0.103350601084975, -0.0729090354522075, 
-0.400153521158964, -0.887015257107641, 0.1362666683468, 0.13919994231771, 
0.221388292373518, 0.087380368104602, -0.189831042487278, -0.452154909992189, 
-0.456044210600938, -0.948567833126862, -0.170785020294756, -0.00253939338337472, 
-0.00240533038312774, 0.228145471304061, -0.0713518661553421, 
0.0165138860659871, 0.0915102566139487, 0.100284493544177, 0.265652059802101, 
0.230938443729295, 0.257246215885006, 0.281209408151878, 0.188533028671265, 
-0.00201164134414489, 0.0110851592192505, 0.0481858583559124, 
0.0237904823161768, 0.094614581053392, 0.0882862377341187, 0.241468070168396, 
0.234837060900023, 0.162029971029324, 0.176601607696189, 0.168307425791361, 
0.0759851164110966, 0.109970788582389, 0.0703849242291975, -0.059492586621119, 
0.00581616568295407, 0.0125631925046972, -0.0827672867080164, 
-0.123023227393077, -0.139691063870559, -0.0595125909296922, 
-0.0827672867080164, -0.074799966578053, -0.044324863847201, 
-0.00820062690976645, -0.0443132308515717, -0.0667648997869916, 
-0.0860567642206439, -0.0627706942069095, -0.0153914247452083, 
0.0771546773236518, 0.0377224646820258, 0.0596889425617937, 0.1425196179012, 
0.148379247725525, 0.13162698340227, 0.0996137276510431, 0.133686233062275, 
0.136388667637584, 0.105222539655097, 0.197385328960716, 0.127361748973716, 
0.0554268640818151, 0.0678473149754353, 0.0330232883757411, 0.0197208677278167, 
0.0707862239701058, 0.0885648870712001, 0.216820906265572, 0.286245951224793, 
0.247258814186372, 0.138394666330137, 0.122716205945161, 0.103719679674083, 
0.148789344619283, 0.197893429730301, 0.168006688568371, 0.189742414352596, 
0.165430712615822, 0.133664933948451, 0.11833998959919, 0.0720581343490991, 
0.0270069004188009, 0.077834296346802, 0.0653403280475977, 0.100918894574441, 
0.176071877748707, 0.162219750035618), OLS_1 = c(-2.97674658085357, 
-2.95792547866683, -2.97674412477729, -2.7937460366665, -2.96913739819288, 
-2.95639989365184, -2.95069150171007, -2.96910314906723, -2.3856485268894, 
-0.647452287114872, -2.68293610049662, -2.670570393744, -2.10297963546522, 
-2.84137496711892, -2.84927190111917, -2.23638642750757, -2.22477621905134, 
-0.385841816000001, -2.87715002139054, -2.96747293407547, -2.96740133507642, 
-2.02609643038743, -2.9697648045679, -2.95427875550959, -2.8310157181346, 
-2.80733412921436, -1.38551048535346, -1.99204069101103, -1.57679230211392, 
-0.821029648, -2.39395151432173, -2.96718943992586, -2.95867282134313, 
-2.9175506236826, -2.94755679517459, -2.82290206987746, -2.83914454134393, 
-1.84931168689084, -1.94200482386918, -2.56030139156351, -2.4747687889082, 
-2.52507434784403, -2.86749990988846, -2.77838660436577, -2.87908253396987, 
-2.97385415360498, -2.96244666805069, -2.95752797222193, -2.96426392038595, 
-2.93361303993881, -2.91621877029975, -2.97384869333029, -2.96426392038595, 
-2.96826157356433, -2.97653443074828, -2.97023260580068, -2.97653534550966, 
-2.9715473503959, -2.96240424133875, -2.97289412424858, -2.9730125951007, 
-2.86497897723402, -2.93188917574701, -2.89904800305061, -2.6561144854951, 
-2.62935195635151, -2.70174255054932, -2.80922741244202, -2.69350740105694, 
-2.68242924921473, -2.79295820376613, -2.32657978700299, -2.718248099245, 
-2.90625073580661, -2.88407071600265, -2.93759776247538, -2.95143559806685, 
-2.87827902655775, -2.83845377816351, -2.15100018436527, -0.392139380784325, 
-1.7590965971582, -2.67400272569948, -2.73540774982849, -2.79741598960129, 
-2.62741730304073, -2.322499279269, -2.52681590220219, -2.38514457172383, 
-2.541507865502, -2.6935934995898, -2.75082409521646, -2.87570553083222, 
-2.94427256930162, -2.86349763526591, -2.88884317216564, -2.80553055841713, 
-2.47811758528604, -2.55927025907886), OLS_2 = c(-2.83865555876367, 
-2.82203271957637, -2.83865550287755, -2.66277932892391, -2.83073328950317, 
-2.8182826854432, -2.81275284604234, -2.83069942358793, -2.27571536741022, 
-0.632851535784811, -2.56646067709365, -2.55491098827374, -2.02364579120999, 
-2.71420058960775, -2.71564453925406, -2.13442002502496, -2.12343285482248, 
-0.385841816, -2.74223576659719, -2.83068449367348, -2.83062014186059, 
-1.95158880862936, -2.83135434505306, -2.81870405841395, -2.70456098525177, 
-2.68251016192609, -1.35080974869909, -1.91966655284606, -1.53026524143009, 
-0.821029648, -2.29619548286091, -2.83042962848176, -2.82271365766308, 
-2.78489427206998, -2.81254809712918, -2.69700817487578, -2.71212546804251, 
-1.78585373408616, -1.87276085874404, -2.45184700668681, -2.37183555552258, 
-2.41889982491589, -2.73848954857785, -2.65553364194069, -2.74924637290594, 
-2.8354502300085, -2.82614423798244, -2.82167034953476, -2.82594242161564, 
-2.7962902949221, -2.77959589724382, -2.83544467118397, -2.82594242161564, 
-2.82986834510621, -2.83829410413293, -2.83315419155684, -2.83829521382395, 
-2.83312719078141, -2.82412509152621, -2.83447802392599, -2.83561001727694, 
-2.73614728712302, -2.79813447119318, -2.76776591170989, -2.54140667394362, 
-2.5163996858597, -2.58402223424852, -2.68427373122372, -2.57633280462435, 
-2.56598731123967, -2.66911582708562, -2.23311605677819, -2.59943103595799, 
-2.7744383205277, -2.75387620457868, -2.80339428073398, -2.81610308322424, 
-2.74850042856033, -2.71148276169435, -2.06864445166113, -0.418358709691658, 
-1.7012556906544, -2.558117011201, -2.61544592452239, -2.67326984561107, 
-2.5145916492569, -2.22929491666958, -2.42052887445801, -2.28795076147412, 
-2.43427089501948, -2.57641320261571, -2.62982944259216, -2.74611100908034, 
-2.80953310903525, -2.73477077084888, -2.75830410348864, -2.68083005992821, 
-2.37496906485549, -2.4508827380889), OLS_3 = c(-2.58083646581942, 
-2.5683178338716, -2.58084089114316, -2.41826149362172, -2.57232965672457, 
-2.56041470241702, -2.55521822468909, -2.57229650627193, -2.0704676472292, 
-0.605591599496051, -2.34899840070827, -2.33897223601076, -1.87552769159633, 
-2.47676312148376, -2.46615920192222, -1.94404642215785, -1.9342224786085, 
-0.385841816000001, -2.49034777076914, -2.57529735049815, -2.57524652934739, 
-1.81248137667339, -2.57293885513887, -2.56558300171966, -2.46846711008925, 
-2.44946096338359, -1.28602268062379, -1.78454238349805, -1.4433981562183, 
-0.821029648, -2.11368273887782, -2.57509593622485, -2.56887479307252, 
-2.53722183306237, -2.56048377359198, -2.46196139684977, -2.47497795642607, 
-1.66737628649693, -1.7434807939705, -2.24936019247138, -2.17965685727221, 
-2.22066956504207, -2.49762425675709, -2.42616435450559, -2.50683929408026, 
-2.57704694280319, -2.57166448720316, -2.56802106429762, -2.56769302344379, 
-2.53990559282486, -2.52451787208599, -2.57704119998386, -2.56769302344379, 
-2.57148502596854, -2.58019625622877, -2.57722566059429, -2.58019772985789, 
-2.57469359055957, -2.56595475982599, -2.57605200249485, -2.57907626550515, 
-2.49561557851369, -2.54841138215235, -2.52265924802504, -2.32724456926626, 
-2.30551521644622, -2.36423571438323, -2.45098235381054, -2.35756515622, 
-2.3485875529132, -2.43789928063234, -2.05861713726078, -2.37759686441414, 
-2.52834152993493, -2.51080007744427, -2.55283331443161, -2.56343418632904, 
-2.50620082129485, -2.47442497328161, -1.91488441727801, -0.467310795744689, 
-1.59326539683083, -2.34175573481226, -2.39147445613669, -2.44148615865099, 
-2.30394357612981, -2.05528024243402, -2.22208856552246, -2.10648769733616, 
-2.23405702128991, -2.35763491117015, -2.40392966200837, -2.50415507637054, 
-2.55797145858227, -2.49443477420494, -2.51458468009137, -2.44801138045477, 
-2.18238842077399, -2.24852076027753), OLS_4 = c(-2.4289478285331, 
-2.41681903415288, -2.42895104301202, -2.27867081965274, -2.4213161496905, 
-2.41038194422522, -2.40559515788832, -2.42128586809391, -1.95522949388955, 
-0.590647453749078, -2.21077815389366, -2.20138321248198, -1.76758669368012, 
-2.33060054299992, -2.32313500877883, -1.83755181381677, -1.82840597739465, 
-0.385841816, -2.34557046847711, -2.42346978407977, -2.42342111188123, 
-1.70861264386732, -2.42187239429871, -2.41422413566286, -2.32281181955877, 
-2.30497392699143, -1.21632553238408, -1.68248005204524, -1.36346128591018, 
-0.781669317752002, -1.99042352676657, -2.42327691796255, -2.41734804581689, 
-2.38744248609079, -2.40939495374384, -2.31670510436427, -2.32892438647688, 
-1.57289978140148, -1.64407512538075, -2.11744278294415, -2.05217911016675, 
-2.09057710272701, -2.35019495754122, -2.28311871426765, -2.35885543710246, 
-2.42560672084754, -2.42000135641999, -2.4165372393818, -2.41707097497419, 
-2.39145946177805, -2.3772271125231, -2.42560153071694, -2.41707097497419, 
-2.420544236609, -2.42841594588832, -2.4253216199613, -2.42841722040367, 
-2.42347169955882, -2.41547562547196, -2.42470587973943, -2.4271143253132, 
-2.34830761179908, -2.39799094116799, -2.37373288731684, -2.19039487337143, 
-2.17003793409615, -2.22505776553193, -2.30640152341961, -2.21880622042115, 
-2.21039315621698, -2.294126428977, -1.93888869626962, -2.23758086989921, 
-2.37908034073483, -2.36257901260101, -2.4021644964107, -2.41218787827608, 
-2.35825527028976, -2.3284051877118, -1.80440438182757, -0.451087514089169, 
-1.50359480720157, -2.20399138892979, -2.25058992243427, -2.29749148286179, 
-2.16856567860513, -1.93576601076367, -2.09190575790345, -1.98368936201681, 
-2.10311255036163, -2.21887159162467, -2.26226743315126, -2.35633238493592, 
-2.407018936, -2.34719820268328, -2.36613768370737, -2.30361375259329, 
-2.05473632620086, -2.11665669129059), OLS_5 = c(-2.2911912568638, 
-2.28123967681215, -2.29119683586224, -2.14805590207021, -2.28325670505768, 
-2.27261386268403, -2.268006850245, -2.28322682471889, -1.84560662105751, 
-0.576090713535621, -2.0945064647732, -2.0859234999636, -1.68828464788266, 
-2.20368547406672, -2.18986194988925, -1.73587625378362, -1.72735189094969, 
-0.385841816, -2.21101101562234, -2.28700200417098, -2.28696051646281, 
-1.63411554699496, -2.28380625780731, -2.27896044246845, -2.19661244829441, 
-2.18039652225164, -1.18146437845759, -1.6101070827248, -1.31682391434599, 
-0.781364138557704, -1.89278224977018, -2.28683751979873, -2.28170279433502, 
-2.25507742887343, -2.27469315563211, -2.19106352335337, -2.20216376634672, 
-1.50940418054145, -1.57481865165838, -2.00915316980509, -1.94938461398854, 
-1.98455653642811, -2.22145418758665, -2.1605019074557, -2.22929361026136, 
-2.28754415075922, -2.28401553991566, -2.28099274980288, -2.2790962708342, 
-2.25448944582185, -2.24095826297856, -2.28753886744317, -2.2790962708342, 
-2.28249611688223, -2.29051254450738, -2.28856644852124, -2.29051401027405, 
-2.28539476406181, -2.27754391464367, -2.28663199585719, -2.29003809100396, 
-2.21974449731936, -2.26453458741267, -2.24273365564754, -2.0758820670505, 
-2.05727208640147, -2.1075473897372, -2.18169509790509, -2.10183883759691, 
-2.0941547862828, -2.17052540371127, -1.84552009498619, -2.11897925887711, 
-2.2475539961602, -2.23266092019845, -2.26826235385205, -2.27716458812284, 
-2.22875067319638, -2.20169236013157, -1.72209385129724, -0.476893951190187, 
-1.44569200778405, -2.08830648613957, -2.13084935049209, -2.17358829667077, 
-2.05592583172644, -1.84265559786228, -1.98577321824112, -1.88660772323127, 
-1.99603456442263, -2.10189853669134, -2.14149931364702, -2.22701080101746, 
-2.27258448425562, -2.21873931960315, -2.23587705524471, -2.17915915787995, 
-1.95172754860073, -2.00843362344438), OLS_6 = c(-2.14615029819501, 
-2.1274826763545, -2.14613692884822, -2.038363166, -2.14482079785526, 
-2.13839956793073, -2.1352633011825, -2.14480554064275, -1.77137087834078, 
-0.604458131512312, -1.92044345866761, -1.91142894340333, -1.5035051350835, 
-2.03720410348948, -2.07364942604987, -1.67230210256299, -1.66457879312031, 
-0.427523081653794, -2.09111249534671, -2.1358169999572, -2.13575175544593, 
-1.44873737433719, -2.14509683128765, -2.12442374236989, -2.02946586195686, 
-2.01185030632841, -0.994510606111227, -1.42450007218492, -1.12983335353955, 
-0.596198212559954, -1.7115906309286, -2.13555900800151, -2.12811588444992, 
-2.09509015766854, -2.11889016916752, -2.02341958358771, -2.03553614239934, 
-1.32305159796573, -1.38891263096519, -1.83141440901763, -1.76969899713653, 
-1.80596583024281, -2.05682837956465, -1.99043348930533, -2.06558998487816, 
-2.14664801486533, -2.13135448546891, -2.1271468279034, -2.14250449627423, 
-2.12545741758249, -2.11509475538252, -2.1466464083569, -2.14250449627423, 
-2.14442504670383, -2.14684049810003, -2.13838680613343, -2.1468398441846, 
-2.14583791450693, -2.14156460788614, -2.14633891192499, -2.14114136796206, 
-2.05492693125432, -2.10632999229998, -2.08080895764288, -1.90090288083161, 
-1.88144765246016, -1.93417313784094, -2.01325520240903, -1.9281579797124, 
-1.92007377040746, -2.00120005643771, -1.66327168223962, -1.94624457366434, 
-2.08634430885867, -2.06937728900294, -2.11086434271986, -2.12206882778948, 
-2.06498074945642, -2.03501978622706, -1.5377512452434, -0.29431817292714, 
-1.25902518147068, -1.91393009686737, -1.95881793980313, -2.00449939786682, 
-1.88004274517372, -1.66034827254381, -1.80722288608151, -1.70526848086161, 
-1.81783189921089, -1.92822084254891, -1.97013652098612, -2.0630309651189, 
-2.1162243283256, -2.0538104595074, -2.07300962091288, -2.0105124912345, 
-1.7721107506457, -1.83066883021211)), .Names = c("Y", "X", "Null", 
"OLS_1", "OLS_2", "OLS_3", "OLS_4", "OLS_5", "OLS_6"), row.names = c(NA, 
99L), class = "data.frame")
  

Мой точечный график будет состоять из первого столбца (Y) и второго столбца (X).

Третий столбец я не буду использовать.

В четвертом столбце приведены кривые, которые соответствуют значениям регрессий OLS.

Как мне включить их с помощью plot функции?

я делаю это, но это не работает

 plot(data[,2],data[,1])
for(i in 4:9){
lines(data[,i])
}
  

Что я делаю не так?

Ответ №1:

В принципе, вы хотите

 data <- data[order(data$X), ]    ## reordering so that `X` is increasing
plot(data$X, data$Y)
for (i in 4:9) {
  lines(data$X, data[,i], col = i)    ## remember to set `x-coordinates`
  }
legend("topright", legend = names(data)[4:9], col = 4:9, lty = 1)  ## add legend
  

введите описание изображения здесь