Shade Area using Polygon in R - r

This is my data:
data=structure(c(-0.0471957808923093, -0.0467988635384236, -0.0463342530558707,
-0.0457926330882236, -0.0451638382532273, -0.0444368569264807,
-0.0435998514313549, -0.0426401983500639, -0.0415445516030975,
-0.0402989307802484, -0.0388888369350307, -0.0372993976665075,
-0.0355155428026053, -0.0335222113649458, -0.0313045897396848,
-0.028848380109615, -0.026140097233332, -0.023167390606974, -0.0199193879382963,
-0.0163870547327459, -0.0125635636730607, -0.00844466640840535,
-0.00402905939999997, 0.000681265356828354, 0.00568069455370951,
0.0109597029612447, 0.01650460835736, 0.0222973821907433, 0.0283155256820092,
0.0345320202245192, 0.0409153597579977, 0.0474296712607441, 0.0540349276635842,
0.0606872553679083, 0.0673393362019039, 0.0739409011365642, 0.0804393104801724,
0.0867802126590783, 0.0929082711616744, 0.0987679468617816, 0.104304320836165,
0.109463941032544, 0.114195674804447, 0.11845154847061, 0.122187554726899,
0.125364408967776, 0.127948236372159, 0.12991117296429, 0.131231865742443,
0.131895859325063, 0.131895859325063, 0.131231865742443, 0.12991117296429,
0.127948236372159, 0.125364408967776, 0.122187554726899, 0.11845154847061,
0.114195674804447, 0.109463941032544, 0.104304320836165, 0.0987679468617816,
0.0929082711616744, 0.0867802126590783, 0.0804393104801724, 0.0739409011365642,
0.0673393362019039, 0.0606872553679083, 0.0540349276635841, 0.0474296712607441,
0.0409153597579976, 0.0345320202245192, 0.0283155256820092, 0.0222973821907433,
0.01650460835736, 0.0109597029612447, 0.00568069455370951, 0.000681265356828344,
-0.00402905939999997, -0.00844466640840537, -0.0125635636730607,
-0.0163870547327459, -0.0199193879382963, -0.023167390606974,
-0.026140097233332, -0.028848380109615, -0.0313045897396848,
-0.0335222113649458, -0.0355155428026053, -0.0372993976665075,
-0.0388888369350306, -0.0402989307802484, -0.0415445516030976,
-0.0426401983500639, -0.0435998514313549, -0.0444368569264807,
-0.0451638382532273, -0.0457926330882236, -0.0463342530558707,
-0.0467988635384236, -0.0471957808923093, -169.075778315231,
-165.419424999945, -161.763071684659, -158.106718369373, -154.450365054087,
-150.794011738801, -147.137658423515, -143.481305108229, -139.824951792942,
-136.168598477656, -132.51224516237, -128.855891847084, -125.199538531798,
-121.543185216512, -117.886831901226, -114.23047858594, -110.574125270654,
-106.917771955368, -103.261418640081, -99.6050653247953, -95.9487120095092,
-92.2923586942231, -88.636005378937, -84.9796520636509, -81.3232987483648,
-77.6669454330787, -74.0105921177926, -70.3542388025065, -66.6978854872204,
-63.0415321719343, -59.3851788566482, -55.7288255413621, -52.072472226076,
-48.4161189107899, -44.7597655955038, -41.1034122802177, -37.4470589649316,
-33.7907056496455, -30.1343523343594, -26.4779990190733, -22.8216457037872,
-19.1652923885011, -15.508939073215, -11.8525857579289, -8.19623244264278,
-4.53987912735666, -0.88352581207058, 2.77282750321554, 6.42918081850162,
10.0855341337877, 13.7418874490738, 17.3982407643599, 21.054594079646,
24.7109473949321, 28.3673007102183, 32.0236540255043, 35.6800073407905,
39.3363606560765, 42.9927139713627, 46.6490672866487, 50.3054206019349,
53.9617739172209, 57.6181272325071, 61.2744805477931, 64.9308338630793,
68.5871871783654, 72.2435404936515, 75.8998938089376, 79.5562471242237,
83.2126004395098, 86.8689537547959, 90.525307070082, 94.1816603853681,
97.8380137006542, 101.49436701594, 105.150720331226, 108.807073646513,
112.463426961799, 116.119780277085, 119.776133592371, 123.432486907657,
127.088840222943, 130.745193538229, 134.401546853515, 138.057900168801,
141.714253484087, 145.370606799374, 149.02696011466, 152.683313429946,
156.339666745232, 159.996020060518, 163.652373375804, 167.30872669109,
170.965080006376, 174.621433321662, 178.277786636948, 181.934139952235,
185.590493267521, 189.246846582807, 192.903199898093), .Dim = c(100L,
2L), .Dimnames = list(NULL, c("Y", "X")))
With this data I can plot this graph below:
plot(data[,1],data[,2],type="l")
abline(h=quantile(data[,2],0.75))
abline(h=quantile(data[,2],0.25))
Using polygon function how can I shade the area (with different colors) between the distribution tails and 0.75/0.25 quantiles lines?

An answer:
Let dat be your data frame,
oo <- dat[24:76, ];
oo[1, ] <- c(dat[1,1], dat[25,2]);
oo[, nrow(oo)] <- c(dat[100,1], dat[75,2]);
plot(dat, type = "l"); polygon(oo, col = "gray")

Related

Plot VAR fitted values with original data R

Suppose you have a matrix of time series, where each column is a time series.
Now, suppose you take second differences of this matrix, (so the data is stationary), using this code:
var_train_data <- diff(var_train, differences = 2)
and then you estimate this model:
var_1 <- VAR(y=var_train_data, lag=9, type="const")
Recall that this function VARcomes from vars package.
How can you plot the original data and the fitted(var_1) data? Is there a function to return the fitted data from the VAR to "level"?
I want to do this but with a VAR.
Thanks in advance!!
Here is the data:
> dput(var_train)
structure(c(8.59225428063812, 8.61632381521795, 8.48941975098913,
8.42616336163893, 8.4656261753598, 8.49981580266834, 8.50658473928686,
8.54846279771184, 8.58669260125764, 8.56316032696837, 8.52323490346238,
8.48186742854092, 8.48127581622944, 8.49729119076164, 8.49055049683316,
8.58344453055748, 8.66539149708416, 7.9357662604667, 6.05291235942189,
6.03890804924141, 5.98225560802266, 5.97240914057282, 5.94306180787981,
6.20137810965131, 6.12654161955374, 5.94624694111468, 5.85594359899009,
5.80529348520438, 5.8381279297813, 5.94886540621632, 5.89095336551159,
5.78449385503938, 5.80588769759335, 5.70948049577085, 5.67528501375454,
5.52325929980363, 5.31085745753021, 5.33500450854624, 5.3938341365814,
5.45958551414416, 5.62663959880732, 5.67152161243798, 5.87292229474808,
6.13320939934395, 6.0539450308861, 5.91620206260743, 5.98431404307914,
5.97698522584315, 6.09421451539306, 6.16657264938364, 6.27079937807193,
6.32011405804798, 6.31492735684061, 6.33107810311843, 6.44029387009958,
6.5049295418109, 6.68300329197134, 7.30512711870751, 7.48990882366175,
7.51140038002074, 7.36108949229449, 7.40527167395002, 7.52991897172882,
7.48794703789686, 7.28420343801981, 7.03762657251759, 6.94570770568258,
6.8027650651245, 6.70994292841968, 6.5635196517431, 6.58704241900519,
6.57918807870411, 6.56763428848419, 6.67249919251384, 6.55714884087979,
6.44556863637826, 6.64144323226655, 6.69044694236696, 6.64031146877134,
6.57024614132845, 6.55644009549116, 6.41999492814714, 6.30406385390409,
6.25618617067998, 6.20859002609663, 6.29478035236011, 6.33882450179958,
6.27861522839648, 6.36442286111981, 6.40470239932559, 6.38864543562845,
6.57792182635296, 6.77748387294482, 6.80771392480191, 6.78808230061387,
6.83179944673993, 6.74635337244069, 6.71228769051817, 6.71323231410622,
6.87445699358062, 6.99334898488415, 7.02875008260248, 6.99559171234453,
6.94609768011091, 6.82185333419185, 6.78972221207055, 7.08994524817146,
6.94355669144552, 6.96382050291509, 7.01613754035382, 7.0956860745406,
7.10902496856521, 7.0683268590798, 7.09328003338984, 7.03698767140573,
6.97311732456672, 6.96941465936886, 6.83249279448159, 6.71302370211151,
6.68918458951307, 6.83687286833789, 6.87816384462923, 6.779867793454,
6.6690749885188, 6.68210859744981, 6.63725803128446, 6.47906806746391,
6.63432043737224, 6.57746390143601, 6.63242090143254, 6.51253166492618,
6.61381121689155, 6.59605370068614, 6.52834257739573, 6.38542432810392,
6.40417606373692, 6.39057618124397, 6.38557758480921, 6.40206794670863,
6.38004182339231, 6.33048455580799, 6.31905391015162, 6.18631691346691,
6.16989597138095, 6.18599200958145, 6.15475193126883, 6.10944958256305,
6.07359299021025, 6.30235729839158, 6.21488078851159, 6.19123711209895,
6.13415226194445, 6.10489951928334, 6.10835829857948, 6.17117854049486,
6.20272828827286, 6.14697144970638, 6.11798126273311, 6.09592248164895,
6.04625173074978, 5.99916211737789, 6.01626801829834, 6.07798622087923,
6.06003704039333, 5.96216446139643, 5.8926960131323, 5.91466063648335,
5.87549237085556, 5.89885310338971, 5.9930422492496, 6.02137002988689,
6.01266675475859, 6.15176510875538, 6.27125844671076, 6.34813949104671,
6.50500387674558, 6.50357881390098, 6.4894121494231, 6.48577961874698,
6.65109576407555, 6.55243995455944, 6.52108609966874, 6.61791214295492,
6.73192688164199, 6.82964630649759, 6.7830420348085, 6.67163010253559,
7.35778808496937, 7.68222895818785, 7.65345178297661, 7.75628546245401,
7.62338589734873, 18.0052565534027, 18.0283161481762, 18.0354203734597,
18.071119409619, 18.1057986145642, 18.1434464627582, 18.1679613320011,
18.1724822603554, 18.1626507380013, 18.1735086813587, 18.2048932826437,
18.2692972785199, 18.2533671856288, 18.2415818049433, 18.2496122176447,
18.2718727763864, 18.3010161442641, 18.3291390076371, 18.3572538287519,
18.3507204046193, 18.3600370069532, 18.3939643481435, 18.4165259145903,
18.4908471052973, 18.4867143437776, 18.4804562806922, 18.469238614477,
18.4879629103507, 18.5207111296724, 18.5392075918007, 18.549601058126,
18.5389132074898, 18.5541926126363, 18.5648519425587, 18.6134248798087,
18.6623309458927, 18.6585086859232, 18.6627389755133, 18.6664151651325,
18.7001141293336, 18.7124583930686, 18.7555889295117, 18.7668588119494,
18.7436675451662, 18.7459556596228, 18.7514145615817, 18.7664618044321,
18.8918838160788, 18.8524507901715, 18.8267408416858, 18.8351746759483,
18.8659203733722, 18.8520280137673, 18.8947341990718, 18.8888147953761,
18.8994685106982, 18.9232996525635, 18.8866047503417, 18.9024896904205,
19.0233652386019, 18.987660272875, 18.9544232700339, 18.9334369249929,
18.9449183299094, 18.9631673064947, 18.9933965789268, 18.9872237290741,
18.9873356136058, 19.0123636641398, 19.0206652835947, 19.0270219591515,
19.1598215152463, 19.1409817254047, 19.1376727583276, 19.1107669946426,
19.1241264422661, 19.1370331827589, 19.1912655465102, 19.2115823692113,
19.2158835209359, 19.2414814594526, 19.2701126456528, 19.2843375695361,
19.457274554838, 19.3897327048706, 19.3892248817594, 19.4122218249334,
19.4394604950819, 19.4541631046363, 19.4997359433774, 19.5176831040473,
19.5230181490842, 19.5320146650821, 19.5251016863462, 19.5528663129766,
19.7183358456188, 19.6474484090812, 19.6534432674726, 19.6576920553381,
19.6789279209334, 19.7218625314866, 19.7970056245357, 19.8212720524189,
19.8242227530495, 19.8192388840771, 19.8436023860703, 19.881222941016,
20.0436302759402, 19.9570453168158, 19.9591040082, 19.9740784817898,
19.9751993031549, 19.986022300742, 20.0531114406967, 20.0696985627145,
20.0800828758451, 20.1153963422351, 20.1076549894179, 20.1267044622338,
20.2723835551353, 20.2393724865888, 20.1653637627644, 20.175313365826,
20.1653179028692, 20.2067113730004, 20.2925659543297, 20.2949687148603,
20.3127893925416, 20.3352981379241, 20.3625848609943, 20.4154309157981,
20.5264488163096, 20.4391570088463, 20.4361213999428, 20.4679188013375,
20.4735221776474, 20.5104401429986, 20.5802596472874, 20.5970515415252,
20.6075988336636, 20.6257156522032, 20.6528395142733, 20.7198461324164,
20.7747860368889, 20.739620648921, 20.7095747158136, 20.6824219109357,
20.6683460262695, 20.6916705410586, 20.759196128171, 20.7576642433693,
20.7734082911213, 20.7797650725761, 20.8116082106002, 20.8694893686511,
21.0404672977562, 20.985356981043, 20.9557258620312, 20.9617164802732,
20.9759120662088, 20.9794392421448, 21.0620148226635, 21.0624457124599,
21.0769973151371, 21.0901703250319, 21.098690344787, 21.1226698925968,
21.2711836808076, 21.2200341433781, 21.207193477524, 21.2331394396842,
21.2179204523064, 21.2683699326247, 21.3524515602354, 21.2680244710733,
21.361479141605, 21.3261431820478, 21.26986226303, 21.2991364766223,
21.4762517489448, 21.3298374568077, 21.3331519252179, 21.3667485789704,
21.3885075875912, 21.3906997195313, 21.4381231831175, 21.458826223458,
21.5063778806171, 21.5151033730744, 21.507010374386, 21.5687143378936,
21.7306835559895, 4.44765724836912, 4.47100191330161, 4.54882158610783,
4.5440708847657, 4.51747797566305, 4.55880902228773, 4.58341393701683,
4.64251003043301, 4.67923099142439, 4.67449572095413, 4.61706049834258,
4.66515462150976, 4.68125825810232, 4.66447683681913, 4.67120538921521,
4.75098823360583, 4.7865929014467, 4.76411723853929, 4.80166768795887,
4.80738451939104, 4.72395597727812, 4.70774920353605, 4.73128098441694,
4.7497092825493, 4.70907556418776, 4.76025128174992, 4.78333093429487,
4.83897472963042, 4.84221516595959, 4.84570544645976, 4.86534283142894,
4.83189781246032, 4.88116983304783, 4.93726397688361, 4.9949887298286,
4.99476493720366, 5.03985402500338, 5.08369093771621, 5.14388523087357,
5.18085114132468, 5.24643999990121, 5.29616550395191, 5.31221160299188,
5.19904892834139, 5.11149173059605, 4.91002237688405, 4.74559139066756,
4.62999621355914, 4.65748259587475, 4.62140362369755, 4.62160891785885,
4.64311994758678, 4.72234866533005, 4.80015528005185, 4.76255281172576,
4.81741428916888, 4.78833902844309, 4.84857045098378, 4.88628200385209,
4.90595450753678, 4.94790389478918, 4.92401525818964, 4.95677349014216,
5.01088694769778, 4.9503589208899, 4.92948635242132, 4.93119567690321,
4.95837860539992, 4.97675522372483, 5.02843176452887, 5.06149708771953,
5.11680002731314, 5.15318428207365, 5.18999785496126, 5.22291283107711,
5.27233625060222, 5.23506513572608, 5.22540885753388, 5.23894254044616,
5.2156683767814, 5.20459712839743, 5.16868853558729, 5.18200968431041,
5.1634206717668, 5.17963852108505, 5.21018392350377, 5.23236124392477,
5.21618459500301, 5.15529912255812, 5.08809220037847, 5.1198724394553,
5.14734561157324, 5.15737088983502, 5.14805750597054, 5.13433360782898,
5.13895940081431, 5.16304129678691, 5.17149369912226, 5.1450893326955,
5.12587216446798, 5.11899200918883, 5.10413318074705, 5.1160008826641,
5.13174199954551, 5.1280052744334, 5.11204768607761, 5.10168238434182,
5.12328408372326, 5.11612649122192, 5.14161587137474, 5.13479881416009,
5.14165443688476, 5.12847989418342, 5.13034008951874, 5.11006252539728,
5.07602981399522, 5.04380082747131, 4.9865270245507, 4.93875062722085,
4.83814457466529, 4.73816812891552, 4.76719339629964, 4.73243951213948,
4.7435649612657, 4.77654431271813, 4.75791190194754, 4.69814760133106,
4.63006025665572, 4.62852090719856, 4.62807952037229, 4.56670114439206,
4.50814804330275, 4.44643563505667, 4.47016149299473, 4.53912223957418,
4.57221861050859, 4.61097392628225, 4.63675759989718, 4.63305952571057,
4.63769373351235, 4.63818304039896, 4.66879922486243, 4.65819557081487,
4.71530442638786, 4.75283411765132, 4.75677199897661, 4.70834590013243,
4.70931748325188, 4.68986307689509, 4.66007542691547, 4.68658557879665,
4.71287923105122, 4.74116955758817, 4.75433533727191, 4.79454505679725,
4.80874809318759, 4.86630450704699, 4.84278723259879, 4.83917234051045,
4.85488742314064, 4.89072885950008, 4.88255695031045, 4.86964539401132,
4.85185198871889, 4.88602282140254, 4.89354723701055, 4.80281371512246,
4.75326969467741, 4.7628424628326, 4.77922033722999, 4.78674176423299,
4.81556668261512, 4.79146335227998, 4.74952108237436, 4.77041301487697,
4.71934595594234, 4.74327045521818, 4.7274675891773, 4.76201943815388,
4.78742822435935, 4.7838694829636, 4.70898722458582, 4.54042300123519,
4.43046567457835, 4.51451988598857, 4.60392217055997, 4.635511244447,
4.69067644375465, 4.68405634850711, 4.70605958678574, 4.74618067900467,
4.83260092214898, 4.58877721775886, 4.5853033584615, 4.58186865729406,
4.55644763424071, 4.57312995884514, 4.59141300784382, 4.61405195391224,
4.62539633475001, 4.62855764989542, 4.63775153880854, 4.6400153979937,
4.63484394226681, 4.66244207350167, 4.66434146018009, 4.67076691372282,
4.68407880009055, 4.68958339529632, 4.68622457601331, 4.69208811620889,
4.6940756895394, 4.69428420237835, 4.70035131806994, 4.71249186301022,
4.7271653599702, 4.7349479901132, 4.74160205558268, 4.74031934574645,
4.75062724305051, 4.74555258590119, 4.7562138340694, 4.77117641542375,
4.77731860313779, 4.78678284676528, 4.79153067887926, 4.79406738928742,
4.81477266396795, 4.80529042610458, 4.82344884188224, 4.84012452381554,
4.82978062729767, 4.84653955140898, 4.85661455200409, 4.84934319669535,
4.85983345926684, 4.86432477646786, 4.87752996938969, 4.88896248079701,
4.89778714881147, 4.90781018324614, 4.89428104718603, 4.88909193570591,
4.91223232224708, 4.90441909943736, 4.89184080947695, 4.90838616906774,
4.9120349738912, 4.91193835958192, 4.88849965474606, 4.86952089013496,
4.82754082787148, 4.82297945991404, 4.83636363788566, 4.81537478739995,
4.80170678155239, 4.79837796712813, 4.80808503798778, 4.82809683942688,
4.84168009931907, 4.85375583235012, 4.86250775605786, 4.86576774877652,
4.85101858001533, 4.88095185322792, 4.889979963576, 4.89811535971474,
4.91927159620284, 4.93489213808326, 4.94589827772837, 4.93753721242439,
4.94070114394025, 4.93820671550976, 4.94272951036566, 4.9566005031372,
4.95762163540098, 4.98371534679409, 4.97127518438922, 4.97300329929901,
4.97577277168663, 4.99013444353118, 4.98890830668264, 4.99059253380479,
4.98920233887426, 5.00013814038861, 4.99339203377456, 4.99332756375832,
4.99585549034717, 4.99514277314306, 4.97378016705823, 4.98107090641032,
4.95128483491647, 4.94360774445335, 4.94820569518161, 4.97397566869503,
4.97837258336568, 4.98575124596766, 4.99233525890304, 4.9912972420199,
5.00502716857625, 4.98574031284931, 4.99974886393147, 5.00248006801806,
5.00029210629614, 5.00032426635492, 5.00234130906807, 4.99807143017482,
5.01211153206913, 5.01282969959663, 5.00369558714223, 4.99803646081775,
4.99122145915995, 4.98203245011163, 4.98898183118227, 4.97676031402907,
4.97661306140277, 4.97790228452433, 4.97467881736556, 4.97074319702324,
4.96777121735822, 4.96666260100269, 4.97008836269719, 4.97439822264025,
4.97499263100689, 4.97356254116369, 4.99650332598755, 4.99359641505544,
5.00958842621975, 5.00937803525786, 5.01453325868578, 5.01528247340244,
5.01105287591122, 5.00468878177894, 5.00658901722306, 5.0002693118926,
4.98932483790769, 4.99855492515587, 4.98814720173172, 4.98315862786098,
4.97816073933055, 4.97250227511499, 4.97217034019837, 4.9727134145512,
4.98093640727607, 4.97743699009155, 4.97708205787666, 4.98170032114438,
4.98999371770747, 4.99342868670462, 4.98710032461129, 4.99711328978229,
4.99575482652789, 5.00170349753027, 5.01247168017715, 5.01239395194275,
5.01245423405598, 5.01879001126698, 5.02033652362449, 5.02739033730593,
5.02651459803757, 5.02453819926525, 5.02388052084628, 5.01926462079431,
4.98975208317983, 4.97258722645873, 4.9635436865624, 4.96424225452655,
4.98770778945255, 4.9635436865624, 4.96633503519968, 4.95371214669663,
4.95441761409803, 4.96214508493582, 4.96772779308498, 4.95159275346247,
4.95934199970871, 4.97397130972466, 4.96772779308498, 4.97880057057624,
4.97535347995162, 4.94946885885877, 4.96494033483413, 4.94662996412034,
4.94378298710842, 7.48320429381319, 7.6561645369399, 7.82110610880829,
7.59320189720296, 7.68683478110016, 7.95667983810111, 7.59617632139741,
7.62474227649511, 7.81676325321851, 7.71940521069487, 7.67181853053348,
8.33880996371242, 7.21094058581954, 8.03822762707727, 7.82261070201569,
7.82357158274152, 7.83735667738348, 8.1046367156374, 7.80815635609445,
7.76703032589162, 7.89220083728877, 7.83855143477394, 7.5073939335162,
8.37816256879826, 7.5059752001373, 8.15552853001404, 7.99048020502086,
7.87268970278342, 7.90447413791873, 8.36780891138641, 8.01981945164711,
7.98025535548044, 8.17482965571992, 8.03312420507814, 8.1396225979523,
8.53154436348939, 8.30064119442864, 8.31217764468587, 8.46515718841819,
8.29227902146296, 8.37957956757638, 8.77038284745496, 8.38757448056262,
8.42970131889157, 8.55673020197898, 7.94125019923153, 7.6879198778969,
9.45050056056844, 8.49521672781314, 8.5173342801255, 8.79041321175791,
8.62481679196763, 8.6089805031612, 9.02875849754497, 8.71206191409011,
8.70162108392494, 8.71428574579492, 8.73089101876822, 8.78844251470506,
9.20696012451967, 6.5096550774221, 9.31509857797606, 9.13789797374865,
8.81392733764639, 8.85674985041305, 9.24914586810566, 7.91025892946504,
9.3832351526754, 9.0349150142277, 9.07250596538971, 8.99441603776387,
9.53980564522775, 8.88032246234735, 9.0418287190433, 9.32660204283357,
9.10057585013137, 9.08539025332009, 9.49154546126259, 9.14507434057652,
9.15862717595491, 9.34371391025786, 9.23770140195837, 9.331902300461,
9.84529784537339, 9.22652292064231, 9.36711817060034, 9.45464102264676,
9.43080717829243, 9.48542815127549, 9.80315392429245, 9.46220102483428,
9.49195605787191, 9.58345398545618, 9.63034810064769, 9.62459170291441,
9.97734143922148, 7.7599692507259, 10.24019393111, 9.83720243907435,
9.76324132919314, 9.80448476069629, 10.1429407688952, 9.78954063027604,
9.81148978580127, 9.90999022870695, 9.88337955430856, 9.86638017850479,
10.3500614614058, 8.04569598345531, 10.5137723520267, 10.0317143144973,
10.0016019420988, 10.0649053768744, 10.4518094727319, 10.0925802568863,
10.1154740580661, 10.2417047261218, 10.2102565428829, 9.95096958506337,
10.736142313028, 10.2104846692811, 10.0464027137144, 10.4566501947458,
10.213399969191, 10.4108390826055, 10.7497332163144, 10.3922428157884,
10.3629994803926, 10.5490843518049, 10.5299430900147, 10.5342652815607,
10.903442654213, 10.523826123055, 9.94829558655345, 11.0676534061714,
10.7289541973848, 10.7146558087789, 11.1095150342409, 10.7975434766392,
10.8030323275495, 10.9150557366667, 10.8468764795822, 10.1911966896029,
11.575751749895, 10.8395599835383, 10.9068704738973, 11.155501263489,
11.0291969763111, 11.0962769537602, 11.4469279398172, 11.0612218040278,
11.1062426423224, 11.2517538845846, 11.2333784859651, 10.9841022587667,
11.7991470802475, 11.2598050882812, 11.254649905487, 11.4720292631746,
11.3598860229692, 11.4170427738813, 11.766391720184, 11.4134853828265,
11.4410605118049, 11.5576389232754, 11.5626475736127, 11.5282047555652,
11.9147793383982, 11.4669389639854, 11.5920019692809, 11.7656439584139,
11.5489709584581, 11.6145933534541, 12.0526884789762, 11.5721225093198,
11.5785294086931, 11.9478679038354, 11.7577088515946, 11.644026901156,
12.282699606583, 11.6893399093708, 11.7081194573217, 12.0745898194066,
11.8513909555241, 12.0120697218447, 12.3777829279197, 11.9427908749701,
11.9852440709239, 12.2849763358503, 12.1610242572971, 12.2863478497246,
12.6987948609176, 8.87626014701704, 8.75781187913873, 8.79677757108755,
8.86048921162718, 9.42285091166665, 9.15587149508903, 9.07157541184028,
9.04422296009634, 8.98415367463587, 8.99264229713128, 8.99995926865439,
8.98630888588454, 9.08279027699937, 9.01801415653217, 9.03092644682495,
9.13585333120665, 9.3964555249613, 9.31056129804753, 9.21149824677136,
9.23582853002755, 9.19896921092407, 9.1993094115266, 9.22157244245253,
9.33255711303986, 9.32050974348142, 9.25734511507957, 9.25346237744606,
9.21161756310925, 9.57206242922829, 9.53477519401849, 9.44848135105606,
9.4736925056348, 9.45353618677624, 9.50006144281617, 9.53134203318552,
9.55522920290037, 9.58433205464551, 9.49975115996635, 9.54279990568933,
9.4939629665153, 9.8456088597159, 9.80015328006924, 9.76821638323259,
9.79109456264224, 9.72522763542793, 9.77851709053319, 9.8204171003855,
9.88434452165958, 9.98640258678757, 9.88349657543331, 9.78072105929449,
9.91545274615568, 10.0965440644382, 10.069889220858, 10.1071834236742,
10.0960053864793, 10.0816170924409, 10.0972659507375, 9.98273573064832,
10.0707648293296, 10.0903422031634, 10.0334967498009, 9.988071454218,
10.0452755646408, 10.214325988114, 10.1943936821307, 10.20408926276,
10.1374653682538, 10.1747246925649, 10.1817608649851, 10.1694093867828,
10.2457147073325, 10.2759167729448, 10.2174707468632, 10.2579653028569,
10.3132625062745, 10.5801172770154, 10.5266233798438, 10.5287486381328,
10.4503144592933, 10.481479184849, 10.4934241207427, 10.502542901769,
10.5494285881606, 10.6156346275554, 10.5119074689424, 10.5311093136153,
10.5900727521127, 10.832499150024, 10.7930060469324, 10.775718578877,
10.7531531154054, 10.773311204382, 10.7696395290979, 10.7637570976447,
10.7977938807946, 10.8750990746633, 10.763894326936, 10.7864939948099,
10.8084313558315, 11.0190608163543, 10.9799244653747, 11.0238093735289,
11.0096646409103, 10.9573562063908, 11.0042746906925, 11.0131160377438,
11.0384446615171, 11.0925921149442, 11.016788345678, 11.0067286524726,
11.1218140056974, 11.2613143806394, 11.2211796761646, 11.2935015874788,
11.2367103049134, 11.1817094995799, 11.2157604633905, 11.2061686914688,
11.2392748154291, 11.4109721102435, 11.3043371225574, 11.2735248395737,
11.4375261360429, 11.5623874976603, 11.5247175044255, 11.5814242903823,
11.5093986724378, 11.5000330452891, 11.5579600619228, 11.5311240131655,
11.5954153362326, 11.6738399129326, 11.57644340322, 11.5655204319799,
11.6321174901907, 11.8360148309349, 11.8553639635506, 11.8913520673329,
11.7944527780615, 11.770991474902, 11.8087026529826, 11.7713523360005,
11.884723151518, 11.9993814545919, 11.8115717892501, 11.8332855179777,
11.9237740730483, 12.0455787827743, 12.0702342014546, 12.1012197983564,
12.0183149296719, 12.0336059065465, 12.0269239230024, 12.1132590047525,
12.5264959691571, 12.2615706812812, 12.0565371292502, 12.2657313524817,
12.1439258858968, 12.2359003050756, 12.3309092088311, 12.3771893569204,
12.3071632432092, 12.3196939443778, 12.3000789109455, 12.321469532421,
12.3683550017493, 12.4759510736235, 12.3701719620075, 12.3835321964786,
12.372549767611, 12.5961565138223, 12.6077080727407, 12.5909743980745,
12.5893548765137, 12.5974984744602, 12.6523879936874, 12.6119355657399,
12.6758307918205, 12.8047085707425, 12.7095434517684, 12.7003715885686,
12.7865053255695, 13.0041421782442, 13.0268258741114, 13.0190226032443,
13.0357037893755, 12.9527882182394, 13.0084592868531, 13.0707965999479,
13.1069788399601, 1.04885832686158, 1.06016074629379, 1.0517956106758,
1.02907998600003, 1.05054370620123, 1.07261670636915, 1.0706491823234,
1.0851355199628, 1.08488055975672, 1.08085233559646, 1.081489249884,
1.08587205516048, 1.07249155362154, 1.05497731364761, 1.05675866316574,
1.06428371643968, 1.06065865122313, 1.05621234529568, 1.05339905298902,
1.05787030302435, 1.0658034000068, 1.08707776713932, 1.08626056161822,
1.10238697375394, 1.11390088086972, 1.12120513732074, 1.11937921359653,
1.10341241626668, 1.1156190247407, 1.12376155972358, 1.12411603174635,
1.12183475077377, 1.12994175229071, 1.12956170931204, 1.12199732095331,
1.11645064755987, 1.12481242467782, 1.13066151473637, 1.13028712061827,
1.12694056065497, 1.12382226475179, 1.12352013167586, 1.13391069257413,
1.14763982976838, 1.14481816405703, 1.14852949174863, 1.14182560351963,
1.14086563926171, 1.14491904045717, 1.14897189333479, 1.14616964486707,
1.15074750127031, 1.14681353487065, 1.11151754535415, 1.10497749493861,
1.10963378437214, 1.12415745716768, 1.17507535290893, 1.20285968503846,
1.22784769136553, 1.23940795216891, 1.254741010879, 1.29442450660416,
1.30428779451896, 1.31314618462517, 1.32544236970695, 1.33728107423435,
1.34408499591568, 1.34199331033196, 1.34027541040719, 1.33616830504407,
1.33360421057602, 1.33332422301893, 1.34717794252774, 1.3502492092262,
1.35168291803248, 1.35827816606688, 1.36772644852242, 1.36755741578293,
1.36926148542701, 1.37264481021763, 1.37322962601678, 1.37643913938007,
1.37906284181634, 1.37644362054554, 1.38911039237937, 1.39412557349575,
1.40094895608589, 1.40630864159528, 1.40823485306921, 1.4138446752069,
1.42340582796496, 1.43641264727375, 1.43605231080207, 1.44839810240334,
1.45451041581127, 1.46166006472498, 1.46774816064695, 1.46930608347752,
1.47885183796249, 1.49059366171423, 1.49849145403671, 1.51209667142067,
1.5250141727637, 1.5392257264567, 1.55144303632514, 1.56488453313021,
1.58308777691125, 1.59737589266492, 1.60896279958586, 1.62553339664661,
1.63594174408691, 1.65233080464302, 1.67114336171075, 1.6897476078746,
1.71673790971729, 1.74453973794979, 1.76317526009814, 1.79187692264759,
1.84186982937622, 1.9460629324144, 2.05986108970089, 2.06767436493269,
2.0783176148561, 2.08271855277262, 2.09358626977224, 2.09674958523685,
2.11582742548029, 2.12810020369675, 2.13596929171732, 2.13972610568317,
2.14456803530813, 2.15013985201827, 2.16007349878874, 2.17165498940627,
2.18057666565755, 2.19162746118342, 2.20308765886345, 2.21304799942168,
2.22367586966847, 2.23629862083737, 2.24751866055731, 2.26100586740225,
2.40972893063106, 2.60366275683037, 2.68572993101095, 2.70501080420283,
2.6676315643757, 2.6479269687206, 2.64641010174172, 2.69966594490103,
2.69665303568271, 2.71396750774502, 2.71900427132191, 2.72876269360869,
2.76276620421252, 2.76620189252239, 2.74632816231219, 2.74196673817286,
2.72905831066292, 2.75190757584346, 2.77801573354251, 2.84089580821293,
2.85681823660541, 2.84754572013613, 2.85858396073969, 2.86184353545653,
2.86958309986952, 2.94279115543111, 2.98631808884879, 3.00648449252989,
3.00620698598987, 3.15207693676406, 3.27614511764022, 3.32011714920345,
3.39367422894347, 3.64822360464499, 3.61835354049394, 3.59374251055335,
3.63237359915986, 3.62209957896007, 3.64554153297999, 3.71611226971083,
3.76031231050606, 3.80307769833913, 3.77959145461296, 3.74772344909971,
3.95072671083008, 4.03652777624058, 4.06630193640976, 4.08838169421096,
4.09074775372752), .Dim = c(192L, 7L), .Dimnames = list(NULL,
c("EMBI+", "M2 (pesos)", "Commodity Price index", "emae",
"gasto programas SS", "recursos tributarios", "ex_rate")), .Tsp = c(2004,
2019.91666666667, 12), class = c("mts", "ts", "matrix"))

Is there a way to do a spatial join between point layer based on lat and lon with heat map created as a result of info?

I have a dataset with geometry through the sf package included with lat and lon information. I also have a heat map created as a result of the dataset
Overlay<-stat_density2d(aes(x = df$LONGITUDE, y = fd$LATITUDE, fill = ..density..), geom = 'tile', contour = F, alpha = .5)
Is there a way to join the heat map density color values into the point file?
st_join(Shots1B,Overlay)
gives me an error
Error in st_join.sf(Shots1B, Overlay) :
second argument should be of class sf: maybe revert the first two arguments?
Reproductive dataset to follow the process. Overlay is based on the result of this dataset here.
structure(list(LATITUDE = c(40.68358, 40.69754, 40.843464, 40.692547,
40.626457, 40.526894, 40.840775, 40.694035, 40.857365, 40.698807,
40.71402, 40.815, 40.55079, 40.655903, 40.890076, 40.650402,
40.79335, 40.72538, 40.75184, 40.649788, 40.686928, 40.712963,
40.801285, 40.633976, 40.670296, 40.66423, 40.817696, 40.668495,
40.841087, 40.70955, 40.733376, 40.700356, 40.83801, 40.66584,
40.761436, 40.74958, 40.73197, 40.76249, 40.668507, 40.638268,
40.696735, 40.870823, 40.574867, 40.866577, 40.775414, 40.84744,
40.542908, 40.78468, 40.632416, 40.714207, 40.727913, 40.854485,
40.698986, 40.841717, 40.861687, 40.691822, 40.856014, 40.83383,
40.68781, 40.642044, 40.69814, 40.64664, 40.680897, 40.760822,
40.74608, 40.626293, 40.767967, 40.673634, 40.579212, 40.57365,
40.73632, 40.619396, 40.820263, 40.601864, 40.810318, 40.666306,
40.708805, 40.826424, 40.63174, 40.727146, 40.67253, 40.702335,
40.587894, 40.67922, 40.65047, 40.836555, 40.870056, 40.579372,
40.805138, 40.85968, 40.605595, 40.819214, 40.827972, 40.66496,
40.719177, 40.748825, 40.733597, 40.54048, 40.738403, 40.68039,
40.817627, 40.751446, 40.76161, 40.689648, 40.596977, 40.63864,
40.565254, 40.655895, 40.68821, 40.71649, 40.876785, 40.86367,
40.835827, 40.793396, 40.84827, 40.656273, 40.693462, 40.66725,
40.844105, 40.651707, 40.680496, 40.834415, 40.7357, 40.771038,
40.69484, 40.785774, 40.733017, 40.709023, 40.692886, 40.620487,
40.618595, 40.803787, 40.82319, 40.680088, 40.827927, 40.66895,
40.879055, 40.67043, 40.875874, 40.675037, 40.767582, 40.734352,
40.63083, 40.63532, 40.714073, 40.702194, 40.764362, 40.69496,
40.79656, 40.805016, 40.66406, 40.7963, 40.66563, 40.680477,
40.737785, 40.778606, 40.75868, 40.856045, 40.880257, 40.60677,
40.695683, 40.667236, 40.8351, 40.633682, 40.698116, 40.84747,
40.8047, 40.762676, 40.7158, 40.75584, 40.772102, 40.681602,
40.62677, 40.707493, 40.8252, 40.854115, 40.768875, 40.629707,
40.72654, 40.634415, 40.66937, 40.89466, 40.669067, 40.681484,
40.82433, 40.856606, 40.65785, 40.62764, 40.58401, 40.71791,
40.780437, 40.73973, 40.7952, 40.694794, 40.614063, 40.633152,
40.612736, 40.70166, 40.80641, 40.762234, 40.863647, 40.576626,
40.60118, 40.64721, 40.681145, 40.57529, 40.786, 40.601128, 40.827923,
40.805824, 40.642776, 40.86674, 40.678375, 40.74209, 40.81228,
40.604195, 40.84383, 40.759163, 40.652927, 40.69097, 40.718864,
40.683174, 40.749744, 40.738316, 40.839382, 40.66806, 40.74715,
40.663776, 40.843903, 40.836296, 40.655285, 40.70166, 40.64606,
40.72119, 40.708363, 40.674004, 40.729176, 40.86832, 40.598515,
40.695004, 40.72773, 40.704563, 40.66807, 40.66944, 40.684082,
40.69349, 40.765266, 40.74613, 40.74708, 40.87482, 40.70399,
40.649788, 40.69507, 40.788673, 40.847897, 40.68896, 40.695377,
40.880657, 40.828114, 40.781265, 40.848736, 40.65989, 40.748436,
40.61033, 40.752556, 40.829697, 40.718826, 40.65241, 40.852673,
40.851555, 40.707928, 40.891876, 40.58947, 40.74668, 40.85814,
40.708626, 40.73464, 40.62855, 40.65563, 40.687046, 40.70326,
40.633114, 40.62046, 40.75964, 40.64254, 40.783146, 40.705452,
40.74425, 40.75348, 40.84307, 40.620914, 40.80889, 40.78847,
40.712776, 40.75868, 40.74661, 40.835705, 40.688404, 40.781715,
40.730644, 40.75218, 40.731422, 40.761234, 40.668976, 40.637276,
40.788685, 40.87356, 40.795006, 40.820095, 40.594334, 40.666306,
40.673008, 40.583626, 40.874474, 40.633995, 40.772327, 40.704937,
40.653873, 40.677917, 40.59857, 40.809563, 40.68836, 40.666737,
40.713173, 40.73006, 40.652317, 40.76122, 40.588722, 40.643456,
40.865532, 40.67612, 40.620663, 40.72166, 40.733723, 40.745686,
40.875294, 40.803555, 40.7605, 40.661995, 40.69045, 40.658672,
40.711227, 40.700485, 40.816555, 40.861862, 40.875793, 40.68657,
40.654705, 40.637054, 40.6191, 40.734566, 40.714912, 40.74734,
40.6963, 40.63598, 40.724358, 40.586277, 40.671932, 40.650703,
40.61378, 40.727375, 40.573204, 40.671604, 40.740276, 40.684,
40.704494, 40.845642, 40.82681, 40.681168, 40.662476, 40.64739,
40.687138, 40.865143, 40.866673, 40.72313, 40.674934, 40.708363,
40.739525, 40.637997, 40.750965, 40.671585, 40.694294, 40.810173,
40.694748, 40.687103, 40.861744, 40.741074, 40.67875, 40.666943,
40.6635, 40.827824, 40.575832, 40.730366, 40.640945, 40.784237,
40.76803, 40.669823, 40.659336, 40.616093, 40.763546), LONGITUDE = c(-73.97617,
-73.98312, -73.836, -73.990974, -73.918, -74.16728, -73.87246,
-73.72679, -73.84657, -73.91837, -73.74827, -73.89402, -74.20098,
-73.89817, -73.819855, -73.89422, -73.97275, -74.00011, -73.90358,
-73.9622, -73.920815, -73.93647, -73.95394, -74.02211, -73.997604,
-73.919106, -73.922615, -73.925606, -73.86447, -73.95887, -73.86665,
-73.95732, -73.87329, -73.75551, -73.76995, -73.86541, -73.78651,
-73.839584, -73.779625, -73.93187, -73.93481, -73.8721, -74.00069,
-73.8722, -73.91984, -73.89968, -74.15579, -73.80911, -73.94724,
-73.92817, -73.873245, -73.854645, -73.91671, -73.94435, -73.82435,
-73.92223, -73.91213, -73.921234, -73.9237, -73.98124, -73.89111,
-73.9246, -73.95118, -73.99832, -73.974945, -74.01572, -73.96822,
-73.89294, -73.976265, -74.11252, -73.85631, -73.969574, -73.92976,
-74.00232, -73.943634, -73.79171, -73.92577, -73.85868, -73.96793,
-73.954735, -73.798386, -73.89073, -73.95504, -73.90405, -73.917366,
-73.94306, -73.83222, -74.16948, -73.945244, -73.90427, -73.98404,
-73.84662, -73.88707, -73.82226, -73.79223, -73.96984, -73.91062,
-74.153404, -73.93864, -73.94956, -73.92366, -74.001434, -73.97076,
-73.9184, -73.97324, -74.02245, -74.1301, -73.898224, -73.96583,
-73.98484, -73.87446, -73.86741, -73.89068, -73.94043, -73.88312,
-73.90731, -73.965485, -73.88799, -73.923065, -73.93121, -73.821365,
-73.92854, -73.90377, -73.83413, -73.98391, -73.97052, -73.8852,
-73.757835, -73.832184, -74.029305, -73.99847, -73.953896, -73.889496,
-73.94398, -73.90094, -73.9339, -73.87439, -73.928185, -73.84989,
-73.930534, -73.9109, -74.00849, -73.90736, -73.95033, -73.95087,
-73.93587, -73.96162, -73.946236, -73.97226, -73.92109, -73.73846,
-73.93829, -73.73921, -73.7921, -73.93496, -73.98163, -73.87552,
-73.90079, -73.843864, -73.759575, -73.741875, -73.770004, -73.8825,
-73.89868, -73.977325, -73.89134, -73.91243, -73.954346, -73.824486,
-73.99238, -73.763954, -73.95855, -73.946884, -73.94153, -73.867714,
-73.89091, -73.94898, -73.90486, -73.71589, -74.08535, -73.89523,
-73.86137, -73.9878, -73.85049, -73.874374, -73.92841, -73.91648,
-73.89022, -73.98587, -73.95341, -73.94989, -73.70625, -73.94623,
-73.98246, -74.01393, -73.97762, -74.01172, -73.961464, -73.94227,
-73.98987, -73.8918, -73.98478, -73.99098, -74.01531, -73.7923,
-73.97655, -73.84574, -73.997086, -73.93483, -73.954636, -74.02003,
-73.92873, -73.9265, -73.984985, -73.90941, -73.97218, -73.886375,
-73.988396, -73.959335, -73.94833, -73.97483, -73.87389, -73.884315,
-73.98773, -73.84531, -73.90394, -73.985504, -73.889915, -73.927284,
-73.87369, -73.93658, -73.961464, -74.01648, -73.761185, -73.87203,
-73.81881, -73.87898, -73.83317, -73.766464, -73.9525, -73.90674,
-73.91066, -73.80789, -73.883995, -73.90864, -73.97917, -73.81517,
-73.83605, -73.98135, -73.877, -73.85589, -73.9622, -73.80114,
-73.97136, -73.92499, -73.93326, -73.94921, -73.877625, -73.93107,
-73.97599, -73.93234, -73.90536, -73.984566, -73.95932, -73.92972,
-73.91313, -73.98424, -73.9264, -73.919106, -73.952446, -73.784294,
-73.8616, -73.80105, -73.9745, -73.895744, -73.94513, -73.87421,
-73.952835, -73.92596, -73.792114, -73.86474, -73.94928, -74.07641,
-73.95817, -73.87652, -73.97833, -73.78149, -73.7334, -73.980896,
-73.848076, -73.9753, -73.95581, -73.968895, -73.90601, -73.87552,
-73.86473, -73.88875, -73.93803, -73.823845, -73.97329, -73.85201,
-73.94641, -73.96389, -73.90668, -73.93177, -73.94386, -73.81853,
-73.9485, -73.955086, -73.990944, -73.79171, -73.97851, -73.98407,
-73.90031, -73.98138, -73.94531, -73.94932, -74.008156, -73.93861,
-73.9689, -73.92923, -73.96444, -73.90224, -73.90076, -73.710754,
-73.92752, -73.93056, -73.960464, -73.972725, -73.86238, -73.936005,
-74.1524, -73.888664, -73.72516, -73.97213, -73.9088, -73.91184,
-73.95699, -73.91959, -73.959435, -73.90019, -73.72826, -73.93637,
-73.91755, -73.91282, -73.85465, -73.776146, -74.00731, -73.98643,
-74.159615, -73.72269, -73.94784, -73.88673, -73.97545, -73.915146,
-73.742516, -73.9862, -73.85165, -73.920586, -74.07225, -73.90313,
-74.09711, -73.86907, -73.92782, -73.95031, -73.81743, -73.90211,
-73.85361, -73.92941, -73.768326, -73.92314, -73.75136, -73.87204,
-73.90896, -73.90614, -73.80186, -73.92401, -73.92512, -74.02136,
-73.94027, -73.99843, -73.74868, -73.95117, -73.73427, -73.89251,
-73.911804, -73.7258, -73.794, -73.890144, -73.94276, -73.91934,
-74.12409, -73.91388, -73.94852, -73.947075, -73.87722, -73.90981,
-73.92726, -74.14523, -73.88209), COLLISION_ID = c(4407147L,
4136992L, 4395664L, 4397513L, 4403773L, 4405244L, 4405914L, 4407366L,
4407778L, 4407461L, 4407407L, 4407900L, 4407760L, 4407746L, 4408143L,
4407638L, 4407958L, 4407885L, 4407616L, 4408038L, 4408224L, 4407392L,
4407765L, 4407821L, 4407971L, 4408071L, 4407430L, 4408259L, 4407592L,
4407674L, 4407708L, 4408396L, 4407152L, 4407862L, 4407636L, 4407792L,
4407853L, 4408205L, 4407945L, 4408118L, 4408242L, 4407563L, 4408098L,
4407169L, 4407798L, 4407797L, 4407349L, 4407994L, 4408032L, 4407478L,
4407924L, 4408315L, 4407892L, 4408280L, 4408403L, 4407753L, 4408003L,
4407497L, 4408229L, 4407525L, 4407817L, 4407539L, 4408306L, 4407830L,
4407282L, 4407688L, 4407701L, 4407728L, 4408052L, 4407849L, 4407320L,
4407291L, 4408200L, 4407649L, 4407802L, 4407345L, 4408356L, 4407245L,
4408057L, 4408332L, 4407785L, 4407929L, 4407425L, 4408080L, 4408123L,
4408290L, 4408412L, 4407757L, 4407770L, 4407554L, 4407873L, 4407502L,
4408044L, 4408165L, 4407544L, 4407277L, 4407834L, 4407338L, 4407397L,
4408109L, 4407683L, 4407829L, 4407496L, 4407609L, 4407689L, 4407861L,
4407350L, 4407721L, 4407381L, 4407653L, 4407896L, 4407914L, 4407512L,
4408427L, 4407532L, 4408086L, 4407856L, 4407729L, 4407789L, 4408129L,
4407576L, 4408193L, 4407643L, 4407906L, 4407414L, 4407623L, 4407436L,
4407952L, 4407761L, 4408063L, 4407388L, 4407766L, 4407901L, 4408104L,
4407486L, 4408264L, 4407678L, 4407355L, 4408155L, 4408271L, 4408380L,
4407598L, 4407866L, 4407452L, 4407809L, 4407393L, 4407453L, 4408214L,
4407824L, 4407431L, 4407632L, 4407841L, 4407096L, 4407658L, 4407933L,
4407976L, 4407714L, 4407373L, 4407568L, 4407707L, 4407692L, 4407865L,
4407154L, 4407637L, 4407354L, 4407793L, 4407432L, 4407946L, 4407852L,
4408249L, 4407987L, 4408215L, 4408027L, 4407389L, 4407562L, 4407794L,
4407779L, 4407997L, 4407408L, 4408313L, 4407747L, 4408402L, 4407702L,
4407762L, 4407297L, 4407321L, 4408100L, 4407505L, 4408047L, 4407805L,
4407697L, 4407367L, 4407837L, 4407548L, 4407820L, 4408150L, 4407842L,
4407673L, 4407278L, 4407459L, 4407514L, 4408367L, 4407313L, 4407396L,
4407611L, 4408053L, 4407905L, 4407642L, 4407556L, 4407774L, 4407360L,
4408139L, 4408223L, 4408075L, 4407967L, 4407875L, 4407915L, 4408258L,
4408033L, 4408308L, 4407869L, 4407732L, 4407602L, 4407959L, 4408397L,
4408067L, 4408279L, 4407723L, 4408198L, 4407501L, 4408119L, 4408359L,
4407816L, 4407543L, 4407801L, 4407346L, 4407769L, 4408093L, 4407475L,
4408113L, 4407833L, 4407923L, 4407424L, 4407741L, 4407891L, 4407647L,
4407909L, 4407439L, 4407604L, 4407752L, 4407380L, 4408037L, 4408416L,
4407549L, 4408002L, 4408227L, 4408262L, 4407897L, 4408187L, 4407975L,
4408164L, 4408085L, 4408250L, 4408028L, 4407580L, 4408302L, 4407400L,
4408128L, 4408194L, 4408289L, 4407953L, 4408391L, 4408204L, 4407870L,
4407524L, 4408353L, 4407445L, 4408056L, 4407327L, 4407784L, 4407928L,
4407454L, 4407372L, 4407444L, 4407756L, 4407620L, 4407412L, 4407633L,
4407119L, 4407660L, 4408154L, 4407773L, 4407567L, 4407938L, 4407713L,
4408208L, 4408043L, 4408233L, 4407990L, 4408079L, 4408217L, 4408331L,
4407698L, 4407825L, 4408122L, 4407838L, 4407948L, 4407843L, 4407515L,
4407312L, 4407314L, 4407489L, 4407656L, 4407332L, 4407506L, 4408401L,
4407677L, 4407359L, 4408266L, 4407696L, 4407261L, 4407857L, 4407490L,
4407788L, 4408379L, 4408103L, 4408076L, 4408049L, 4407527L, 4407836L,
4408274L, 4407547L, 4407815L, 4408375L, 4407279L, 4407331L, 4407686L,
4408398L, 4408066L, 4408309L, 4408068L, 4407691L, 4407353L, 4407433L,
4407679L, 4407566L, 4408026L, 4407651L, 4407296L, 4407826L, 4407368L,
4407804L, 4407242L, 4407858L, 4407795L, 4407347L, 4408059L, 4407715L,
4408114L, 4408312L, 4407922L, 4407342L, 4407748L, 4407619L, 4408297L,
4407763L, 4408005L, 4407504L, 4408231L, 4407049L, 4408106L, 4407542L,
4407523L, 4407428L, 4408257L, 4407595L, 4407672L, 4407832L, 4407384L,
4408387L, 4407790L, 4407612L, 4407775L, 4407864L, 4407379L, 4407156L,
4407634L, 4408389L, 4407724L, 4407942L, 4408197L, 4407847L, 4407403L,
4408120L, 4407703L, 4407668L, 4407570L, 4408101L, 4407800L, 4407917L
)), row.names = c(NA, 400L), class = "data.frame")

polygon shading the inverse region in R- how to fix

I am plotting a truncated density in R. Here are the coordinates of my polygon from dput output:
dat <- structure(list(pro.d.x = c(0.587084148727984, 0.589041095890411,
0.590998043052838, 0.592954990215264, 0.594911937377691, 0.596868884540117,
0.598825831702544, 0.600782778864971, 0.602739726027397, 0.604696673189824,
0.60665362035225, 0.608610567514677, 0.610567514677104, 0.61252446183953,
0.614481409001957, 0.616438356164384, 0.61839530332681, 0.620352250489237,
0.622309197651663, 0.62426614481409, 0.626223091976517, 0.628180039138943,
0.63013698630137, 0.632093933463796, 0.634050880626223, 0.63600782778865,
0.637964774951076, 0.639921722113503, 0.64187866927593, 0.643835616438356,
0.645792563600783, 0.647749510763209, 0.649706457925636, 0.651663405088063,
0.653620352250489, 0.655577299412916, 0.657534246575342, 0.659491193737769,
0.661448140900196, 0.663405088062622, 0.665362035225049, 0.667318982387475,
0.669275929549902, 0.671232876712329, 0.673189823874755, 0.675146771037182,
0.677103718199609, 0.679060665362035, 0.681017612524462, 0.682974559686888,
0.684931506849315, 0.686888454011742, 0.688845401174168, 0.690802348336595,
0.692759295499021, 0.694716242661448, 0.696673189823875, 0.698630136986301,
0.700587084148728, 0.702544031311155, 0.704500978473581, 0.706457925636008,
0.708414872798434, 0.710371819960861, 0.712328767123288, 0.714285714285714,
0.716242661448141, 0.718199608610568, 0.720156555772994, 0.722113502935421,
0.724070450097847, 0.726027397260274, 0.727984344422701, 0.729941291585127,
0.731898238747554, 0.73385518590998, 0.735812133072407, 0.737769080234834,
0.73972602739726, 0.741682974559687, 0.743639921722113, 0.74559686888454,
0.747553816046967, 0.749510763209393, 0.75146771037182, 0.753424657534246,
0.755381604696673, 0.7573385518591, 0.759295499021526, 0.761252446183953,
0.76320939334638, 0.765166340508806, 0.767123287671233, 0.769080234833659,
0.771037181996086, 0.772994129158513, 0.774951076320939, 0.776908023483366,
0.778864970645793, 0.780821917808219, 0.782778864970646, 0.784735812133072,
0.786692759295499, 0.788649706457926, 0.790606653620352, 0.792563600782779,
0.794520547945205, 0.796477495107632, 0.798434442270059, 0.800391389432485,
0.802348336594912, 0.804305283757339, 0.806262230919765, 0.808219178082192,
0.810176125244618, 0.812133072407045, 0.814090019569472, 0.816046966731898,
0.818003913894325, 0.819960861056751, 0.821917808219178, 0.823874755381605,
0.825831702544031, 0.827788649706458, 0.829745596868884, 0.831702544031311,
0.833659491193738, 0.835616438356164, 0.837573385518591, 0.839530332681018,
0.841487279843444, 0.843444227005871, 0.845401174168297, 0.847358121330724,
0.849315068493151, 0.851272015655577, 0.853228962818004, 0.85518590998043,
0.857142857142857, 0.859099804305284, 0.86105675146771, 0.863013698630137,
0.864970645792564, 0.86692759295499, 0.868884540117417, 0.870841487279843,
0.87279843444227, 0.874755381604697, 0.876712328767123, 0.87866927592955,
0.880626223091976, 0.882583170254403, 0.88454011741683, 0.886497064579256,
0.888454011741683, 0.89041095890411, 0.892367906066536, 0.894324853228963,
0.896281800391389, 0.898238747553816, 0.900195694716243, 0.902152641878669,
0.904109589041096, 0.906066536203523, 0.908023483365949, 0.909980430528376,
0.911937377690802, 0.913894324853229, 0.915851272015656, 0.917808219178082,
0.919765166340509, 0.921722113502935, 0.923679060665362, 0.925636007827789,
0.927592954990215, 0.929549902152642, 0.931506849315068, 0.933463796477495,
0.935420743639922, 0.937377690802348, 0.939334637964775, 0.941291585127201,
0.943248532289628, 0.945205479452055, 0.947162426614481, 0.949119373776908,
0.951076320939335, 0.953033268101761, 0.954990215264188, 0.956947162426614,
0.958904109589041, 0.960861056751468, 0.962818003913894, 0.964774951076321,
0.966731898238748, 0.968688845401174, 0.970645792563601, 0.972602739726027,
0.974559686888454, 0.976516634050881, 0.978473581213307, 0.980430528375734,
0.98238747553816, 0.984344422700587, 0.986301369863014, 0.98825831702544,
0.990215264187867, 0.992172211350294, 0.99412915851272, 0.996086105675147,
0.998043052837573, 1), pro.d.y = c(0, 0.0124763366981593, 0.0141124551851867,
0.0159018210787856, 0.0178518210828018, 0.0199740509194942, 0.0223445527492011,
0.0249064839074225, 0.0276662964727761, 0.0306302708705622, 0.0338545211312297,
0.0373385397435565, 0.0410496060511471, 0.0449918664776745, 0.0491758970805405,
0.0536901895276933, 0.0584490455650341, 0.0634544237153568, 0.0687081431462095,
0.0742700062159316, 0.0801277089300282, 0.086240025634052, 0.0926086975186578,
0.0992440060892446, 0.106239285811027, 0.113504059066459, 0.121044019265177,
0.128865573418761, 0.137048548852888, 0.145593863797181, 0.154467617291928,
0.163685889084493, 0.173280379056521, 0.183418988934254, 0.193997374886688,
0.205045023363949, 0.21659280832808, 0.228818951343029, 0.241754902638203,
0.255354359835895, 0.269660574110284, 0.284751550765693, 0.300974352004478,
0.318105788417593, 0.336193761251992, 0.355285850148719, 0.37570768608243,
0.397453045785548, 0.420391899838677, 0.444560692193431, 0.470052830600742,
0.497323432291453, 0.525934510701712, 0.55589495930279, 0.587210052668763,
0.620209479614381, 0.654737312164808, 0.690555064040503, 0.727627117977952,
0.765968447827405, 0.805823333992903, 0.846691934407899, 0.888496561425915,
0.931155864506105, 0.974725912749141, 1.01894272802093, 1.06358946338081,
1.10855652184076, 1.15373258418124, 1.19891666804584, 1.24392280480721,
1.28863577510934, 1.33294134574882, 1.37653543493332, 1.41927889838227,
1.46115790850083, 1.50208011643625, 1.54188812802516, 1.58010796114519,
1.61704327373793, 1.65264078703095, 1.68685158559086, 1.71924489000963,
1.74994440160701, 1.77914340182238, 1.80683605037902, 1.8329284799749,
1.85702633860664, 1.87966618832539, 1.90087561596266, 1.92068458372179,
1.93878680429694, 1.95543121172768, 1.97084637077113, 1.98507694736024,
1.99809848497021, 2.00970880002476, 2.0203323177052, 2.03001268341046,
2.03879277291169, 2.04650577512196, 2.05333770822687, 2.05943314402644,
2.06482425018454, 2.06949798196726, 2.07333994584229, 2.07659348278548,
2.07927970501823, 2.08141886898654, 2.08289568306932, 2.0838167309169,
2.08426362597097, 2.08425136563913, 2.08376347737203, 2.08272129413889,
2.0812848925494, 2.07947064189011, 2.07729538738197, 2.07469292214384,
2.07175241370009, 2.06853824861803, 2.06507440050244, 2.06137194834198,
2.05743920123393, 2.05338026815633, 2.04922923154637, 2.04502128439529,
2.04081183457356, 2.03667849489226, 2.03266717082546, 2.02882485726764,
2.02522571223853, 2.02203770460098, 2.0192476674025, 2.01691496522578,
2.01510017656093, 2.01407190394343, 2.01385052206395, 2.01443658686903,
2.01590202326501, 2.01841515958639, 2.02236038715918, 2.02751514911719,
2.03395909497369, 2.0417722975773, 2.0515066820042, 2.06304583606758,
2.07630508949246, 2.09136419448661, 2.1084876952701, 2.12825404999518,
2.15014999861247, 2.17424485778335, 2.2006062832039, 2.23002312279098,
2.26215697251894, 2.2968041549068, 2.33400970407032, 2.37407124084828,
2.41760250489697, 2.4638260220103, 2.51275499122616, 2.56439904161634,
2.61956960340193, 2.67771334572469, 2.73852719993077, 2.80198177705806,
2.86829607508334, 2.93788571360972, 3.00990094479677, 3.08426858458271,
3.16091081580917, 3.24034038243829, 3.3219275671303, 3.40533998955121,
3.49044957580254, 3.57725694079797, 3.66572547223431, 3.75521994794363,
3.84555419923005, 3.93653584878367, 4.02795152621416, 4.11931406838431,
4.21034136592417, 4.30077594829744, 4.39020281325105, 4.47783699125228,
4.56363022834507, 4.64726414556742, 4.72841524545307, 4.80564182077226,
4.87897706777627, 4.94833350100604, 5.01335537401227, 5.07307996209023,
5.1259118810479, 5.17287726780172, 5.21364011252759, 5.24787000019841,
5.27287709283815, 5.28975954430921, 5.29886896386876, 5.29997375028775,
5.29185426024869, 5.27278869421654, 5.24503234244234, 5.20852067701082,
5.16320857873499, 5.10629616085988, 5.04001017913819, 4.96520581342729,
4.88204743797809)), .Names = c("pro.d.x", "pro.d.y"), row.names = 301:512, class = "data.frame")
I generally shade a polygon region fairly easy using plot() and polygon() like this:
plot(dat,xlim = c(0,1), ylim = c(0,10), bty = 'n', xlab = NA, ylab = NA, main = NA, yaxs='i', xaxs = 'i', las = 1, cex = 0)
polygon(dat, col = 'green')
However this plot looks like this:
Its shading some type of inverse of what I would like, and adding this very straight line. Any idea how I could shade all regions below the line?
The concept of polygon() is to connect all the points in order and at last it will link the last point to the first one. (That is why the straight line is created) To deal with it, you need to make an additional vertex c(1, 0) as a relay between the first and last points.
polygon(rbind(dat, c(1, 0)), col = 'green')

Plotting Curves from Data Frame Columns

i am facing a problem in plot ols estimations in a scatterplot:
I have this data frame: With 9 columns and 99 rows:
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")
My scatter plot will consist of the first column (Y) and the second column (X).
The third column i will not use.
From the fourth column are the curves that are fitted values of OLS regressions.
How do I include them using the plot function?
i am doing this, but its not working
plot(data[,2],data[,1])
for(i in 4:9){
lines(data[,i])
}
What am i doing wrong?
Basically you want
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

How to plot the forecasted values against actual values observed later in R?

We used the R library forecast to make predictions for the next 24 hours. We have the following:
fore_cast=forecast.tbats(model,h=24,level=90)
fore_cast
Point Forecast Lo 90 Hi 90
5.380952 6270.778 5389.089 7296.643
5.386905 5458.096 4557.375 6536.743
5.392857 5219.995 4248.967 6412.814
5.398810 5187.102 4126.390 6520.328
Now we have 2 problems:
We need 'time' (in hour e.g. 01,23,19 etc) instead of 'point'.
We wish to plot the trendline against time showing the actual observed
values against these predicted values. We have loaded actual observed
values from a CSV file.
We tried:
actual_data = read.csv('actualdata.csv')
plot(actual_data,fore_cast)
Doesn't work, and using plot(actual_data) just shows some points in a straight line instead of curved trendline.
EDIT:
Sample output of fore_cast from dput:
structure(list(model = structure(list(lambda = 0.000438881055939422,
alpha = 0.65694875480321, beta = -0.0983972877836753, damping.parameter = 0.800419363290521,
gamma.one.values = c(-0.00150031474145603, -0.00124696854910294
), gamma.two.values = c(0.0023600487982342, -0.002465549595849
), ar.coefficients = NULL, ma.coefficients = NULL, likelihood = 13202.294346586,
optim.return.code = 0L, variance = 0.00855092137349485, AIC = 13258.294346586,
parameters = structure(list(vect = c(0.000438881055939422,
0.65694875480321, 0.800419363290521, -0.0983972877836753,
-0.00150031474145603, -0.00124696854910294, 0.0023600487982342,
-0.002465549595849), control = structure(list(use.beta = TRUE,
use.box.cox = TRUE, use.damping = TRUE, length.gamma = 4L,
p = 0, q = 0), .Names = c("use.beta", "use.box.cox",
"use.damping", "length.gamma", "p", "q"))), .Names = c("vect",
"control")), seed.states = structure(c(7.44188559667267,
0.00357069100887873, -0.0664300680553579, 0.0229067500159256,
0.00460111570469819, -0.00772324725408007, -0.000610110386029883,
0.00568378752162509, -0.0084050648066819, -0.0324093004247092,
-0.000720936399990958, -0.00705790547321605, -0.00738992950838566,
0.00180424326179638, -0.00107745502434416, 0.00242014705705761,
-0.01824679745657, 0.0123019701003545, -0.0245935735677402,
0.0181321397860132), .Dim = c(20L, 1L)), fitted.values = structure(c(1598.57443298879,
1435.74973092922, 1397.92464316794, 1296.90202189518, 1440.3201303663,
1544.11695101118, 1777.97079874181, 1766.50571671645, 1925.27360388028,
1863.26963233038, 1773.08363764691, 1887.26580055295, 1887.48006609474,
1841.66200850472, 1991.90290660363, 2233.04775631848, 2081.30246965768,
1872.12639817609, 1899.38583561568, 2213.43437455052, 2214.00832820531,
1745.36311914995, 1678.67975050944, 1502.35472259274, 1512.27350460399,
1456.14165844166, 1464.3803467642, 1517.99443293857, 1484.54280422369,
1382.37041287489, 1452.43700910726, 1545.16934543365, 1440.50974319508,
1475.59742668699, 1544.88546424501, 1790.95280713647, 1916.4267023671,
1928.72804180587, 1819.15839770808, 1916.43079357329, 1836.80043977753,
1720.25638746452, 1730.03629161412, 1614.6048115754, 1599.23641723244,
1635.86950932066, 1543.46360784778, 1641.35066985679, 1608.60556151299,
1651.47649465456, 1475.15006990464, 1403.67294742438, 1507.58932406857,
1666.3170708439, 1696.06132797576, 1543.32187293056, 1704.58043626911,
1914.72424191575, 2109.33624862625, 2092.98934458578, 2222.13355258602,
2084.68677709368, 1962.9230489947, 2045.61547393981, 2140.30565941261,
2097.46130996426, 2126.07936955385, 2226.18935508502, 2269.54492801286,
2300.37314952852, 2398.48786829541, 2303.31270702723, 2332.74139979969,
2146.51487558643, 2101.27480789243, 2111.61910899422, 2053.57840714969,
2046.56606362537, 2073.82870990658, 2094.88831798868, 2334.85185938782,
2541.72156227893, 2502.36031483721, 2398.12240784327, 2266.35832277135,
2151.05248890962, 2266.88803633019, 2366.19453856405, 2399.97570044332,
2341.74959623409, 2144.33465155869, 2102.91952061083, 2214.48622101851,
2179.48115699957, 2288.28092735955, 2224.55218736155, 2195.1506809087,
2163.94619334319, 2161.41843642149, 2134.75060670667, 2138.77895768654,
2142.84680080931, 2258.55072549978, 2297.90237035988, 2314.94197015208,
2300.99928929609, 2277.39754662665, 2291.06980363364, 2487.04257346235,
2381.05768214413, 2509.40078456481, 2657.61336243367, 2528.65026804303,
2434.2722174014, 2366.04811963942, 2270.6647135766, 2231.33965004538,
2376.51043520344, 2249.42861599343, 2193.98771109322, 2252.12327312365,
2210.76969838623, 2180.50451255189, 2221.92898123682, 2537.84678083006,
2329.57350097532, 2252.82349908982, 2143.92033677754, 2092.3142840022,
2084.70304624685, 2111.18929138546, 2160.05383108999, 2280.94409931504,
2118.22029344747, 2214.65738250204, 2269.05911898631, 2084.26658709038,
2016.04764576402, 2095.57091797435, 2161.07354463394, 2427.77607700887,
2333.91103594967, 2234.23838054763, 2250.71557301013, 2186.97925802073,
2129.51096829218, 2115.40228652934, 2094.89231085691, 2086.41044567131,
2180.94542608489, 2105.38187642016, 2459.45788915933, 2292.36325639374,
2410.75372754831, 2375.56640249604, 2491.11938114866, 2470.51372278037,
2464.95765202085, 2600.85929020727, 2709.48518695182, 2779.77558137814,
2518.29927341458, 2344.06621605191, 2391.56719713269, 2368.68842788795,
2199.93530349068, 2113.92970206565, 2458.96718445444, 3121.97852988865,
2559.40932439262, 2331.12829078836, 2238.54586985577, 2241.91440620202,
2225.29804576634, 2154.14147781021, 2060.57980596908, 2037.30100544426,
2215.93410789353, 2364.42668160056, 2518.72871618042, 2537.34279365294,
2473.76096855791, 2623.63387707374, 2589.08335304697, 2577.0563838788,
2349.53279218826, 2305.52193868551, 2232.63712180453, 2167.50003597208,
2320.23187534213, 2281.86365949586, 2281.21119271599, 2323.2014703372,
2185.94404743238, 2140.21863271207, 2011.67723856012, 1966.52063119589,
2002.67344212857, 1952.41101080662, 1988.37461163105, 2126.75137749373,
2239.14722292367, 2320.98046489603, 2444.91847853015, 2431.69548763034,
2514.73820659393, 2505.85249387343, 2888.19773974179, 2853.20690693738,
2502.20865871069, 2524.56894781003, 2659.52271740553, 2615.9025930681,
2923.69327019152, 2754.76074569658, 2784.59488335761, 2874.24378479002,
2683.41908597168, 2733.83011888159, 2774.1325162997, 2906.41593326865,
2726.06821502751, 2460.21579967528, 2450.8035097605, 2547.39389733175,
2625.60323572861, 2827.94083526683, 2971.92012845614, 3042.90981987278,
2835.00811374845, 2846.98066660519, 2871.21876763166, 2901.99696373824,
2627.47532996657, 2583.75084300313, 2602.68041642846, 2632.8054092953,
2667.85374690972, 2639.10586730146, 2466.95799545022, 2381.06823502402,
2531.32611053776, 2407.14812148706, 2342.75701798463, 2401.73791085847,
2365.50645844524, 2404.50408575777, 2452.57343738519, 2613.15332739214,
2665.50965844576, 2723.8237337447, 2915.09266385617, 2890.17498445896,
2853.6278331055, 2868.1228183545, 2917.07803535669, 2876.59409770233,
2577.82035337979, 2581.91435020803, 2520.20342021937, 2603.37973251208,
2536.03988578365, 2510.83398648802, 2472.80606784857, 2425.51212342113,
2442.02863541673, 2465.73405821711, 2384.42988766816, 2555.51500549788,
2737.77091706275, 2425.00224845814, 2460.17325671183, 2639.16650619329,
2816.37024420397, 2755.69999167982, 2802.64991688288, 2685.12803367301,
2521.77568128564, 2500.99980614696, 2620.41659854805, 2529.25134423133,
2590.14804885984, 2318.80485234464, 2341.88940012276, 2460.21008281205,
2513.70688167177, 2437.71670675479, 2383.29782281743, 2499.36244454453,
2472.98602901478, 2491.10649022417, 2350.1405559119, 2362.78308814045,
2431.3911847573, 2321.15216823049, 2355.74203614213, 2429.60523843166,
2355.61947983433, 2346.3751018515, 2453.82214513707, 2542.98125962684,
2342.43364707529, 2302.17741211575, 2388.93541944219, 2435.41878657221, ....
Sample output from dput for actual observed values:
structure(list(index12 = c(6297.416944, 5406.865556, 4718.355556,
5304.729167, 4968.014722, 5081.130833, 5544.955, 4655.009444,
4269.023056, 4346.588333, 4511.455833, 5102.57, 4818.673333,
4862.343056, 4785.176667, 5385.005278, 6469.080833, 7166.025278,
7010.708333, 511.114167)), .Names = "index12", class = "data.frame", row.names = c(NA,
-20L))
The value of Point is unusual in spite of hour unit data. I think you failed to make a model.
Here is my example:
actual_data <- structure(list(index12 = c(6297.416944, 5406.865556, 4718.355556,
5304.729167, 4968.014722, 5081.130833, 5544.955, 4655.009444,
4269.023056, 4346.588333, 4511.455833, 5102.57, 4818.673333,
4862.343056, 4785.176667, 5385.005278, 6469.080833, 7166.025278,
7010.708333, 511.114167)),
.Names = "index12", class = "data.frame", row.names = c(NA, -20L))
# I suppose that actual_data was taken per hour.
num_actual <- as.numeric(actual_data[,1])
model <- bats(num_actual)
fore_cast <- forecast(model, h=24, level=90)
fore_cast # Point is from 21 to 44 because of length(actual_data)=20 and demanding predictions for the next 24 hours
# Point Forecast Lo 90 Hi 90
# 21 5063.207 2902.187 7224.226
# 22 5108.114 2946.988 7269.241
# :
# 44 5108.114 2944.629 7271.600
# plot() has forecast method. It draws actual_data and prediction, and paints Lo90-Hi90.
plot(fore_cast, main="")

Resources