I would like to group values of all other columns by the year in column yearqtr the following data
dput(narepurchasement)
structure(list(Date = structure(c(844128000, 852076800, 859852800,
867715200, 875664000, 883612800, 891388800, 899251200, 907200000,
915148800, 922924800, 930787200, 938736000, 946684800, 954547200,
962409600, 970358400, 978307200, 986083200, 993945600, 1001894400,
1009843200, 1017619200, 1025481600, 1033430400, 1041379200, 1049155200,
1057017600, 1064966400, 1072915200, 1080777600, 1088640000, 1096588800,
1104537600, 1112313600, 1120176000, 1128124800, 1136073600, 1143849600,
1151712000, 1159660800, 1167609600, 1175385600, 1183248000, 1191196800,
1199145600, 1207008000, 1214870400, 1222819200, 1230768000, 1238544000,
1246406400, 1254355200, 1262304000, 1270080000, 1277942400, 1285891200,
1293840000, 1301616000, 1309478400, 1317427200, 1325376000, 1333238400,
1341100800, 1349049600, 1356998400, 1364774400, 1372636800, 1380585600,
1388534400, 1396310400, 1404172800, 1412121600, 1420070400, 1427846400,
1435708800, 1443657600, 1451606400, 1459468800, 1467331200, 1475280000,
1483228800, 1491004800, 1498867200, 1506816000, 1514764800, 1522540800,
1530403200, 1538352000, 1546300800, 1554076800, 1561939200, 1569888000,
1577836800, 1585699200, 1593561600, 1601510400, 1609459200, 1617235200,
1625097600, 1633046400, 1640995200, 1648771200), class = c("POSIXct",
"POSIXt"), tzone = "UTC"), NetIssuance = c("-7450", "-13950",
"-14675", "-22875", "-25875", "-21675", "-17808", "-64840", "-111214",
"-6920", "-76700", "-26188", "-1", "27044", "-50630", "-10731",
"-83887", "-4850", "-14775", "-27350", "-1150", "-2644", "6357",
"-20316", "2098", "-10173", "-3438", "0", "-2055", "-0.802",
"-16823", "-32200", "-70730", "-43031", "-58722", "-90630", "-83784",
"-110795", "-116977", "-107859", "-137542", "-109583", "-149516",
"-162019", "-226618", "-84099", "-38612", "-73533", "-93475",
"-37950", "39311", "20920", "-62302", "-35987", "-35433", "-71238",
"-58295", "-59766", "-101392", "-133088", "-88329", "-49568",
"-99135", "-73428", "-77876", "-38256", "-73497", "-60269", "-105274",
"-101911", "-48493", "-80452", "-71090", "-116963", "-102404",
"-129399", "-104711", "-127487", "-136914", "-150658", "-80792",
"-89438", "-55464", "-119607", "-61042", "-122438", "-225035",
"-79778", "-190075", "-174006", "-46583", "-111504", "-124927",
"-95947", "-14946", "7398", "-67450", "-30403", "-133211", "-218291",
"-237670", "-227868", "-135084"), GrossIssuance = c(35393, 34426,
39963, 36586, 40630, 36993, 57637, 31110, 52737, 52487, 78711,
65846, 95574, 113349, 86067, 75480, 71906, 54552, 64094, 39824,
55322, 43624, 50257, 29329, 35664, 32098, 36084, 42285, 48634,
57955, 47497, 43892, 55599, 48385, 52197, 63692, 63159, 68401,
69557, 63825, 94723, 88627, 97967, 102944, 108022, 86316, 96002,
93730, 75885, 64674, 77307, 62616, 66705, 54873, 57173, 48392,
68703, 64334, 69966, 43637, 55198, 66678, 70380, 68331, 72198,
73702, 83784, 103945, 94138, 89471, 100239, 100418, 111302, 129933,
124281, 116589, 97678, 106734, 118234, 106262, 107965, 122679,
115625, 107485, 112226, 106358, 99560, 101952, 91526, 95447,
118912, 108570, 100615, 107853, 154908, 134115, 154227, 163567,
126579, 112180, 132474, 92327, 80342), GrossRetirement = c(42843,
48376, 54638, 59461, 66505, 58668, 75445, 95950, 163951, 59407,
155411, 92034, 96134, 86305, 136697, 86211, 155793, 59402, 78869,
67174, 56472, 46268, 43900, 49645, 33566, 42271, 39522, 42226,
50689, 58757, 64320, 76092, 126329, 91416, 110919, 154322, 146943,
179196, 186534, 171684, 232265, 198210, 247483, 264963, 334640,
170415, 134614, 167263, 169360, 102624, 37996, 41696, 129007,
90860, 92606, 119630, 126998, 124100, 171358, 176725, 143527,
116246, 169515, 141759, 150074, 111958, 157281, 164214, 199412,
191382, 148732, 180870, 182392, 246896, 226685, 245988, 202389,
234221, 255148, 256920, 188757, 212117, 171089, 227092, 173268,
228796, 324595, 181730, 281601, 269453, 165495, 220074, 225542,
203800, 169854, 126717, 221677, 193970, 259790, 330471, 370144,
320195, 215426), Repurchases = c(22263, 22638, 23514, 25005,
34369, 26643, 29082, 41095, 27253, 31805, 30779, 29350, 35972,
38084, 22859, 24761, 30152, 25245, 26623, 27689, 24038, 20954,
27243, 27314, 18885, 20208, 22000, 25993, 34329, 31567, 34011,
42358, 46643, 52980, 63201, 66599, 90778, 76295, 97243, 91990,
96248, 92541, 121025, 121251, 98213, 94359, 75799, 80943, 45745,
26459, 17862, 24888, 33600, 40277, 59624, 57199, 62624, 66172,
73022, 96186, 74495, 64511, 83483, 65770, 86040, 77135, 100169,
97375, 105120, 124551, 99652, 108215, 106062, 113685, 100343,
122057, 107005, 123418, 99546, 75010, 89025, 93073, 81638, 84879,
87762, 143170, 138764, 134874, 148169, 137193, 107400, 108922,
119371, 143785, 79929, 88312, 110984, 128796, 141252, 154680,
195502, 220050, 120000), MA = c(20579, 25738, 31124, 34456, 32136,
32025, 46364, 54855, 136698, 27602, 124632, 62684, 60162, 48221,
113837, 61450, 125641, 34157, 52246, 39486, 32434, 25314, 16657,
22331, 14681, 22063, 17522, 16233, 16360, 27191, 30309, 33735,
79686, 38436, 47718, 87723, 56166, 102901, 89291, 79694, 136016,
105669, 126458, 143711, 236427, 76055, 58816, 86320, 123615,
76166, 20134, 16809, 95407, 50583, 32982, 62430, 64373, 57928,
98336, 80539, 69032, 51735, 86032, 75988, 64033, 34823, 57112,
66838, 94292, 66831, 49079, 72655, 76330, 133211, 126342, 123931,
95384, 110803, 155602, 181911, 99732, 119044, 89451, 142213,
85506, 85626, 185832, 46856, 133432, 132260, 58095, 111152, 106172,
60015, 89925, 38404, 110693, 65174, 118539, 175791, 174642, 100146,
95426), GDP = c(8259.771, 8362.655, 8518.825, 8662.823, 8765.907,
8866.48, 8969.699, 9121.097, 9293.991, 9411.682, 9526.21, 9686.626,
9900.169, 10002.179, 10247.72, 10318.165, 10435.744, 10470.231,
10599, 10598.02, 10660.465, 10783.5, 10887.46, 10984.04, 11061.433,
11174.129, 11312.766, 11566.669, 11772.234, 11923.447, 12112.815,
12305.307, 12527.214, 12767.286, 12922.656, 13142.642, 13324.204,
13599.16, 13753.424, 13870.188, 14039.56, 14215.651, 14402.082,
14564.117, 14715.058, 14706.538, 14865.701, 14898.999, 14608.208,
14430.901, 14381.236, 14448.882, 14651.248, 14764.611, 14980.193,
15141.605, 15309.471, 15351.444, 15557.535, 15647.681, 15842.267,
16068.824, 16207.13, 16319.54, 16420.386, 16629.05, 16699.551,
16911.068, 17133.114, 17144.281, 17462.703, 17743.227, 17852.54,
17991.348, 18193.707, 18306.96, 18332.079, 18425.306, 18611.617,
18775.459, 18968.041, 19148.194, 19304.506, 19561.896, 19894.75,
20155.486, 20470.197, 20687.278, 20819.269, 21013.085, 21272.448,
21531.839, 21706.532, 21538.032, 19636.731, 21362.428, 21704.706,
22313.85, 23046.934, 23550.42, 24349.121, 24740.48, 25248.476
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-103L))
I don't know how exactly...
We may use
library(dplyr)
library(zoo)
library(lubridate)
narepurchasement %>%
mutate(yearqtr = as.yearqtr(Date)) %>%
group_by(year = year(yearqtr))
Related
I am having some trouble with visualizing cumulative returns in R. For some reason I can't change the axis labels and line type in chart.TimeSeries or chart.CumReturns from the PerformanceAnalytics package. Changes in the line type do appear in the legend.
My code:
ret_compare <- cbind(conventional.scaled.returns.monthly, conditional.scaled.returns.monthly, unscaled.returns.monthly)
cum_ret_compare <- cumprod(ret_compare+1)
chart.TimeSeries(cum_ret_compare, ylab = "Cumulatief rendement", colorset= c(2,3,1),lty = 2, legend.loc = "topleft")
The plot I get:
A screenshot of the data I used (below is the full dput() output):
The data I used (dput(ret_compare)):
structure(c(0.0565939990771869, -0.0347482427707391, -0.0277900457059636,
0.0429234080870222, -0.0718787783241953, -0.016863029966348,
0.059355733239068, 0.037562234119064, -0.0677988560026864, 0.0600705787852096,
-0.0517080807079698, 0.00180517117961099, -0.0209223756892264,
0.0272845085630986, 0.0837601002893609, 0.0359655556488165, 0.0289223567394459,
0.00786503568014307, 0.0613306520140755, -0.0691778150018606,
0.0326380403566631, -0.0133417659847783, 0.0124121161628492,
0.0642708816666793, 0.00507849967404161, 0.0261052878209909,
0.0151493267776062, 0.00676515233437147, 0.00653446817356351,
0.0150714363479285, -0.050143190382553, 0.0345058557122753, 0.0441645265494663,
0.0330199447063566, 0.080567937081057, 0.0316571988327838, 0.000198181362019767,
0.0208690458801075, 0.0504761702266239, -0.00550425517164699,
0.0399051303505815, 0.0599723999392408, 0.0568078556781997, -0.047301231431996,
0.0883885279858534, -0.0511316915459871, 0.00804493275737861,
0.0299441033050929, 0.0334788931639478, 0.0582014365192993, 0.0678197532602445,
0.0159992998095304, 0.0125443303556689, 0.00819301763612046,
0.00991755318616083, -0.120605712811205, -0.0262409920857444,
0.040946793757588, 0.0164392980283827, 0.0306621368418329, -0.0238640140220965,
-0.0156901113780832, 0.0101061236361657, 0.0280945470081029,
-0.0493498768067187, 0.0158476544516308, -0.0130536804845046,
0.00928507222402164, -0.0174652889946849, 0.0415549564151227,
0.00501328628644071, 0.141989003051248, -0.0684962605913843,
0.0476352610219926, 0.0161558138743416, -0.0288967250807524,
-0.0136524963030579, 0.013609536698864, -0.0101217287483828,
-0.0158427614092369, -0.0659110431281645, -0.00361214051470649,
-0.0339148683510243, 0.0549197973662541, 0.00351817414237154,
-0.0836247487330098, -0.0660975163205073, 0.0304100958405675,
-0.0433097428906102, -0.0363928303567407, 0.0106002084863943,
-0.027020033384645, -0.0291214507902441, 0.013711263904689, 0.0191434464401923,
0.0249660315555751, -0.0360122263475078, 0.00687569978857283,
0.0574596331980592, -0.0114262145986058, -0.00694415263166959,
-0.0519113175679171, -0.092809259472171, -0.000240961580031951,
-0.0573097859137003, 0.0476330153045359, 0.0158596707003653,
-0.0261396384934428, -0.0286505239756832, -0.0366692368775817,
-0.0127341035075788, 0.0669385878663209, 0.0465027267386504,
0.00704423441903423, 0.0217546431041822, -0.00582565417712289,
0.0130023382378637, 0.10511251526633, 0.0518522334205838, 0.107127520905078,
0.0178719315683786, 0.0278267973953032, -0.0485503100189258,
-0.0108659147438031, 0.0109666518413147, 0.0154645672337712,
-0.0338505700478167, -0.00436219527802972, 0.05104724595336,
0.0541549024268144, 0.0873384487014046, 0.0628186392725139, -0.0288161017299512,
0.0950492127308722, -0.0491296716777756, -0.0444532289596897,
-0.00216600237045717, 0.0190268040149375, 0.067817072016372,
0.0146918057312835, 0.0295514071747491, -0.0435830986730233,
0.0215714644357461, 0.0494583026657589, 0.0913329109870025, -0.0053448319045083,
0.0601557176652443, 0.0568172510761795, -0.0485426969753849,
0.00175302828494006, 0.00984938190559648, 0.0240502738702311,
-0.00118579262163876, 0.0561485994364315, 0.0685148593157117,
0.04683955790033, -0.0574638736286489, -0.0138207306792366, 0.0355299071029838,
0.0531266681464893, 0.0117742011764304, -0.00525222583312346,
-0.0293812846037232, -0.0131594446545449, 0.0289964530305211,
0.0360588122490606, -0.0445991604911897, -0.0133107604099818,
-0.038145829508777, -0.000486060215306838, 0.00104676175588558,
0.0208886233797061, -0.00263583111403876, -0.12338493810393,
-0.0166803342153315, -0.0297568741980336, -0.112338323364713,
-0.0693713119660443, -0.0180597800314281, 0.0134278508097088,
-0.0330598180720567, -0.0366483220489359, 0.0339417580345724,
0.0509050678267557, 0.0528878066096272, -0.018266616997294, 0.0677991301131817,
0.0413170767588944, 0.0179821345844948, -0.0120530476718976,
0.0506564243990657, 0.0108111883471529, -0.0607609892616301,
-0.0245384465619248, 0.0513624569791613, -0.0375362970369394,
-0.108672749280212, -0.00337217907778453, 0.087093380777213,
-0.0338856992001196, 0.0840213297383585, 0.0404836319184165,
-0.0844650405081253, 0.0619874075245053, 0.0504278836336267,
0.0415781344576631, -0.0167132483670377, 0.076801882730787, -0.0369574751565279,
-0.0206697388445634, -0.0328788588005816, -0.083383558090487,
-0.0323162584368404, 0.051610493839749, -0.0220809995437766,
-0.00870937103673219, 0.0225163266689465, 0.0397241319702022,
-0.0102895797305198, -0.0281284547507554, -0.0991696936611708,
0.0636541757418483, 0.0179711424415336, 0.0333173103972026, 0.0225887202551132,
0.00443860666159801, 0.014806760557537, 0.0310237163003237, 0.144376590041557,
-0.0448055417930532, -0.00513324649717639, 0.0537257308063575,
-0.00999221359281199, -0.0819189957530423, 0.0476018428802933,
-0.0214364641397353, 0.0918599937028983, 0.068148748800128, 0.00803440955796253,
0.0397444044913302, -0.0552739603600437, 0.10420213043066, -0.0215270370205893,
0.0249963705701839, -0.0093007099097957, -0.00944183950206712,
-0.0971704450184838, 0.00264470427302532, -0.0723146624220592,
-0.0604005470589978, 0.0267660599545763, -0.0809302399859423,
-0.0155073183470853, 0.0664580522537737, -0.0536551996227884,
0.0468841220908309, -0.0208428453992652, -0.0416561940283499,
0.0333869121623922, -0.0710574857344434, -0.0493355477841083,
0.0574454161378841, -0.0413939958360005, -0.0361717353766919,
-0.0730611841593626, -0.0306524518642073, 0.0448704347638376,
0.0189138798885842, -0.019112690070504, -0.0607784992411501,
0.0200647095833417, 4.64025545323654e-05, 0.0167228957527705,
-0.0399352463740604, -0.0533583100515406, 0.0922371375755624,
0.0469543874397995, 0.0130431580117298, 0.0904898600303281, 0.0657425429880596,
0.0499463961351239, -0.0261922592092307, 0.065751610214466, -0.00621555863424983,
0.0832799329468406, 0.00866941466260029, -0.017688747365096,
0.0449778523037858, 0.0954877576723518, -0.118831287262529, -0.0142308642932637,
0.0385103238081386, -0.064183870293033, -0.0142310862314927,
0.046099549894892, -0.0550393421312394, 0.00274530215199009,
-0.156434752199699, -0.00708826723870748, -0.0462880814806087,
0.059565753901339, 0.0316103424635057, 0.00219296046281703, 0.0485668880949881,
-0.16121577788262, 0.0851533099681305, -0.0386176257065057, -0.0712227636357597,
0.0312116344677249, 0.0527030336009604, 0.016990538618856, 0.0943310408154399,
-0.0417768266816103, -0.167029346552333, -0.136573394710224,
0.0151856742327374, 0.0150795243191497, 0.0256024944818467, 0.0319573321526792,
0.0423101107019896, -0.060681262543504, -0.0516425733035891,
0.160387164752116, 0.0380848238199503, -0.0206859096083852, 0.0113327971407688,
0.0582468900635822, 0.0619978730333017, 0.077263516690542, -0.0171388878765958,
0.0199810159760507, 0.0158023347139837, -0.103334180665634, 0.0549659271604657,
-0.0893066705987667, 0.101606771360827, 0.0492865186589087, -0.0238877039788714,
-0.0277900457059636, 0.0404617408226444, -0.0718787783241953,
-0.0123575474841724, 0.059355733239068, 0.029908291943469, -0.0677988560026864,
0.0600705787852096, -0.0437493069001588, 0.00180517117961099,
-0.0209223756892264, 0.0272845085630986, 0.0837601002893609,
0.0299074232605328, 0.0240446051441223, 0.00748096094707074,
0.0613306520140755, -0.0691778150018606, 0.0246049232880761,
-0.0133417659847783, 0.0124121161628492, 0.0642708816666793,
0.00507849967404161, 0.0261052878209909, 0.0151493267776062,
0.00676515233437147, 0.00653446817356351, 0.0150714363479285,
-0.050143190382553, 0.0277659888972834, 0.0441645265494663, 0.0330199447063566,
0.080567937081057, 0.0316571988327838, 0.00111260813987357, 0.016406229113636,
0.0504761702266239, -0.00550425517164699, 0.0395917385160307,
0.048456257520995, 0.0430035722547741, -0.047301231431996, 0.087565977773425,
-0.050604038742966, 0.00804493275737861, 0.0299441033050929,
0.0357934879374062, 0.0582014365192993, 0.0698786365111124, 0.0171741022871303,
0.0125443303556689, 0.00819301763612046, 0.00991755318616083,
-0.127008834727823, -0.0262409920857444, 0.040946793757588, 0.0164392980283827,
0.0306621368418329, -0.0238640140220965, -0.0156901113780832,
0.00959306152476125, 0.030309665786747, -0.0520373515064326,
0.0155414686264252, -0.00868280688491285, 0.00882165631942744,
-0.0152414835528486, 0.0354479986139713, 0.00495367631225263,
0.0958858811432524, -0.0684962605913843, 0.0476352610219926,
0.0161558138743416, -0.0288967250807524, -0.0136524963030579,
0.013609536698864, -0.0101217287483828, -0.0129056749640298,
-0.0561043848567603, -0.00361214051470649, -0.0339148683510243,
0.0549197973662541, 0.00352545246358327, -0.0908929634017446,
-0.0766867551408712, 0.0304100958405675, -0.0433097428906102,
-0.0395849516878581, 0.00961086344015882, -0.0281887998980271,
-0.0277864019559111, 0.013711263904689, 0.0191434464401923, 0.0254260098579855,
-0.0360122263475078, 0.00713376835505675, 0.0515609918115263,
-0.0104156632309022, -0.00554941806867115, -0.0366770391324845,
-0.092809259472171, -0.000240961580031951, -0.0573097859137003,
0.0476330153045359, 0.0158596707003653, -0.0261396384934428,
-0.0286505239756832, -0.0366692368775817, -0.0127341035075788,
0.0669385878663209, 0.0465027267386504, 0.00697103541730559,
0.0240759738135028, -0.00499995833368028, 0.0130023382378637,
0.0660819187835711, 0.0381905638254254, 0.081864083562877, 0.0178719315683786,
0.0253466261092197, -0.0349542847267367, -0.0123113683808378,
0.00922796120595493, 0.0154645672337712, -0.0256621847300804,
-0.00257097347471802, 0.0339067339228674, 0.0356102024265696,
0.0571472869544005, 0.0628186392725139, -0.0221911881645702,
0.0950492127308722, -0.0491296716777756, -0.0304308647089176,
-0.00105025749743581, 0.0190268040149375, 0.067817072016372,
0.0118702126988408, 0.02056060572427, -0.0320256986349159, 0.0173034620187353,
0.0348383376789623, 0.0913329109870025, -0.00508049371248243,
0.0601557176652443, 0.0474269701349554, -0.0362187348642155,
0.00175302828494006, 0.00984938190559648, 0.0240502738702311,
-0.000928072811158587, 0.0422905855543541, 0.0685148593157117,
0.031757030973413, -0.0574638736286489, -0.00959417435537924,
0.0325380097149295, 0.0597952080195425, 0.00944188854178352,
-0.00229856160324715, -0.0271236735291358, -0.0137710545829336,
0.0289964530305211, 0.0360588122490606, -0.0427573578511865,
-0.0133107604099818, -0.0413350154935502, -0.000486060215306838,
0.00104676175588558, 0.0208886233797061, -0.00267336295134479,
-0.0885860511912184, -0.0161029691326237, -0.0297568741980336,
-0.112338323364713, -0.0693713119660443, -0.0180597800314281,
0.0134278508097088, -0.0330598180720567, -0.0366483220489359,
0.0339417580345724, 0.0509050678267557, 0.0528878066096272, -0.018266616997294,
0.0677991301131817, 0.0413170767588944, 0.0179821345844948, -0.0121888968895034,
0.0506564243990657, 0.0108111883471529, -0.0607609892616301,
-0.0247113973468298, 0.0513624569791613, -0.0309923310770995,
-0.108672749280212, -0.00337217907778453, 0.087093380777213,
-0.0338856992001196, 0.0840213297383585, 0.0424971554686477,
-0.081464070989248, 0.0619874075245053, 0.0430698737989899, 0.0421349243533173,
-0.0109533653241958, 0.0742378263228765, -0.0369574751565279,
-0.0212240452296111, -0.0328788588005816, -0.083383558090487,
-0.0323162584368404, 0.051610493839749, -0.0220809995437766,
-0.00870937103673219, 0.0225163266689465, 0.0438734449744449,
-0.00892210665440751, -0.0293707722982393, -0.0991696936611708,
0.0636541757418483, 0.0181589135552847, 0.0333173103972026, 0.0210622227911781,
0.00487335405251677, 0.0131356170394468, 0.0273537483318715,
0.144376590041557, -0.02796259796409, -0.00522582776545444, 0.0364354414456791,
-0.00837134854176702, -0.0502137264472271, 0.0476018428802933,
-0.0153208746756319, 0.0513742248497895, 0.0421405589892034,
0.00465956984288418, 0.0215903415755918, -0.0388077343572576,
0.0657724728057736, -0.0132884344984379, 0.0201092644054153,
-0.0051175163129672, -0.00944183950206712, -0.0971704450184838,
0.0016192925508538, -0.0723146624220592, -0.0604005470589978,
0.0258782226065151, -0.0433891880670063, -0.0141086652162496,
0.0625300971761347, -0.0301628728287874, 0.0385126964933498,
-0.0112984784610625, -0.0323098915386699, 0.0365004974846537,
-0.0737970611899815, -0.0493355477841083, 0.0574454161378841,
-0.0306092896219177, -0.0262257339374732, -0.0569910029244357,
-0.0306524518642073, 0.0448704347638376, 0.0185695069363463,
-0.0155548984204991, -0.0467627599243856, 0.0200647095833417,
8.29964155073526e-05, 0.0167228957527705, -0.0335972446871899,
-0.0294344648848727, 0.0518143754361482, 0.0257859588754956,
0.00824571243269778, 0.0904898600303281, 0.0657425429880596,
0.0415235900563797, -0.0261922592092307, 0.065751610214466, -0.00621555863424983,
0.0832799329468406, 0.00866941466260029, -0.017688747365096,
0.0449778523037858, 0.0954877576723518, -0.118831287262529, -0.0153069547305189,
0.0218515179870005, -0.0342686779423506, -0.00850677257427712,
0.0325477383763988, -0.0306141018335595, 0.00243501209588759,
-0.156434752199699, -0.005029743685798, -0.0345290845779366,
0.059565753901339, 0.0252512069665196, 0.00207064965943626, 0.0302923006737879,
-0.16121577788262, 0.0654457174010914, -0.0209269433762712, -0.0712227636357597,
0.0269825779272959, 0.0307477365084228, 0.0141733437604381, 0.0943310408154399,
-0.023119978605681, -0.0967380201240571, -0.147305013179531,
0.0151856742327374, 0.0150795243191497, 0.0256024944818467, 0.0319573321526792,
0.0389645831394516, -0.0516711678172406, -0.057263622404775,
0.17035185335458, 0.0380848238199503, -0.014964951245554, 0.00815821494412972,
0.0582468900635822, 0.0410152743642858, 0.077263516690542, -0.014889383359691,
0.0128841652906317, 0.0126255977120013, -0.103334180665634, 0.0438272536940851,
-0.0576089770159162, 0.0647885069687177, 0.0492865186589087,
-0.0238877039788714, -0.0300054599177805, 0.0404617408226444,
-0.0429596698815269, -0.0123575474841724, 0.0417189496432033,
0.029908291943469, -0.0409145877912638, 0.0416155635440896, -0.0437493069001588,
0.00282867922883168, -0.00940990309615575, 0.0190579142175993,
0.0443042237259872, 0.0299074232605328, 0.0240446051441223, 0.00748096094707074,
0.0443570938433033, -0.0404571419085026, 0.0246049232880761,
-0.0066025249154531, 0.00954964988443341, 0.0298053972087278,
0.00476107372301793, 0.0175757232170388, 0.0101068360663179,
0.00609368513738762, 0.00540196687039063, 0.00917053509817412,
-0.0216215093591073, 0.0277659888972834, 0.0226023268044275,
0.0213902871313327, 0.0443373040422519, 0.0176332357221722, 0.00111260813987357,
0.016406229113636, 0.0298863069339816, -0.006561222645037, 0.0395917385160307,
0.048456257520995, 0.0430035722547741, -0.0585570779808038, 0.087565977773425,
-0.050604038742966, 0.0108699498782903, 0.0350978073788761, 0.0357934879374062,
0.0665716262589837, 0.0698786365111124, 0.0171741022871303, 0.0148956336813075,
0.00971692648248834, 0.0118976448527714, -0.127008834727823,
-0.0643807858901128, 0.0785395421983646, 0.0341359119905573,
0.0424124006976598, -0.0297507849081639, -0.0284978431148246,
0.00959306152476125, 0.030309665786747, -0.0520373515064326,
0.0155414686264252, -0.00868280688491285, 0.00882165631942744,
-0.0152414835528486, 0.0354479986139713, 0.00495367631225263,
0.0958858811432524, -0.0754119883858065, 0.0605889700089284,
0.0230055503982476, -0.0452346298784714, -0.0199167187040488,
0.0202669563997153, -0.0151015261797287, -0.0129056749640298,
-0.0561043848567603, -0.00874332327370753, -0.0450463176390838,
0.0676450859834781, 0.00352545246358327, -0.0908929634017446,
-0.0766867551408712, 0.0683714108587556, -0.0562536535683003,
-0.0395849516878581, 0.00961086344015882, -0.0281887998980271,
-0.0277864019559111, 0.0315316866110831, 0.0278566332045715,
0.0254260098579855, -0.0478119064346871, 0.00713376835505675,
0.0515609918115263, -0.0104156632309022, -0.00554941806867115,
-0.0366770391324845, -0.128540218463314, -0.00333101415653492,
-0.120107648833262, 0.0958295457008402, 0.0354990847462275, -0.0370024578753033,
-0.035961013075631, -0.0473812313231093, -0.0190728925526511,
0.129179809515038, 0.0580707168491923, 0.00697103541730559, 0.0240759738135028,
-0.00499995833368028, 0.00801345581575896, 0.0660819187835711,
0.0381905638254254, 0.081864083562877, 0.0102074178146554, 0.0253466261092197,
-0.0349542847267367, -0.0123113683808378, 0.00922796120595493,
0.0190388963276953, -0.0256621847300804, -0.00257097347471802,
0.0339067339228674, 0.0356102024265696, 0.0571472869544005, 0.037933876312189,
-0.0221911881645702, 0.04867790190674, -0.0284893707600057, -0.0304308647089176,
-0.00105025749743581, 0.0113296046090396, 0.0384517691761184,
0.0118702126988408, 0.02056060572427, -0.0320256986349159, 0.0173034620187353,
0.0348383376789623, 0.055078009042417, -0.00508049371248243,
0.0349324182903621, 0.0474269701349554, -0.0362187348642155,
0.000731944005366358, 0.0126588629475626, 0.0284200411474285,
-0.000928072811158587, 0.0422905855543541, 0.0344914231782354,
0.031757030973413, -0.0204446138767127, -0.00959417435537924,
0.0325380097149295, 0.0597952080195425, 0.00944188854178352,
-0.00229856160324715, -0.0271236735291358, -0.0137710545829336,
0.048844176339206, 0.0455008063339308, -0.0427573578511865, -0.0178282872713298,
-0.0413350154935502, -0.00251312075281451, 0.000392568409978589,
0.038867708087317, -0.00267336295134479, -0.0885860511912184,
-0.0161029691326237, -0.0420241198590325, -0.140933679398404,
-0.213182576299088, -0.092462713370572, 0.0518406558167939, -0.0897576490474823,
-0.0843476608722317, 0.0665382076070802, 0.131582448235168, 0.107795304196951,
-0.0220904436860068, 0.105306956007685, 0.0603934116278224, 0.0236136189048726,
-0.0121888968895034, 0.0658652385210567, 0.0143138180286164,
-0.0670856183281777, -0.0247113973468298, 0.0618092509209986,
-0.0309923310770995, -0.128803242463851, -0.00934578652654638,
0.12464789947184, -0.0389238136806155, 0.105292759441438, 0.0424971554686477,
-0.081464070989248, 0.0801304884170133, 0.0430698737989899, 0.0421349243533173,
-0.0109533653241958, 0.0742378263228765, -0.0412233371318413,
-0.0212240452296111, -0.038721073177811, -0.102593285857173,
-0.0660128643244059, 0.120312560041835, -0.0445404035530143,
-0.0160245207220792, 0.0264519215884826, 0.0438734449744449,
-0.00892210665440751, -0.0293707722982393, -0.131667864804966,
0.0765524648247053, 0.0181589135552847, 0.0410867753199928, 0.0210622227911781,
0.00487335405251677, 0.0131356170394468, 0.0273537483318715,
0.0525736688028997, -0.02796259796409, -0.00522582776545444,
0.0364354414456791, -0.00837134854176702, -0.0502137264472271,
0.0532240511918611, -0.0153208746756319, 0.0513742248497895,
0.0421405589892034, 0.00465956984288418, 0.0215903415755918,
-0.0388077343572576, 0.0657724728057736, -0.0132884344984379,
0.0201092644054153, -0.0051175163129672, -0.00294532574721995,
-0.0342077361930025, 0.0016192925508538, -0.0379594485162568,
-0.0270255520457952, 0.0258782226065151, -0.0433891880670063,
-0.0141086652162496, 0.0625300971761347, -0.0301628728287874,
0.0385126964933498, -0.0112984784610625, -0.0323098915386699,
0.0365004974846537, -0.0737970611899815, -0.0556600639245958,
0.0705786039020948, -0.0306092896219177, -0.0262257339374732,
-0.0569910029244357, -0.0405411931490601, 0.0592431057525111,
0.0185695069363463, -0.0155548984204991, -0.0467627599243856,
0.0477649909637343, 8.29964155073526e-05, 0.00912820625534794,
-0.0335972446871899, -0.0294344648848727, 0.0518143754361482,
0.0257859588754956, 0.00824571243269778, 0.0364118589045417,
0.0311793838385637, 0.0415235900563797, -0.0130142562807395,
0.0340778557667032, -0.00227377328383616, 0.0347900685331286,
0.00384546460879642, -0.00418305832682986, 0.0144009024701055,
0.0443595078891013, -0.0509816485786232, -0.0153069547305189,
0.0218515179870005, -0.0342686779423506, -0.00850677257427712,
0.0325477383763988, -0.0306141018335595, 0.00243501209588759,
-0.0772496207460813, -0.005029743685798, -0.0345290845779366,
0.0670034415430896, 0.0252512069665196, 0.00207064965943626,
0.0302923006737879, -0.0635789495123614, 0.0654457174010914,
-0.0209269433762712, -0.0282304959937522, 0.0269825779272959,
0.0307477365084228, 0.0141733437604381, 0.0384548002293232, -0.023119978605681,
-0.0967380201240571, -0.147305013179531, 0.0566080914066258,
0.0267254767970793, 0.0387793305132154, 0.0441493507889792, 0.0389645831394516,
-0.0516711678172406, -0.057263622404775, 0.17035185335458, 0.0462477698800481,
-0.014964951245554, 0.00815821494412972, 0.0272545550529284,
0.0410152743642858, 0.0388027775914868, -0.014889383359691, 0.0128841652906317,
0.0126255977120013, -0.0535302063156535, 0.0438272536940851,
-0.0576089770159162, 0.0647885069687177), .Dim = c(336L, 3L), .Dimnames = list(
NULL, c("Conventionele.strategie", "Conditionele.strategie",
"Niet.geschaald")), index = structure(c(759974400, 762393600,
765072000, 767577600, 770342400, 772934400, 775440000, 778291200,
780883200, 783561600, 786153600, 788745600, 791510400, 793929600,
796608000, 799027200, 801878400, 804470400, 807148800, 809827200,
812332800, 815097600, 817689600, 820195200, 823046400, 825552000,
828057600, 830822400, 833500800, 835920000, 838771200, 841363200,
844041600, 846720000, 849225600, 851990400, 854668800, 857088000,
859766400, 862358400, 864950400, 867628800, 870307200, 872812800,
875577600, 878256000, 880675200, 883526400, 886118400, 888537600,
891302400, 893894400, 896400000, 899164800, 901843200, 904521600,
907113600, 909705600, 912384000, 915062400, 917568000, 919987200,
922838400, 925430400, 927849600, 930700800, 933292800, 936057600,
938649600, 941155200, 943920000, 946598400, 949276800, 951782400,
954460800, 956880000, 959731200, 962323200, 965001600, 967680000,
970185600, 972950400, 975542400, 978048000, 980899200, 983318400,
985910400, 988588800, 991267200, 993772800, 996537600, 999216000,
1001635200, 1004486400, 1007078400, 1009756800, 1012435200, 1014854400,
1017273600, 1020124800, 1022803200, 1025222400, 1028073600, 1030665600,
1033344000, 1036022400, 1038528000, 1041292800, 1043971200, 1046390400,
1049068800, 1051660800, 1054252800, 1056931200, 1059609600, 1062115200,
1064880000, 1067558400, 1069977600, 1072828800, 1075420800, 1077840000,
1080691200, 1083283200, 1085702400, 1088553600, 1091145600, 1093910400,
1096502400, 1099008000, 1101772800, 1104451200, 1107129600, 1109548800,
1112227200, 1114732800, 1117497600, 1120089600, 1122595200, 1125446400,
1128038400, 1130716800, 1133308800, 1135900800, 1138665600, 1141084800,
1143763200, 1146182400, 1149033600, 1151625600, 1154304000, 1156982400,
1159488000, 1162252800, 1164844800, 1167350400, 1170201600, 1172620800,
1175212800, 1177891200, 1180569600, 1183075200, 1185840000, 1188518400,
1190937600, 1193788800, 1196380800, 1199059200, 1201737600, 1204243200,
1206921600, 1209513600, 1212105600, 1214784000, 1217462400, 1219968000,
1222732800, 1225411200, 1227830400, 1230681600, 1233273600, 1235692800,
1238457600, 1241049600, 1243555200, 1246320000, 1248998400, 1251676800,
1254268800, 1256860800, 1259539200, 1262217600, 1264723200, 1267142400,
1269993600, 1272585600, 1275004800, 1277856000, 1280448000, 1283212800,
1285804800, 1288310400, 1291075200, 1293753600, 1296432000, 1298851200,
1301529600, 1304035200, 1306800000, 1309392000, 1311897600, 1314748800,
1317340800, 1320019200, 1322611200, 1325203200, 1327968000, 1330473600,
1333065600, 1335744000, 1338422400, 1340928000, 1343692800, 1346371200,
1348790400, 1351641600, 1354233600, 1356912000, 1359590400, 1362009600,
1364428800, 1367280000, 1369958400, 1372377600, 1375228800, 1377820800,
1380499200, 1383177600, 1385683200, 1388448000, 1391126400, 1393545600,
1396224000, 1398816000, 1401408000, 1404086400, 1406764800, 1409270400,
1412035200, 1414713600, 1417132800, 1419984000, 1422576000, 1424995200,
1427760000, 1430352000, 1432857600, 1435622400, 1438300800, 1440979200,
1443571200, 1446163200, 1448841600, 1451520000, 1454025600, 1456704000,
1459382400, 1461888000, 1464652800, 1467244800, 1469750400, 1472601600,
1475193600, 1477872000, 1480464000, 1483056000, 1485820800, 1488240000,
1490918400, 1493337600, 1496188800, 1498780800, 1501459200, 1504137600,
1506643200, 1509408000, 1.512e+09, 1514505600, 1517356800, 1519776000,
1522281600, 1525046400, 1527724800, 1530230400, 1532995200, 1535673600,
1538092800, 1540944000, 1543536000, 1546214400, 1548892800, 1551312000,
1553817600, 1556582400, 1559260800, 1561680000, 1564531200, 1567123200,
1569801600, 1572480000, 1574985600, 1577750400, 1580428800, 1582848000,
1585612800, 1588204800, 1590710400, 1593475200, 1596153600, 1598832000,
1601424000, 1604016000, 1606694400, 1609372800, 1611878400, 1614297600,
1617148800, 1619740800, 1622160000, 1625011200, 1627603200, 1630368000,
1632960000, 1635465600, 1638230400, 1640908800), tzone = "UTC", tclass = c("POSIXct",
"POSIXt")), class = c("xts", "zoo"))
Similar to you, I could only affect the legend by changing lty or lwd parameters. The package authors state that
"This function is intended to be used inside other charting functions."
— ?chart.TimeSeries
Indeed, you can select a plot.engine by passing it a function argument: plot.engine = "ggplot2" will return and plot a ggplot graph, "plotly" an interactive plotly-plot and so on. Then again you'll need the corresponding libraries installed and some basic aquaintance with each package (so I wonder why to use chart.TimeSeries as a wrapper anyway).
Edit - two alternatives to plot your data with library(ggplot2)
Fast and simple:
cum_ret_compare %>% autoplot.zoo
with basic dplyr and ggplot operations:
my_plot <-
cum_ret_compare %>%
as.data.frame %>%
mutate(Date = as.Date(time(cum_ret_compare))) %>% ## extract date to column Date
## stack variables instead of side-by-side
## to easier apply variable-wise color further down:
pivot_longer(cols = -Date,
names_to = 'strategy_type',
values_to = 'value'
) %>%
## construct ggplot
ggplot(aes(Date, value)) +
## add lines per strategy_type
geom_line(aes(col = strategy_type),
lty = 1, ## linetype
lwd = .1 ## linewidth
) +
labs(x = 'Date', y = 'cum. return')
## plot it:
my_plot
## make it look impressive ;-)
library(ggthemes)
my_plot + theme_economist()
My first data frame (df) contains Entrydate and ExitDate columns. Another dataframe (n1) has all trading dates. I need a new column in first dataframe calculated as number of days as calculated from the second dataframe. How do I call this dayCount function for each row of df. When I try to use mapply, I am unable to pass n1 as a parameter.
dayCount <- function (startDate, endDate, n1) {
return (nrow(subset(n1, Date >= startDate & Date <= endDate)))
}
df<- structure(list(EntryDate = structure(c(11355, 11418, 11436, 11449,
11520, 11523, 11548, 11620, 11768, 11773), class = "Date"), ExitDate = structure(c(11360,
11422, 11438, 11457, 11522, 11526, 11554, 11625, 11772, 11778
), class = "Date")), row.names = c(22L, 65L, 76L, 84L, 135L,
138L, 155L, 204L, 305L, 307L), class = "data.frame")
n1<- structure(c(11354, 11355, 11358, 11359, 11360, 11361, 11362,
11365, 11366, 11367, 11368, 11369, 11372, 11373, 11374, 11375,
11376, 11379, 11380, 11381, 11382, 11383, 11386, 11388, 11389,
11390, 11393, 11394, 11395, 11396, 11397, 11400, 11401, 11402,
11403, 11404, 11407, 11408, 11409, 11410, 11411, 11414, 11415,
11416, 11418, 11421, 11422, 11423, 11424, 11428, 11429, 11430,
11431, 11432, 11435, 11436, 11437, 11438, 11439, 11442, 11444,
11445, 11446, 11449, 11450, 11451, 11452, 11453, 11456, 11457,
11458, 11459, 11460, 11463, 11464, 11465, 11466, 11467, 11470,
11471, 11472, 11473, 11474, 11477, 11478, 11479, 11480, 11481,
11484, 11485, 11486, 11487, 11488, 11491, 11492, 11493, 11494,
11495, 11498, 11499, 11500, 11501, 11502, 11505, 11506, 11507,
11508, 11509, 11512, 11513, 11514, 11515, 11516, 11519, 11520,
11521, 11522, 11523, 11526, 11527, 11528, 11529, 11530, 11533,
11534, 11535, 11536, 11537, 11540, 11541, 11542, 11543, 11544,
11547, 11548, 11550, 11551, 11554, 11555, 11557, 11558, 11561,
11562, 11563, 11564, 11565, 11568, 11569, 11570, 11571, 11572,
11575, 11576, 11577, 11578, 11579, 11582, 11583, 11584, 11585,
11586, 11589, 11590, 11591, 11592, 11593, 11596, 11598, 11599,
11600, 11603, 11604, 11605, 11606, 11607, 11610, 11611, 11612,
11613, 11614, 11617, 11618, 11619, 11620, 11624, 11625, 11626,
11627, 11628, 11631, 11632, 11633, 11634, 11635, 11638, 11639,
11640, 11641, 11645, 11646, 11647, 11648, 11649, 11652, 11653,
11654, 11655, 11659, 11660, 11661, 11662, 11663, 11666, 11667,
11668, 11669, 11670, 11674, 11675, 11676, 11677, 11680, 11682,
11683, 11684, 11687, 11688, 11689, 11690, 11691, 11694, 11695,
11696, 11697, 11698, 11701, 11702, 11703, 11704, 11705, 11708,
11709, 11710, 11711, 11712, 11715, 11716, 11717, 11718, 11719,
11722, 11723, 11724, 11725, 11726, 11729, 11730, 11731, 11732,
11733, 11736, 11737, 11738, 11739, 11740, 11743, 11744, 11745,
11746, 11747, 11750, 11751, 11752, 11753, 11754, 11757, 11758,
11759, 11760, 11761, 11764, 11765, 11766, 11767, 11768, 11772,
11773, 11774, 11778), class = "Date")
You can use %in% to count number of days in n1 between each EntryDate and ExitDate.
df$dayCount <- colSums(mapply(function(x, y) n1 %in% seq(x, y, by = '1 day'),
df$EntryDate, df$ExitDate))
df
# EntryDate ExitDate dayCount
#22 2001-02-02 2001-02-07 4
#65 2001-04-06 2001-04-10 3
#76 2001-04-24 2001-04-26 3
#84 2001-05-07 2001-05-15 7
#135 2001-07-17 2001-07-19 3
#138 2001-07-20 2001-07-23 2
#155 2001-08-14 2001-08-20 4
#204 2001-10-25 2001-10-30 3
#305 2002-03-22 2002-03-26 2
#307 2002-03-27 2002-04-01 3
I have a list in R from a Structural VAR Model I've run. Here is the dataset I've used
structure(c(-2.46773175636719, -7.72826473957142, 11.7280053716074,
-1.90017613519231, 2.46070753200911, -10.9055849254775, 12.5325444143789,
1.3401777880631, 1.11706357603545, -11.9683333308867, 13.8014223673123,
3.00567366940339, 1.93467425287253, -12.4811258520841, 12.5337510851622,
6.17970387794458, -0.311524449552714, -4.43067381564184, 6.00580266302142,
0.733215859597713, -0.0920826461465296, -10.4968848655989, 15.1592141225619,
-0.0142294828419764, 2.81612947228318, -7.95484787976672, 11.3258100085652,
-4.26536944046738, 1.71777230300645, 3.05071906367438, 6.86888493397788,
-2.04121390648186, -5.45632423113958, 4.32701496742318, -1.77039007913358,
-3.38387552001187, -3.6948032720689, 9.71137617961375, 7.70239243534423,
-7.34908250395865, 1.66529724744393, 4.12699586248877, 1.46757481960265,
0.371903496562886, -3.1265641490501, 10.1504446414976, -6.97113134346488,
10.6335568680931, -7.74296970668011, -0.31563625249369, 4.86848083529221,
0.0394209612387897, 4.1060141554107, 4.40828962229318, -3.65631265465627,
-0.804371886780864, 5.300399767623, 3.33547294572138, 2.19966779280814,
-2.96620054964727, 2.01147630916658, 3.00808531323007, -1.78858517036105,
-0.262917554632125, -2.63540419004151, 3.42060573075127, -0.379197105963414,
-2.24861964821645, -1.33132752566709, 1.67672890048411, 2.42834777679839,
-1.28230298674303, 2.33228006495114, -1.79196678955762, -3.25533945043563,
-2.49506277883942, 3.87275419034108, -0.237088301198796, 3.15158720918571,
-8.16796902280537, -5.40342750457601, -0.684253285161596, 2.76776077124627,
3.03762540403962, 1.59655060303945, 2.81108799665279, 2.12619249528281,
1.25976266808472, 1.75072385830202, 3.35523567403357, 3.66056933213099,
2.81157744760496, 2.8694753969682, 1.22389693906415, 1.23949528512668,
0.655297291930701, -0.0811557166513488, 2.49469766016768, 1.04750905161959,
0.553154355370644, 0.0394037933455493, 0.495095459931427, 0.263970407073533,
1.46876270708987, 1.02690822562028, 1.21249150827216, 0.744350196120713,
2.28082505446663, 1.40375659950536, 1.59077468759525, 1.74048892256584,
1.81791859189397, 0.640927792895951, 2.05320969806211, 3.31708856718933,
3.94172305193239, 1.29907384513261, 2.18844272980503, 2.02564945461834,
2.89662683480758, 0.531971587307556, 1.69103059033393, 0.195326999327161,
1.20287931837835, -0.0417495586606087, 1.89055607207678, 1.8793164132429,
1.19212718508228, 1.43097285231573, 2.4079363839828, 1.02287700043888,
1.41700117422383, 1.9324642470476, 1.42192313279805, 1.00916357593048,
1.26198292983659, 0.929701659038162, 1.77329986790085, 1.88793046797189,
0.685737107435092, 0.400744609924519, 0.766302538581343, 1.09780031685389,
1.00049427220021, 0.535284014354875, 3.18688973234602, 1.75770144393539,
1.19538307882463, 0.904511107610428, 3.52708500618766, 0.526934426385495,
0.361429093322574, 0.624469028588326, 1.95600784906818, 0.781933189336748,
1.09121935535335, 1.30962848646536, 1.24173607356797, 0.453213216209214,
0.468243562258674, 0.489076348335971, 0.848706378173603, 0.125371629239357,
0.817153292286932), .Dim = c(82L, 2L), .Dimnames = list(NULL,
c("GDP_NAM", "CPI_NAM")), index = structure(c(962323200,
970272000, 978220800, 985996800, 993859200, 1001808000, 1009756800,
1017532800, 1025395200, 1033344000, 1041292800, 1049068800, 1056931200,
1064880000, 1072828800, 1080691200, 1088553600, 1096502400, 1104451200,
1112227200, 1120089600, 1128038400, 1135987200, 1143763200, 1151625600,
1159574400, 1167523200, 1175299200, 1183161600, 1191110400, 1199059200,
1206921600, 1214784000, 1222732800, 1230681600, 1238457600, 1246320000,
1254268800, 1262217600, 1269993600, 1277856000, 1285804800, 1293753600,
1301529600, 1309392000, 1317340800, 1325289600, 1333152000, 1341014400,
1348963200, 1356912000, 1364688000, 1372550400, 1380499200, 1388448000,
1396224000, 1404086400, 1412035200, 1419984000, 1427760000, 1435622400,
1443571200, 1451520000, 1459382400, 1467244800, 1475193600, 1483142400,
1490918400, 1498780800, 1506729600, 1514678400, 1522454400, 1530316800,
1538265600, 1546214400, 1553990400, 1561852800, 1569801600, 1577750400,
1585612800, 1593475200, 1601424000), tzone = "UTC", tclass = "Date"), class = c("xts",
"zoo"))
and the code I've used to create the model
library(tidyverse)
library(vars)
var.namibia <- namibia %>% VAR(.,p=1,type = 'both',season=NULL)
SVAR.namibia <- BQ(var.namibia)
from this, I want to extract residuals from the variables in my model. I can do it by indexing (see below), but I'm interested in finding a dplyr solution to achieve the same. Any suggestions?
res <- SVAR.namibia$var$varresult$CPI_NAM$residuals
You can use purrr's pluck function.
SVAR.namibia %>%
purrr::pluck('var', 'varresult', 'CPI_NAM', 'residuals')
I'm trying to create a 30 day forecast using auto.arima from the forecast package. I want to capture the long term trend, so I inserted it into the xreg argument.
The data:
dput(data)
structure(list(TKDate = structure(c(15706, 15707, 15708, 15709,
15710, 15711, 15712, 15713, 15714, 15715, 15716, 15717, 15718,
15719, 15720, 15721, 15722, 15723, 15724, 15725, 15726, 15727,
15728, 15729, 15730, 15731, 15732, 15733, 15734, 15735, 15736,
15737, 15738, 15739, 15740, 15741, 15742, 15743, 15744, 15745,
15746, 15747, 15748, 15749, 15750, 15751, 15752, 15753, 15754,
15755, 15756, 15757, 15758, 15759, 15760, 15761, 15762, 15763,
15764, 15765, 15766, 15767, 15768, 15769, 15770, 15771, 15772,
15773, 15774, 15775, 15776, 15777, 15778, 15779, 15780, 15781,
15782, 15783, 15784, 15785, 15786, 15787, 15788, 15789, 15790,
15791, 15792, 15793, 15794, 15795, 15796, 15797, 15798, 15799,
15800, 15801, 15802, 15803, 15804, 15805, 15806, 15807, 15808,
15809, 15810, 15811, 15812, 15813, 15814, 15815, 15816, 15817,
15818, 15819, 15820, 15821, 15822, 15823, 15824, 15825, 15826,
15827, 15828, 15829, 15830, 15831, 15832, 15833, 15834, 15835,
15836, 15837, 15838, 15839, 15840, 15841, 15842, 15843, 15844,
15845, 15846, 15847, 15848, 15849, 15850, 15851, 15852, 15853,
15854, 15855, 15856, 15857, 15858, 15859, 15860, 15861, 15862,
15863, 15864, 15865, 15866, 15867, 15868, 15869, 15870, 15871,
15872, 15873, 15874, 15875, 15876, 15877, 15878, 15879, 15880,
15881, 15882, 15883, 15884, 15885, 15886, 15887, 15888, 15889,
15890, 15891, 15892, 15893, 15894, 15895, 15896, 15897, 15898,
15899, 15900, 15901, 15902, 15903, 15904, 15905, 15906, 15907,
15908, 15909, 15910, 15911, 15912, 15913, 15914, 15915, 15916,
15917, 15918, 15919, 15920, 15921, 15922, 15923, 15924, 15925,
15926, 15927, 15928, 15929, 15930, 15931, 15932, 15933, 15934,
15935, 15936, 15937, 15938, 15939, 15940, 15941, 15942, 15943,
15944, 15945, 15946, 15947, 15948, 15949, 15950, 15951, 15952,
15953, 15954, 15955, 15956, 15957, 15958, 15959, 15960, 15961,
15962, 15963, 15964, 15965, 15966, 15967, 15968, 15969, 15970,
15971, 15972, 15973, 15974, 15975, 15976, 15977, 15978, 15979,
15980, 15981, 15982, 15983, 15984, 15985, 15986, 15987, 15988,
15989, 15990, 15991, 15992, 15993, 15994, 15995, 15996, 15997,
15998, 15999, 16000, 16001, 16002, 16003, 16004, 16005, 16006,
16007, 16008, 16009, 16010, 16011, 16012, 16013, 16014, 16015,
16016, 16017, 16018, 16019, 16020, 16021, 16022, 16023, 16024,
16025, 16026, 16027, 16028, 16029, 16030, 16031, 16032, 16033,
16034, 16035, 16036, 16037, 16038, 16039, 16040, 16041, 16042,
16043, 16044, 16045, 16046, 16047, 16048, 16049, 16050, 16051,
16052, 16053, 16054, 16055, 16056, 16057, 16058, 16059, 16060,
16061, 16062, 16063, 16064, 16065, 16066, 16067, 16068, 16069,
16070, 16071, 16072, 16073, 16074, 16075, 16076, 16077, 16078,
16079, 16080, 16081, 16082, 16083, 16084, 16085, 16086, 16087,
16088, 16089, 16090, 16091, 16092, 16093, 16094, 16095, 16096,
16097, 16098, 16099, 16100, 16101, 16102, 16103, 16104, 16105,
16106, 16107, 16108, 16109, 16110, 16111, 16112, 16113, 16114,
16115, 16116, 16117, 16118), class = "Date"), spend = c(7984.39,
11476.06, 6555.57, 3981.45, 3963.83, 4827.72, 6309.32, 13503.36,
17075.89, 33353.71, 29324.34, 7968.68, 5540.63, 12113.45, 15596.38,
19328.67, 20224.68, 18977.55, 16128.27, 10633.56, 11887.79, 17881.11,
12613.46, 11607.55, 38232.11, 7861.25, 9397.88, 12056.02, 15115.87,
12275.93, 14537.35, 9594.26, 8215.83, 9632.52, 9993.15, 13478.37,
28509.38, 12016.33, 8907.76, 8757.43, 9513.09, 10299.5, 10385.03,
12515.62, 9008.95, 17825.68, 9320.47, 11189.58, 12902.31, 13341.35,
18675.32, 16989.53, 10114.53, 9876.65, 11203.39, 11718.73, 26264.95,
12414.19, 12275.16, 9242.85, 8883.97, 10095.72, 11581.55, 14815.78,
25064.12, 9297.07, 8047.91, 6876.37, 8881.63, 10982.85, 9975.33,
24124.62, 8514.66, 15719.84, 5807.39, 8422.38, 15184.95, 14757.58,
11087.61, 11070.78, 10425.67, 15517.8, 11257.69, 11915.47, 11720.37,
34064.62, 6493.41, 5757.4, 4387.54, 6520.58, 7806.81, 6356.63,
10916.36, 9013.43, 9722.41, 6044.25, 7971.7, 23933.54, 8627.85,
9722.77, 18660.13, 13011.36, 11445.11, 14219.2, 17138.92, 16016.68,
11434, 31379.03, 8494.25, 12493.85, 7708.1, 21583.05, 9026.17,
9379.35, 8287.13, 7298.16, 6097.03, 8076.57, 12871.87, 11346.89,
9115.82, 7737.98, 15065.38, 5262.73, 6522.58, 12743.94, 23945.16,
16109.26, 6985.89, 6345.08, 6246.93, 6824.66, 8491.42, 9654.99,
18976.58, 19565.68, 8075.47, 7219.79, 8629.04, 12491.64, 11915.89,
27533.16, 13554.35, 10102.21, 20029.15, 11641.82, 15855.19, 14139.17,
15376.63, 14625.99, 9098.87, 9396.64, 12015.84, 17532.75, 15131.65,
15815.5, 16048.65, 9769.63, 9582.12, 11201.8, 12810, 18857.38,
11822.71, 19289.08, 8911.29, 9437.55, 10987.14, 12995.65, 16675.26,
9741.82, 9723.57, 10328.24, 7738.04, 8432.16, 23021.73, 10367.28,
8210.53, 10468.4, 8024.25, 7296.25, 7445.34, 8539.59, 12386.23,
15335.72, 9013.49, 7994.95, 7759.46, 8789.38, 11242.38, 28653.23,
9750.96, 14398.62, 9248.74, 6766.08, 8159.14, 9899.38, 9453.35,
17588.96, 8958.16, 8256.61, 6240.4, 7235.24, 23841.62, 9002.73,
11839.47, 8693.31, 7161.37, 7046.39, 9221.53, 10004.93, 8698.76,
7948.68, 9013.27, 18536.68, 7980.38, 8968.95, 23594.14, 17744.66,
12615.73, 13646.05, 10512.58, 9066.02, 9665.15, 13183.2, 23864.45,
12017.52, 10831.07, 8954.76, 7276.41, 7882.9, 16616.41, 15384.68,
11046.53, 10621.01, 8094.74, 5451.26, 6237.79, 10717.69, 7076.38,
7044.62, 7047.45, 7774.77, 6496.21, 6340.9, 7110.53, 7691.28,
17482.02, 5576.19, 3763.79, 11477.68, 5710.5, 6519.51, 20022.61,
13153.68, 6526.28, 5885.28, 5656.17, 6270.04, 9795.38, 6320.95,
5741.98, 10808.72, 5150.87, 5416.52, 6305.05, 20953.12, 6569.02,
6360.21, 9376.68, 4973.93, 5034.48, 6380.45, 15307.28, 14386.65,
17705.88, 4779.52, 4784.79, 4737.05, 5350.28, 12112.11, 13153.72,
6049.69, 5430.46, 4627.59, 3637.2, 5482.43, 16705.15, 12221.16,
13198.88, 6484.54, 5590.86, 4979.09, 5771.75, 7311.92, 16111.86,
8047.77, 11706.91, 6042.14, 5670.74, 6905.07, 11261.89, 9700.4,
6643.03, 5693.85, 14778.67, 9128.14, 3682.01, 7911.5, 17742.85,
5093.31, 7867.97, 3202.78, 2843.35, 2598.77, 10930.81, 11204.67,
7289.62, 4000.17, 4178.89, 4507.33, 6671.48, 10317.48, 9368.98,
6156.41, 8375.24, 2762.76, 2457.59, 4707.51, 4584.52, 3749.82,
11667.82, 4271.67, 3614.3, 3715.83, 4510.57, 4872.36, 21805.71,
4757.04, 6515.92, 2834.25, 2685.19, 3509.28, 4479.35, 17817.99,
10357.67, 3412.15, 3044.95, 2840.24, 3348.91, 13671.68, 2027.42,
1616.25, 1177.73, 995.25, 1062.25, 1578.07, 1649.8, 1410.06,
1592.03, 3995.24, 6489.87, 6895.21, 8298.58, 7698.68, 5782.07,
7671.08, 19539.4, 7023.84, 6509.9, 6643.28, 19850.3, 6856.67,
13142.15, 5524.75, 5063.2, 4916.81, 6117.54, 6717.86, 9393.95,
10462.44, 10511.15, 4497.94, 4038.31, 5503.91, 5554.82, 5801.11,
12992.82, 4778.61, 4067.41, 4359.53, 6148.1, 9236.51, 5773.16,
11313.13, 4702.37, 4167.3, 4067.75, 4469.11, 9278.41, 9911.18,
5161.13, 4477.78, 4459.53, 4080.14, 5084.67, 7735.34, 10676.6,
5507.86, 8286.12, 4332.23, 4737.52, 5952.09, 7134.44)), .Names = c("TKDate",
"spend"), row.names = c(NA, 413L), class = "data.frame")
The code:
library(forecast)
explaining<-rep(1:length(data$TKDate))
predic<-rep((length(data$TKDate)+1):(length(data$TKDate)+31))
modArima <- auto.arima(data[,2],xreg=explaining)
fit<-forecast(modArima,h=30,xreg=explaining,newdata=predic)
plot(fit)
I get this weird jump:
Can anyone explain to me this weird jump? Why doesn't the forecast continue from the last observed data point (or at least close to it)?
This is a hard to find error, I'll admit.
forecast.Arima() takes the new values of the external regressors not in a newdata parameter (as does predict.lm()), but in the xreg parameter. So instead of
fit <- forecast(modArima,h=30,xreg=explaining,newdata=predic)
where you forecasted using the values of explaining, not those of predic (unfortunately, forecast.Arima() does not throw a warning if you feed data to the nonexistent newdata parameter), do this:
fit <- forecast(modArima,h=30,xreg=predic)
and plot (with in-sample fits thrown in for good measure - EDIT: somewhat confusingly, the in-sample fits are not returned by auto.arima() or arima() as they are by lm(), but by forecast.Arima()):
plot(fit)
lines(fit$fitted,col="red")
Is there an R function or library that will give me the monthly (or any other specified timeframe) time weighted rate of return (twrr) for my portfolio?
I am including a dput dump of sample data below of the date and portfolio ending balance below. Not sure why the dates were dput'ed the way they were, but the first date 12053 is '2003-01-01' and the last date 12195 is '2003-05-23'.
portfolio.df <- structure(
list(
Date = structure(c(12053, 12054, 12055, 12058,
12059, 12060, 12061, 12062, 12065, 12066, 12067, 12068, 12069,
12073, 12074, 12075, 12076, 12079, 12080, 12081, 12082, 12083,
12086, 12087, 12088, 12089, 12090, 12093, 12094, 12095, 12096,
12097, 12101, 12102, 12103, 12104, 12107, 12108, 12109, 12110,
12111, 12114, 12115, 12116, 12117, 12118, 12121, 12122, 12123,
12124, 12125, 12128, 12129, 12130, 12131, 12132, 12135, 12136,
12137, 12138, 12139, 12142, 12143, 12144, 12145, 12146, 12149,
12150, 12151, 12152, 12153, 12156, 12157, 12158, 12159, 12163,
12164, 12165, 12166, 12167, 12170, 12171, 12172, 12173, 12174,
12177, 12178, 12179, 12180, 12181, 12184, 12185, 12186, 12187,
12188, 12191, 12192, 12193, 12194, 12195),
class = "Date"),
Ending_Balance = c(56250000L,
56852500L, 57080000L, 57355000L, 57477500L, 56817500L, 57885000L,
57810000L, 57732500L, 57670000L, 57520000L, 57285000L, 57270000L,
56655000L, 55802500L, 56337500L, 55642500L, 54510000L, 54987500L,
55802500L, 56065000L, 56865000L, 56635000L, 56497500L, 56640000L,
56155000L, 55757500L, 55972500L, 55865000L, 55535000L, 55885000L,
56840000L, 56902500L, 56945000L, 56622500L, 57012500L, 57200000L,
58072500L, 57612500L, 57447500L, 57157500L, 57032500L, 57405000L,
57502500L, 56785000L, 57007500L, 56342500L, 55697500L, 56655000L,
56900000L, 57002500L, 57465000L, 57467500L, 57382500L, 57982500L,
56562500L, 58065000L, 58935000L, 58502500L, 58200000L, 57767500L,
57757500L, 58055000L, 58305000L, 58277500L, 58295000L, 59047500L,
58907500L, 59125000L, 59072500L, 59107500L, 59315000L, 59690000L,
58957500L, 59407500L, 59385000L, 59965000L, 60297500L, 59890000L,
59822500L, 60367500L, 60407500L, 60380000L, 60815000L, 61155000L,
61080000L, 61132500L, 61265000L, 60912500L, 61107500L, 61445000L,
61345000L, 61137500L, 61035000L, 60707500L, 61340000L, 61365000L,
61402500L, 61640000L, 61675000L)),
.Names = c("Date", "Ending_Balance"),
row.names = c(NA, 100L),
class = "data.frame")