Related
I hope you're doing well! I have a theoretical time-series analysis problem I hope that you can help me sort out.
To start, you'll find a reproducible example of my dataset below. Date is in a daily timescale. Q25 is 25th or lower quartile of my non-transformed data, Q75 is the 75th or upper quartile of my non-transformed data, fit is local weighted fit of the median, firstder is the first derivative of fit, and secondder is the second derivative of fit.
Plotting out fit produces two oscillations and then a steady increase in the data. Plotting the quartiles around that produces a large spread of data that narrows towards the increase in fit data. The first derivative shows the rate of change of the fit and this is where my issue comes in. I'm not sure where the increase in fit data starts based on the first derivative data. Logically, I know the signal-to-noise start date has to occur after March 7 (based on the quartiles), and before March 20 (before the steady increase in fit data). And this is also represented in the first derivative for about the same interval where the negative-to-positive inflection point changes on March 5th, becomes positive on March 16th, and then produces a stationary time series.
All that being said, should my exact start date be the change in the inflection point from the first derivative, or be the first positive value on March 16th?
I appreciate your time in this problem and any thoughts you may have!
data<-structure(list(Date = structure(c(1485950474, 1486036874, 1486123274,
1486209674, 1486296074, 1486382474, 1486468874, 1486555274, 1486641674,
1486728074, 1486814474, 1486900874, 1486987274, 1487073674, 1487160074,
1487246474, 1487332874, 1487419274, 1487505674, 1487592074, 1487678474,
1487764874, 1487851274, 1487937674, 1488024074, 1488110474, 1488196874,
1488283274, 1488369674, 1488456074, 1488542474, 1488628874, 1488715274,
1488801674, 1488888074, 1488974474, 1489060874, 1489147274, 1489233674,
1489320074, 1489406474, 1489492874, 1489579274, 1489665674, 1489752074,
1489838474, 1489924874, 1490011274, 1490097674, 1490184074, 1490270474,
1490356874, 1490443274, 1490529674, 1490616074, 1490702474, 1490788874,
1490875274, 1490961674, 1491048074, 1491134474, 1491220874, 1491307274,
1491393674, 1491480074, 1491566474, 1491652874, 1491739274, 1491825674,
1491912074, 1491998474, 1492084874, 1492171274, 1492257674, 1492344074,
1492430474, 1492516874, 1492603274, 1492689674, 1492776074, 1492862474,
1492948874, 1493035274, 1493121674, 1493208074, 1493294474, 1493380874,
1493467274, 1493553674, 1493640074, 1493726474, 1493812874, 1493899274,
1493985674, 1494072074, 1494158474, 1494244874, 1494331274, 1494417674,
1494504074, 1494590474, 1494676874, 1494763274, 1494849674, 1494936074,
1495022474, 1495108874, 1495195274, 1495281674, 1495368074), tzone = "UTC", class = c("POSIXct",
"POSIXt")), Q25 = c(-1.61495132528742, -3.86616056128065, -3.92140420424278,
-4.8011229557052, -8.64427034627082, -3.11323607034871, -4.3673083843457,
-1.45023104534208, 0.395769745934938, -1.49394189431791, -3.54063822876105,
-4.36090193633662, -0.966958995958447, -2.43233048854294, -0.181367797683111,
0.826258942687981, 3.36833418895383, -6.8991417494414, -1.15773470862185,
-1.75360705873163, 1.83790453304777, 2.11575746130393, -3.82025172988123,
0.679651741170909, -4.64628184041103, -6.91923314565111, 0.550274303541761,
0.104011128328036, -0.895257855280075, -0.801630235696042, 2.27958927430356,
2.98003963398985, 3.41649824319921, 1.56559818977215, -2.20923132476973,
0.552658760232765, 0.15158829140461, -4.75454688546242, -0.595460561248954,
-2.53729443345183, -0.826010503400985, -5.20578683534568, -2.78364193219594,
-3.62503323095109, 3.37820215582788, -2.53645164034493, -1.76051141957494,
-1.0256290530567, 1.94178279643985, 0.261239031590387, 0.00321585342072063,
2.87814873140354, -2.26732156613212, 2.65097224867168, -4.16746046231376,
1.64816233695592, 3.50505415841016, 2.83685877611882, 1.66353660199615,
2.27900517713667, 5.47721995923733, -5.31044894311933, 7.30753839733595,
5.50143585044911, -1.25129055380416, -2.41051058119916, 3.69266303212359,
2.28752278841533, -0.275687673398348, 5.74597173218469, 6.5773422259343,
3.72096844335478, 2.05388534852328, 5.41063696868948, 0.526467452167141,
1.60445671702256, -1.80394989627014, 1.56432488418924, 5.95370989889123,
7.94953250403525, 4.09121878799004, 2.11516919787794, -2.12808005361608,
6.77215849921842, 9.53718510298556, 4.16562173164636, 10.4573226478082,
7.703077796612, 7.55811710979136, 4.47194951592662, 10.2104312432178,
11.3454383477984, 0.997649090931488, 4.84898050707927, 10.8819209584302,
8.06296236341084, 11.3317616787558, 7.51878628894305, 7.87729934765305,
11.9108509727303, 6.77401202490232, 5.36297357453455, 10.6362047038983,
8.68979831512869, 4.0465996534104, 11.9579904470733, 9.41141176380086,
10.5754750604254, 12.6944336852953, 7.61563466861022), Q75 = c(5.93775779359077,
26.4536084846094, 7.92690107568623, 16.195405687679, 3.47567054091916,
34.9690262666155, 15.5126126583077, 24.4425589002446, 29.7425859431597,
23.1420118192775, 26.827758017105, 18.6306368759596, 19.759179203689,
10.0667740183259, 30.9080218485755, 10.0628623899296, 21.1120424008512,
12.1232187464341, 14.9571040303508, 11.4927011052638, 16.1617172813173,
19.0606972964125, 8.39991659547325, 9.5080530252195, 10.2717546026802,
12.018391863395, 27.2666992661895, 12.5172584337237, 19.9658806224003,
6.90019918091751, 18.4119063276997, 23.2991253786256, 27.95161418973,
16.9477966472485, 26.3880458021082, 19.2178725103802, 5.58699033890406,
9.82525729279156, 6.22139350667344, 5.6625294221828, 8.18283315939774,
4.78856479855966, 4.91215612536983, 5.35278870440784, 15.7471499356884,
7.95473965312171, 7.58463611165082, 6.03119890210746, 9.88624343762245,
6.66377352843609, 6.92675024060609, 7.20403099201013, 6.96877369392089,
17.7034248870798, 6.22890341708267, 6.1624397247754, 23.3856864094132,
7.13518162203812, 6.96344109315883, 7.69414570220079, 18.0859103957135,
7.52300478408242, 10.1635801549871, 10.021556657451, 8.51746254314866,
7.83000625461296, 15.4938419153615, 8.6844260972191, 8.07596479745038,
13.1423674521087, 8.04161364299224, 10.7442773622841, 8.58410892324644,
9.08436532340561, 8.84748510783176, 9.27529549461203, 9.01978932806698,
9.99776533859531, 9.61123990151036, 11.2228855544025, 10.3285714984086,
10.7107229417799, 11.452541129334, 11.9951421202043, 11.3568792509498,
11.139621487692, 12.957244784325, 13.1010906952192, 11.8445972599726,
12.8124554609003, 12.1817389611984, 13.4529860098547, 13.1808997426024,
12.568956945967, 13.9405958892683, 14.4445923505263, 14.5816203429081,
12.798362023978, 13.7926596005317, 14.3284196983115, 14.3967490595795,
14.3699332949429, 13.8061418130819, 15.4045229902535, 15.328632395916,
15.5928587109464, 15.5111381098579, 15.7167488979248, 16.4121827249844,
16.7700564366026), fit = c(1.3157822724014, 1.44491806546299,
1.67963756121542, 1.96834398237369, 2.32222986513481, 2.73223146146706,
3.16143742264514, 3.74278329406317, 4.4673163398484, 5.08529278937518,
5.58735598987316, 6.01592790788482, 6.19893270175371, 6.0219082198616,
5.64253432163072, 5.29694818196536, 4.89670493804841, 4.35145910275626,
3.89449691453349, 3.48150649031492, 3.06858491643756, 2.88963188544926,
3.13399806321574, 3.62311989322663, 4.03902573446563, 4.40598627768245,
4.84291047423098, 5.1737840740012, 5.3972440468493, 5.5747020603732,
5.62430591107552, 5.42843052467024, 5.07513358262307, 4.79108701506415,
4.59907825712695, 4.39731440509327, 4.22559688081583, 4.10100609028878,
4.00444369172723, 3.92144298531529, 3.82259220819525, 3.72499526558926,
3.68395895980124, 3.69588308031619, 3.73924432798967, 3.84246487218137,
4.07884774763199, 4.41108295888359, 4.70167312999791, 4.95537881350854,
5.2206483181831, 5.42551590243433, 5.52148736399275, 5.55736071284688,
5.60710852579646, 5.65757759073701, 5.68911425674423, 5.76594044238814,
5.93786454015275, 6.15175825295678, 6.31743846502224, 6.40077523837882,
6.45704948591979, 6.53019436816257, 6.59356685208809, 6.63353784524384,
6.71356141899707, 6.88849022040772, 7.11437487009308, 7.30646639975639,
7.43724432723552, 7.55279324817994, 7.67877181101032, 7.76924002146674,
7.83161170884946, 7.97157625691941, 8.25223488219952, 8.60947602940562,
8.95816992458796, 9.34076728750423, 9.77554331222275, 10.1411049362597,
10.3842988541376, 10.5696053585185, 10.7520817841281, 10.9357595672387,
11.0970528791622, 11.2495931571849, 11.3764752236255, 11.4864715266717,
11.6317299424136, 11.8381584436134, 12.0667779318613, 12.2724056764894,
12.462010561811, 12.6517333832877, 12.8101492769744, 12.9055352762602,
12.9678598772259, 13.0582354099638, 13.1489397497677, 13.2204738414797,
13.346284619515, 13.6054940294766, 13.9436193637562, 14.2337005769519,
14.5449448398809, 14.8895799498019, 15.0551768009747, 15.0689572800127
), firstder = c(0.0542499277820437, 0.193160412687084, 0.264645386746196,
0.318230646770668, 0.390583391620104, 0.410606699200811, 0.484714112557398,
0.683182658658343, 0.699350916534123, 0.546311900646561, 0.476582322984034,
0.33921923563074, 0.000346679118119919, -0.32275830659655, -0.377372654859586,
-0.342379980870621, -0.492111485610006, -0.524917784293232, -0.414059192641829,
-0.430018688265099, -0.343482693656914, 0.0295127267198723, 0.42189373253822,
0.482044173095213, 0.364522990904745, 0.40991488301477, 0.40715895907959,
0.264020778627613, 0.200548459021332, 0.136695124879259, -0.0667758528503706,
-0.308783766357995, -0.344835056787729, -0.22338628389576, -0.19056674389956,
-0.195775242472453, -0.146360055189657, -0.107867992742261, -0.0856184200473131,
-0.0883963049921002, -0.106496806989568, -0.0747428483921662,
-0.0103234284849929, 0.028493059030597, 0.0620691939868203, 0.163240621281308,
0.304123951137378, 0.325609827601989, 0.261609166722046, 0.261432729205552,
0.249586474110962, 0.150199026157553, 0.0521536950613295, 0.0370628072573624,
0.0565243651980056, 0.0371337817771211, 0.0409727028064402, 0.124422569131023,
0.207609809433488, 0.20232516927351, 0.121600832063498, 0.058433044321534,
0.0638003776220697, 0.0745713396178918, 0.0471802520722933, 0.0467708263829785,
0.126045851395065, 0.213953247074989, 0.220308792495525, 0.158550331399022,
0.11422743390592, 0.123806714974779, 0.114997378074604, 0.0651990840907102,
0.0828996021118185, 0.210617558388392, 0.336478451788591, 0.356675237198802,
0.354610868118913, 0.419333862640583, 0.419974858146042, 0.301270480481834,
0.201419853206041, 0.17882844049566, 0.186628379656891, 0.172934534594114,
0.156583940148236, 0.142289490196014, 0.11234075824169, 0.119081439314575,
0.177295034391252, 0.226764155293772, 0.22057696671022, 0.193643700730051,
0.190744241252391, 0.181381161962744, 0.127949080877661, 0.0681406193708671,
0.0729227267768433, 0.0983314755975622, 0.0766175682172481, 0.0819140886989596,
0.188151474480757, 0.320764600927798, 0.32011829707578, 0.283266397091015,
0.351814702578002, 0.276441515194414, 0.0724489974588587, -0.0273030060468944
), secondder = c(0.172623240328004, 0.105197729482076, 0.0377722186361492,
0.0693983014127931, 0.0753071882860794, -0.0352605731246656,
0.18347539983784, 0.213461692364051, -0.181125176612492, -0.124952855162631,
-0.0145063001624228, -0.260219874544165, -0.417525238481075,
-0.228684732948264, 0.119456036422192, -0.0494706884442619, -0.249992321034509,
0.184379723668058, 0.037337459634748, -0.0692564508812885, 0.242328440097658,
0.503662400655915, 0.281099610980781, -0.160798729866795, -0.0742436345141417,
0.165027418734192, -0.170539266604553, -0.1157370942994, -0.0112075449131641,
-0.116499123370982, -0.290442832088276, -0.193572994926973, 0.121470414067507,
0.12142713171643, -0.055788051724031, 0.0453710545782453, 0.0534593199873461,
0.0235248049074466, 0.0209743404824492, -0.0265301103720232,
-0.00967089362291196, 0.0731788108177152, 0.0556600289966314,
0.0219729460345484, 0.0451793238778984, 0.157163530711077, 0.124603129001063,
-0.081631376071841, -0.0463699456880455, 0.0460170706550578,
-0.0697095808442372, -0.129065315062581, -0.0670253471298663,
0.0368435715219322, 0.0020795443593542, -0.0408607112011232,
0.0485385532597613, 0.118361179389404, 0.0480133012155273, -0.058582581535485,
-0.102866092884539, -0.0234694825993884, 0.0342041492004599,
-0.0126622252088158, -0.0421199498823812, 0.0413010985037516,
0.117248951520421, 0.0585658398394289, -0.045854748998357, -0.0776621731946507,
-0.0109836217915529, 0.0301421839292724, -0.0477608577296227,
-0.0518357302381656, 0.0872367662803821, 0.168199146272765, 0.0835226405276321,
-0.0431290697072093, 0.039000331547431, 0.0904456574959092, -0.0891636664849909,
-0.148245088843424, -0.0514561657081618, 0.00627334028739845,
0.00932653803506511, -0.036714228160621, 0.00401303926886598,
-0.0326019391733094, -0.0272955247353401, 0.0407768868811118,
0.0756503032722406, 0.0232879385327998, -0.0356623156999039,
-0.0182042162604343, 0.012405297305115, -0.0311314558844096,
-0.0757327062857556, -0.0438842167278324, 0.0534484315397847,
-0.00263093389834701, -0.0407968808622812, 0.0513899218257041,
0.161084849737891, 0.10414140315619, -0.105434010860225, 0.0317302108906947,
0.105366400083279, -0.256112774850456, -0.151872260620654, -0.0476317463908522
)), row.names = c(NA, -110L), class = c("tbl_df", "tbl", "data.frame"
))
If the problem is to find where the fit column starts rising then fit a curve made up of a horizontal line segment followed by a sloped line segment (red in the graph) and report the changepoint (Date0 and dashed line in graph).
# calculate starting values, st
fm0 <- lm(fit ~ Date, data, subset = seq(to = nrow(data), length = 20))
st <- c(mean(data$fit[1:20]), coef(fm0))
names(st) <- c("a0", "a", "b")
fm <- nls(fit ~ pmax(a0, a + b * as.numeric(Date)), data, start = st)
# solve a0 = a + b * Date0 for Date0 using calculated a0, a and b
Date0 <- with(as.list(coef(fm)), .POSIXct((a0 - a)/b))
plot(fit ~ Date, data, ylab = "")
lines(fitted(fm) ~ Date, data, col = "red")
abline(v = Date0, lty = 2)
Date0
## [1] "2017-03-21 07:53:56 EDT"
I've got a question kinda basic. It's the first time I use parallel package in R
I'm running a function you could find here. Well, this is not exactly the function but is very similar.
So my function is running on the WTI oil price dataset (see down below) and since p_max and q_max are 12 it takes like 3 min on my PC.
So I am trying to parallelize this process:
this is my code so far:
n.cores <- round(detectCores()/2)
cl <- makeCluster(n.cores)
clusterEvalQ(cl, {library(dplyr); library(forecast)})
clusterExport(cl, c("wti_", "which_arima"))
start_time <- Sys.time()
a <- parApply(cl = cl,
wti_,
function(x)
wti_models <- which_arima(x,p_max = 12, d_max = 1, q_max = 12, expl = F))
end_time <- Sys.time()
stopCluster(cl)
end_time - start_time
this gets me an error
So this is what I know:
I am creating a new environment only with forecast and dplyrpackages
In this environment I am using the wti_ data frame and the which_arima function.
The desired result as you could see in the link is a dataframe, so my question is: how can I apply the function over a dataset, but using the parallelization.
Like how do I say to R, "apply this function, with this data, these libraries, and return my dataframe"
data:
structure(c(3.59131383137997, 3.58693674637687, 3.50782953019081,
3.43581773710991, 3.40722540614203, 3.42200436261882, 3.48904753923734,
3.47452426198165, 3.46675753126697, 3.46568380114955, 3.49807117713751,
3.51687930816857, 3.46335166796688, 3.41285698665032, 3.3790196588941,
3.43353497596237, 3.38675587458917, 3.41877857802065, 3.4351468995404,
3.43645947831124, 3.40956358119916, 3.38513513606487, 3.40276099733318,
3.40963956656788, 3.42328064788982, 3.44117985614171, 3.44875660306423,
3.43353497596237, 3.40231141639508, 3.3889354116122, 3.34201790329997,
3.34511517255433, 3.34481173886808, 3.32549686098232, 3.33946614154227,
3.31876553349471, 3.2719744390575, 3.34603588846443, 3.36637904646734,
3.37366507547086, 3.30585112515203, 3.28962252754128, 3.27225558783058,
3.28979015878231, 3.2868499768523, 3.33280253315514, 3.39774845550964,
3.34281846838198, 3.15856777800092, 2.77168686901602, 2.56117445699168,
2.56878239388438, 2.72383440052745, 2.58909523968371, 2.42867520384351,
2.69982721445759, 2.67193554872065, 2.67205375315245, 2.72614311872445,
2.82219484828974, 2.95059244815988, 2.91370487551786, 2.93858161230468,
2.93740122860658, 2.95149846325297, 2.98463470799191, 3.04069683959108,
2.97765699046636, 2.93763871445365, 2.95889654733423, 2.93807652956505,
2.88480844101773, 2.86942904105267, 2.85571761786751, 2.80852848405648,
2.8935585907424, 2.84269560340417, 2.79881577066578, 2.72282979080091,
2.71603502468445, 2.63791866344239, 2.59536113236419, 2.64062094528629,
2.82526704732671, 2.91780262533689, 2.91651781689448, 2.9913643118826,
3.0536937288849, 2.98445455093627, 2.98213782779332, 2.96514325822283,
2.89244417511265, 2.94275381512042, 2.97141235526606, 2.98724555337152,
3.08337497999601, 3.14759071936976, 3.13289696812015, 3.03905214266832,
2.93471666323591, 2.90128976767023, 2.8107562110377, 2.90783591296553,
3.27335143243915, 3.48472208624227, 3.55142983569527, 3.47663084589964,
3.33547898589394, 3.23423411107465, 3.06018030053868, 3.01423697354859,
3.04650806922403, 3.04258229255412, 2.99208815864649, 3.04771441224973,
3.04928664660562, 3.05142517042555, 3.1169976722344, 3.11951352194927,
3.00707722594537, 2.96128773727663, 2.98175833876288, 2.96532117334658,
3.0197098760693, 3.03074783490711, 3.09323187529891, 3.0616035244104,
3.03068145877458, 3.05371088570641, 3.04800480074745, 3.01464452755963,
2.99937500558328, 2.97449796901343, 3.03554851334902, 3.03659815370675,
3.0221759685645, 2.98195767073768, 2.93870826986599, 2.86679047972241,
2.86234614395537, 2.82942512981342, 2.86936310091189, 2.8199618739421,
2.77816257813924, 2.73548106854293, 2.72856244238227, 2.70795757728122,
2.80272019032947, 2.86822890540934, 2.93347265581204, 2.96006710519097,
2.88160396055978, 2.82656389753239, 2.84142832358034, 2.89747555394281,
2.87668303357381, 2.91724691540316, 2.95671039989371, 2.94344780195585,
3.00076613908048, 2.96379370011001, 2.90417628632361, 2.83960873830006,
2.86012144893326, 2.85926128609918, 2.82313164392855, 2.88020011734877,
2.96955328218305, 2.96606354895414, 2.98543577920877, 3.07562984880824,
3.15570736118902, 3.03455012085685, 2.99554751470324, 3.0435097809677,
3.06165227245572, 3.14193917796638, 3.18716548045293, 3.16534947422127,
3.26569653678997, 3.24350276497619, 3.12060453015829, 3.0531677335756,
2.97682814858604, 3.00688440226154, 2.92397891361979, 2.94209486278654,
2.94852384019642, 2.94200037294792, 3.02516530877763, 2.99127781689946,
2.9294612389212, 2.83139712537276, 2.79796201081782, 2.71543806099047,
2.7407142468292, 2.6875866101351, 2.60423924740296, 2.62901526614014,
2.56740818145639, 2.67394022697303, 2.63930867205784, 2.56343130659516,
2.45978922964202, 2.55235919157821, 2.52305899568833, 2.70863993827665,
2.86457105386915, 2.86261091310512, 2.87064745175996, 2.98121592968395,
3.02833128142963, 3.13942766555372, 3.09041079353602, 3.21442136821367,
3.29527067657245, 3.33321806429822, 3.41425820111181, 3.42204439492442,
3.26347876143116, 3.347641908834, 3.44778510690982, 3.38228340695309,
3.4158576241713, 3.49022619861987, 3.4685900443404, 3.53868951455743,
3.38057910897592, 3.41351461129283, 3.4240289650859, 3.32815366177433,
3.32284599134973, 3.33998170424571, 3.30277100038479, 3.25697583739984,
3.28416630900999, 3.22973411077222, 3.06968747828722, 2.97658284762748,
2.99486130729813, 3.00742083087148, 3.06857117342194, 3.21715730661809,
3.2789612975563, 3.28371073536068, 3.22541728419383, 3.27500077441177,
3.31732265679795, 3.35736068318668, 3.33283267360966, 3.27049196549064,
3.41631064824351, 3.52240767637354, 3.61534075809335, 3.52954642796643,
3.35163665031884, 3.32314665714096, 3.41099388869206, 3.40786497905298,
3.42461901851098, 3.30951378818839, 3.38255745851344, 3.43852131426105,
3.50368460070908, 3.56091764269564, 3.58492322635242, 3.62714661280319,
3.61447455478338, 3.68220458841285, 3.62480591875187, 3.69120858563579,
3.77693846464848, 3.79435311491179, 3.94561880187821, 3.88202371197633,
3.80061460739448, 3.87374132950349, 3.90774155853708, 4.01561092655659,
3.98114594176238, 3.89451764244874, 4.01868127043691, 4.05385010954482,
4.14567914885236, 4.14985582914602, 4.10367767174555, 4.06687407130807,
4.11863663720755, 4.20922906995073, 4.15822897798113, 4.16489749696743,
4.25162605091732, 4.24796372995892, 4.24812466259185, 4.29150230020906,
4.2630054327201, 4.12327520054203, 4.04569209767451, 4.08131048141254,
4.16122019539675, 4.02084951107286, 4.11908513961168, 4.12784213744363,
4.16944548583258, 4.13649191891535, 4.19841321547244, 4.28797065568862,
4.25388037613002, 4.34807914470834, 4.42392192162843, 4.55286074143255,
4.54887319905847, 4.5597691897791, 4.59523020910027, 4.68196749724852,
4.73555825781743, 4.81464312907837, 4.88370149630503, 4.87517492444396,
4.73107073687703, 4.61100598816616, 4.3094825827762, 4.04975401011471,
3.75832555503529, 3.75885483169272, 3.7047332255135, 3.8846870473905,
3.91975172914305, 4.06587924399579, 4.22947702327889, 4.14275318357282,
4.23549897665209, 4.20773532115613, 4.29842497077528, 4.35820833774186,
4.34473021442986, 4.38868400894379, 4.37347426469321, 4.42105341418424,
4.44804171845561, 4.28663516918525, 4.30859773778422, 4.3173240354623,
4.31059519857912, 4.28859054681792, 4.37618571675163, 4.43518826020675,
4.52438659257285, 4.52064662340031, 4.53192251707477, 4.65743120826524,
4.71166719869542, 4.60414256551792, 4.55339702945559, 4.55985555753629,
4.43005081750458, 4.41664247367781, 4.42975186247436, 4.57754526419444,
4.62480609037661, 4.63546694284186, 4.66438801428565, 4.68837471206333,
4.64902787588347, 4.53674738508635, 4.39749965456486, 4.45820582366945,
4.51644331685982, 4.515907622083, 4.46512546285458, 4.46376481745648,
4.51388462404826, 4.57861240050054, 4.59434364751533, 4.55517543186956,
4.53355114792002, 4.53755358906122, 4.54855862986897, 4.63304546972625,
4.6405546157846, 4.63299082099574, 4.58085459388592, 4.54432073943083,
4.61755223285532, 4.57983298789016, 4.64976302955103, 4.63437124427453,
4.63731190211323, 4.60982471864015, 4.64264901041142, 4.61613606110031,
4.54027309727139, 4.50218653982855, 4.40625994180295, 4.32979382573924,
4.11603648165941, 3.88330671224587, 3.96146854482242, 3.89012438371581,
4.00865426030002, 4.06832867298344, 4.07741121519592, 3.91185269043218,
3.72991337610464, 3.78383706950744, 3.80367233828873, 3.75576253590561,
3.65121682739862, 3.47868744897561, 3.45143296652514, 3.65503237854251,
3.72415106324801, 3.83061175389603, 3.87311075168319, 3.78173952982106,
3.77306583078007, 3.77832133572214, 3.88051312207398, 3.82081322094682,
3.98553794729897, 3.98843449509049, 4.01513010556259, 3.92710476842519,
3.94455649433009, 3.86778919512713, 3.79683870390584, 3.82466269416906,
3.84380020173594, 3.87584146117727, 3.91343871827202, 4.03845560314504,
4.0935099318552, 4.18114436146966, 4.16719192787768, 4.16283516968298,
4.20604659334379, 4.23443488412323, 4.19882940452703, 4.24241357979034,
4.19133502159757, 4.21871499991311, 4.22984507672438, 4.03880858585256,
3.92490130849051, 3.96940094923682, 4.04374304044607, 4.08654312078282,
4.16840485009025, 4.09447286457478, 3.98790356609508, 4.0341224802885,
3.97639424075332, 4.00939793877714, 3.95930614779089, 4.04566696145638,
4.12510760047058, 4.07956358085351, 3.95988657890026, 3.42070660208276,
2.81612685060959, 3.33823253465507, 3.63185545042593, 3.68944560656258,
3.71817890679241, 3.64605336714439, 3.64775241750963, 3.71757173717322,
3.88531280554538, 3.98059581620004, 4.11587323591221, 4.15628091394419,
4.13400107484973, 4.16337807059311, 4.25442327230134, 4.26502445234263,
4.18750359928602, 4.23776050752447, 4.36908452896118, 4.37328735734901,
4.30421888163429, 4.44771621423169, 4.55627784876546, 4.75090506053049,
4.7389429012291, 4.70472426847878, 4.69593575555166, 4.65481860133761,
4.60670337428884, 4.56234409901854, 4.53504073166887, 4.53416310882428,
4.53391160984426), .Dim = c(492L, 1L), .Dimnames = list(NULL,
"x"), .Tsp = c(1982, 2022.91666666667, 12), class = "ts")
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"))
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="")
I'm trying to plot a simple graph that show the increase in wealth for two different investment strategies. When using the standard graph from R it works, but when I try to use ggplot2 I get these weird spikes in the lines.
Does any have any idea what could be causing this?
I've tried to simply the code as much as possible:
For the standard graph
ind.ts = ts(cbind(ind.passive,ind.active), start=c(insample.endstart,1),frequency=12)
plot(log(ind.ts),type="lines", col=c("blue","red"))
legend(x="topleft", legend=c("Passive","Active"), col=c("blue","red"), lty=1)
For the ggplot graph
testers=data.frame(ind.ts)
ggplot(testers, aes(date)) +
geom_line(aes(y = log(ind.passive), colour = "Passive",size="1")) +
geom_line(aes(y = log(ind.active), colour = "Active",size="1"))
The Ind.ts data set
structure(c(1, 1.026669, 1.066102329621, 1.09764083483818, 1.13073909657189,
1.17422279926966, 1.201650295415, 1.24229131005623, 1.24436842112664,
1.29675757602449, 1.29281154272065, 1.34840890311535, 1.37447769243928,
1.42187380670767, 1.43432089001159, 1.44828830683852, 1.47037760009442,
1.50663270057995, 1.51269991046518, 1.44617893190248, 1.47609892782461,
1.55880475075062, 1.60230787373457, 1.72267003659376, 1.6884336922865,
1.7947931958647, 1.80827747714523, 1.73407842742553, 1.83823238001199,
1.94879470474019, 2.03637158997651, 2.19836698633073, 2.07500122615881,
2.18823196806907, 2.11573803119891, 2.21303659177769, 2.25083083069207,
2.27667036862841, 2.44006700098487, 2.56495939036328, 2.59127330874902,
2.54554769994283, 2.64902166839781, 2.62135793511473, 2.24229384954953,
2.38534322797539, 2.58003017155629, 2.73574015247005, 2.89313822640227,
3.01496249083961, 2.92082933195062, 3.03735873897812, 3.15584610338566,
3.08028252428619, 3.25121048184135, 3.15027015001163, 3.13383204036887,
3.04763285626648, 3.24152630621501, 3.30661615444381, 3.5011906754359,
3.32628169286315, 3.26271977599422, 3.58162126961968, 3.47465973202375,
3.4018482373392, 3.48660188432426, 3.43296051433394, 3.64465402445034,
3.45302176049876, 3.43920276741325, 3.16710336206381, 3.18321124976327,
3.29673729577483, 2.9957319937214, 2.80662641161774, 3.02543381329387,
3.04403720581181, 2.97111425050939, 2.94227958670819, 2.75683358891715,
2.53472102032527, 2.58379068455775, 2.78122846592754, 2.80549468429276,
2.76500859050373, 2.71079783207832, 2.81360212906206, 2.64401226073284,
2.62324090041252, 2.43641368348514, 2.24723834303094, 2.26148583412576,
2.01595857860056, 2.19346574740491, 2.32192606890168, 2.18514140418268,
2.12856372294559, 2.09571359900937, 2.1165869064555, 2.29149953181808,
2.41150994529845, 2.44221328992199, 2.48518647497146, 2.53301388868229,
2.50620193667058, 2.64742390960003, 2.6698343529948, 2.80897010046677,
2.86115795596334, 2.89979789415863, 2.85611823847891, 2.81197121886675,
2.84980347964538, 2.90496997540435, 2.80930350417434, 2.81972040156782,
2.85016210302314, 2.89418855702854, 3.00999951213804, 3.11183381563269,
3.03729294841303, 3.09892873421517, 3.04396923311387, 2.98710484387007,
3.08097760069353, 3.08499827646243, 3.20047593194697, 3.16912086924169,
3.19575099190593, 3.14371138275373, 3.25904157854143, 3.26071346687123,
3.3485896948034, 3.35499219829987, 3.3971510302637, 3.44342702159796,
3.34200432210381, 3.3473849490624, 3.36955802696499, 3.4464479715823,
3.53637269205683, 3.65311189099431, 3.71864871831875, 3.7710110109214,
3.82954087282191, 3.75144504580245, 3.79450413203817, 3.96444479409563,
4.09921609487092, 4.03197255405065, 3.90887240000293, 3.96507025849778,
4.11298323942078, 4.18000430130714, 4.00202389816178, 3.973681564915,
3.73688988046171, 3.6132997214452, 3.59812747591486, 3.77562310430174,
3.82238042082541, 3.50029900180582, 3.47233161278139, 3.52122551422096,
3.20811814149644, 2.67119786498117, 2.47785656351383, 2.50381211101664,
2.29590056094204, 2.04999813136234, 2.23149881591877, 2.44744541933286,
2.58359925545577, 2.59022877114527, 2.78828284344458, 2.88774646903593,
2.99667515359443, 2.94310059519847, 3.1174675330616, 3.17829867703423,
3.06610473373492, 3.15882374088307, 3.34981254190434, 3.40448483240076,
3.13064849939144, 2.96722864772321, 3.17659630110655, 3.0311907820197,
3.30193068028814, 3.42901538831107, 3.42659107443153, 3.65581631094671,
3.74411158648869, 1, 1.026669, 1.066102329621, 1.09764083483818,
1.13073909657189, 1.17422279926966, 1.201650295415, 1.24229131005623,
1.24436842112664, 1.29675757602449, 1.29281154272065, 1.34840890311535,
1.37447769243928, 1.42187380670767, 1.43432089001159, 1.44828830683852,
1.47037760009442, 1.50663270057995, 1.51269991046518, 1.44617893190248,
1.47609892782461, 1.55880475075062, 1.60230787373457, 1.72267003659376,
1.6884336922865, 1.7947931958647, 1.80827747714523, 1.73407842742553,
1.83823238001199, 1.94879470474019, 2.03637158997651, 2.19836698633073,
2.07500122615881, 2.18823196806907, 2.11573803119891, 2.21303659177769,
2.25083083069207, 2.27667036862841, 2.44006700098487, 2.56495939036328,
2.59127330874902, 2.54554769994283, 2.64902166839781, 2.62135793511473,
2.24229384954953, 2.2509042579318, 2.25833224198298, 2.39462710945113,
2.53239958556629, 2.63903386731532, 2.55663795191, 2.6586375796394,
2.76235103162114, 2.69620929852, 2.84582464870417, 2.75747033083585,
2.74308185064955, 2.66763064126559, 2.83734797029354, 2.89432191753704,
3.06463539645259, 2.91153540595201, 2.85589887587967, 3.13503728790702,
3.04141253434097, 2.97767973468385, 3.05186564759377, 3.00491269460554,
3.19021063591839, 3.02247255089243, 3.01037661574376, 3.02584995154869,
3.04040428981563, 3.05344762421894, 3.06587515604951, 3.07715757662378,
3.08709679559627, 3.09641982791897, 3.10543040961822, 3.1145293207184,
3.12325000281641, 3.13012115282261, 3.13575537089769, 3.14064714927629,
3.14507546175677, 3.14941566589399, 3.15395082445288, 3.15865021118131,
2.96826256970236, 2.97253686780273, 2.97675787015501, 2.98092533117323,
2.98494958037031, 2.98900911179961, 2.99295460382719, 2.99603734706913,
2.99900342404273, 3.00194244739829, 3.00488435099674, 3.00770894228668,
3.01053618869243, 3.16820398996663, 3.20854156316688, 3.26499906051237,
3.32783396743193, 3.29260884488666, 3.47814406068718, 3.5075865501609,
3.69038091563598, 3.75894450266758, 3.80970904817611, 3.75232340078343,
3.69432373797752, 3.74402716954827, 3.81650404749639, 3.69081893620424,
3.70450449281968, 3.74449832332416, 3.80233958892455, 3.95449020757537,
4.08827852027806, 3.99034789660332, 4.07132402646909, 3.99911909485966,
3.92441155104859, 4.04774010845184, 4.05302240929337, 4.20473514411804,
4.16354135391111, 4.19852759190803, 4.1301587686014, 4.28167777318631,
4.28387427388395, 4.39932468556512, 4.40773619436392, 4.4631238073823,
4.52392047988646, 4.39067292607189, 4.39774190948286, 4.42687255189128,
4.52788935665288, 4.64603104574667, 4.79940117659781, 4.88550243370598,
4.95429519347499, 5.03119080917292, 4.92858973500145, 4.9851600879798,
5.20842546768007, 5.38548589145385, 5.29714238089044, 5.13541532685947,
5.20924719301373, 5.40357295030192, 5.49162417152709, 5.25779630592764,
5.22056059248906, 4.90946738678263, 4.91815714405724, 4.9233212090585,
4.92863839596428, 4.93573563525447, 4.94338602548911, 4.95010903048378,
4.95718768639737, 4.96184744282258, 4.96462607739057, 4.96542041756295,
4.96556938017547, 4.96611559280729, 4.9673571217055, 4.9682512459874,
4.96889711864938, 4.96964245321718, 4.97038789958516, 4.9711334577701,
5.14846373047568, 5.34266893085295, 5.24715269570716, 5.55802550431702,
5.66647925598276, 5.46645253824657, 5.63175806300315, 5.97226541900844,
6.06973876291208, 5.58152539525601, 5.29016976962365, 5.2908574916937,
5.04867378086891, 5.04933010846042, 5.24366872567485, 5.2399614518858,
5.59049391317115, 5.72551552216206), .Dim = c(194L, 2L), .Dimnames = list(
NULL, c("ind.passive", "ind.active")), .Tsp = c(1995, 2011.08333333333,
12), class = c("mts", "ts", "matrix"))
The date data set
structure(c(1995.1, 1995.2, 1995.3, 1995.4, 1995.5, 1995.6, 1995.7,
1995.8, 1995.9, 1995.1, 1995.11, 1995.12, 1996.1, 1996.2, 1996.3,
1996.4, 1996.5, 1996.6, 1996.7, 1996.8, 1996.9, 1996.1, 1996.11,
1996.12, 1997.1, 1997.2, 1997.3, 1997.4, 1997.5, 1997.6, 1997.7,
1997.8, 1997.9, 1997.1, 1997.11, 1997.12, 1998.1, 1998.2, 1998.3,
1998.4, 1998.5, 1998.6, 1998.7, 1998.8, 1998.9, 1998.1, 1998.11,
1998.12, 1999.1, 1999.2, 1999.3, 1999.4, 1999.5, 1999.6, 1999.7,
1999.8, 1999.9, 1999.1, 1999.11, 1999.12, 2000.1, 2000.2, 2000.3,
2000.4, 2000.5, 2000.6, 2000.7, 2000.8, 2000.9, 2000.1, 2000.11,
2000.12, 2001.1, 2001.2, 2001.3, 2001.4, 2001.5, 2001.6, 2001.7,
2001.8, 2001.9, 2001.1, 2001.11, 2001.12, 2002.1, 2002.2, 2002.3,
2002.4, 2002.5, 2002.6, 2002.7, 2002.8, 2002.9, 2002.1, 2002.11,
2002.12, 2003.1, 2003.2, 2003.3, 2003.4, 2003.5, 2003.6, 2003.7,
2003.8, 2003.9, 2003.1, 2003.11, 2003.12, 2004.1, 2004.2, 2004.3,
2004.4, 2004.5, 2004.6, 2004.7, 2004.8, 2004.9, 2004.1, 2004.11,
2004.12, 2005.1, 2005.2, 2005.3, 2005.4, 2005.5, 2005.6, 2005.7,
2005.8, 2005.9, 2005.1, 2005.11, 2005.12, 2006.1, 2006.2, 2006.3,
2006.4, 2006.5, 2006.6, 2006.7, 2006.8, 2006.9, 2006.1, 2006.11,
2006.12, 2007.1, 2007.2, 2007.3, 2007.4, 2007.5, 2007.6, 2007.7,
2007.8, 2007.9, 2007.1, 2007.11, 2007.12, 2008.1, 2008.2, 2008.3,
2008.4, 2008.5, 2008.6, 2008.7, 2008.8, 2008.9, 2008.1, 2008.11,
2008.12, 2009.1, 2009.2, 2009.3, 2009.4, 2009.5, 2009.6, 2009.7,
2009.8, 2009.9, 2009.1, 2009.11, 2009.12, 2010.1, 2010.2, 2010.3,
2010.4, 2010.5, 2010.6, 2010.7, 2010.8, 2010.9, 2010.1, 2010.11,
2010.12, 2011.1, 2011.2), .Tsp = c(1995, 2011.08333333333, 12
), class = "ts")
The spikes are in your data, specifically in the crummy way the dates are stored. January, February, March 1995 are coded as 1995.10, 1995.20, 1995.30, but then October, November, and December are 1995.10, 1995.11, 1995.12. When you pass your time series to ggplot you maybe saw a warning like:
Don't know how to automatically pick scale for object of type ts. Defaulting to continuous
So ggplot just converted to numerics, giving October the same x value as January and inserting Nov and Dec before February, causing your spikes. Since your samples (as far as I checked) are spaced every month, you could add a new column to your data like this:
ind.df <- as.data.frame(ind.ts)
ind.df$date <- seq(as.Date('1995-01-01'), as.Date('2011-02-01'), by = "month")
Then, ggplot works best with long-format data, so we can melt it
library(reshape2)
ind.melt <- melt(ind.df, id.vars = "date")
ggplot(ind.melt, aes(x = date, y = value, color = variable) +
geom_line(size = 1)
And the spikes are gone.
One other note, in ggplot don't put anything inside aes() that isn't mapping to a data column. In your post, inside aes() you have size = "1". You don't need the quotes around 1, and since it applies to the whole layer you should put it outside of aes().
The following example illustrates that for a very simple example, the plots from the basic R plotting and ggplot2 are the same, i.e. basic plotting does not get rid of the spikes, nor does ggplot2 introduces spikes. You need to make your example more complete, i.e. provide us with a sample of your data that reproduces the issue you see.
x = 1:100
y = runif(100)
y[50] = 5
plot(x, y)
library(ggplot2)
qplot(x, y, geom = 'line')