Interest Earned Quarterly Using R

Question:

I'm trying to make a little rule in R to accumulate interest rates every three months, as in the time series below:

juros <- structure(c(0.423918702462353, 0.235602849515353, -0.162734895728985, 
0.432584842873254, 0.692919368945965, 0.835769981698709, 0.78885919203314, 
0.573766622312642, 0.292083372033511, 0.192391447159396, -0.510338254373842, 
-0.424247901166525, -0.340828852520887, -0.186667907743088, -0.16417059569588, 
-0.556535950716085, -0.618570097983526, -0.638168735522086, -0.585766634008056, 
-0.444177991447651, -0.0256138665180126, 0.128958521993413, 0.367298503358709, 
0.241148618494771, 0.0260497602656448, -0.0553575199214516, -0.201637309733446, 
-0.14990043807451, -0.0915875027913019, -0.247852955793209, -0.0431090940026737, 
0.00669199568818972, -0.0335118767566783, 0.0337914342678665, 
0.11477187517003, 0.155361421613399, 0.262487401360776, 0.296836204226596, 
0.141629962364754, 0.162497677255579, 0.121745279216505, 0.0203172447182365, 
-0.0270543086627946, -0.00675124079669272, 0.033879799012814, 
-0.0305303068612233, -0.067812926911758, -0.0677774453752877, 
-0.0373062627875294, -0.0711329850664776, -0.182708258298417, 
-0.223378660714272, -0.195586079591939, -0.312924389144267, 0.0764019271997563, 
-0.0900697481022798, -0.0401831093562328, 0.0942967574599733, 
0.0950378276245569, 0.129285375041879, 0.509580459976849, 0.553524731319305, 
0.534749138959858, 0.476910564607436, 0.546134449452785, 0.552681737040681, 
0.375155524278616, 0.425368085876844, 0.39877158980957, 0.398979729759308, 
0.381695899507174, 0.398563676788215, 0.518028983511603, 0.511963900981681, 
0.46734950961608, 0.436099754473606, 0.32792367346606, 0.348457976931074, 
0.387204045283968, 0.474046486643854, 0.492960878951088, 0.547460881988863, 
0.525397164001524, 0.50425713378547, 0.546305398813566, 0.525120360630025, 
0.390345538985159, 0.301708863554652, 0.192092929104909, 0.236662354143917, 
0.247503634544303, 0.314424707228641, 0.26560681691702, 0.246717063322467, 
0.0956896085426968, 0.164912566318318, 0.199692573340141, 0.254232082026942, 
0.199692573340141, 0.117092119393506, -0.0103715730366816, 0.184455608538078, 
0.258102810139533, 0.404369823889505, 0.377800663049443, 0.391092042732633, 
0.374294584777426, 0.415046114290809, 0.434743529753613, 0.339787375717895, 
0.306817893015092, 0.307290660549823, 0.49811008717731, 0.479284845204001, 
0.561205850613946, 0.54018270644082, 0.585226377790377, 0.694211291207312, 
0.677677072884187, 0.71020899457058, 0.722648156787328, 0.7158005319663, 
0.710938254493535, 0.705349741866711, 0.729124913991034, 0.709684959304074, 
0.753695965260011, 0.750074947128555, 0.716168173012596, 0.649936380339839, 
0.570721457515211, 0.710453553899895, 0.773410573899134, 0.678048588205535, 
0.694805666533002, 0.643924247067035, 0.610966587184186, 0.603660047054932, 
0.570897291756442, 0.588846145944536, 0.576673187878668, 0.595031932346826, 
0.604785960329671, 0.723793577640682, 0.72792661469079, 0.700687246931885, 
0.700687246931885, 0.739559474010145, 0.67859230326053, 0.689255275462461, 
0.763368943590703, 0.756222007220669, 0.676559182880387, 0.560213119003167, 
0.356810954325164, 0.401405405205222, 0.381003098501488, 0.360128638666578, 
0.318278279599152, 0.332425581930249, 0.304131177235595, 0.318278279599152, 
0.325262760268086, 0.311290020657212, 0.318278279599152, 0.307800066629725, 
0.212353868379756, 0.255107212878689, 0.265925649887141, 0.177106025275733, 
0.180529989050162, 0.130865535573723, 0.0883272323008656, 0.0564715816388354, 
0.140795198820672, 0.0281895052400792, 0.0421921478892998, 0.0667463215098731, 
-0.0419636437402947, 0.00349829993481024, 0.0418277788163325, 
0.0418052240240672, 0.0348470721516668, 0.0904563581569323, 0.0313890936862063, 
0.0626572305097568, 0.114955106877387, 0.0871352767901655, 0.108017499784952, 
0.101076167258839, 0.108017499784952, 0.0977303357958085, 0.10842689404863, 
0.115481793826257, 0.0822764109638929, 0.080544840437032, 0.0840412498173893, 
0.043801247848091, 0.0455598672190904, 0.0455834550668462, 0.0560577830060233, 
0.0945537344324573, 0.0699774106324455, 0.0821984774654506, 0.0857134121520021, 
0.074073409046108, 0.0524388713658452, 0.0681450640477346, 0.095992664279413, 
0.111631762173668, 0.158494380678478, 0.180915110922793, 0.168803317142761, 
0.172266523307181, 0.170082525166264, 0.156071747971484, 0.22242699556953, 
0.19989467545433, 0.208524656773983, 0.194674878036921, 0.201682092632356, 
0.218857606642782, 0.215475902591281, 0.222367294946118, 0.238401179513411, 
0.22819983535882, 0.137685306313089, 0.134878223419803, 0.112314258336315, 
0.0667895931863125, 0.0439862919651115, 0.0440042949259439, 0.0475110384400442, 
-0.0263854682204449, -0.0035108607572864, -0.0298884214667878, 
-0.0123025098048116, 0.00351589172698918, -0.0662419053742875, 
-0.00525215915972216, 0.0122314180305129, 0.0209477746084435, 
-0.0156984996480565, -0.0209110500400107, -0.0591897890236616, 
-0.066141527794065, -0.0661704956883273, -0.0661771863950118, 
-0.0591897890236616, -0.0714097901398302, -0.0818880031092556, 
-0.0697036910313752, 0.0315252519911766, 0.0438369148226236, 
0.0614590730762542, 0.11280494012109, 0.130746713807923, 0.146828790065185, 
0.155767129858968, 0.152525583524801, 0.24063567062853, 0.220035929450552, 
0.197288824485089, 0.259929382533096, 0.24398428618629, 0.244018202988462, 
0.25112574649254, 0.238715321125485, 0.247601193160177, 0.247601193160177, 
0.235185137919783, 0.240569209284116, 0.240490709375353, 0.247877315076064, 
0.294795144531244, 0.302070238642897, 0.302154441478385, 0.270111818360355, 
0.271978446852488, 0.266627620354437, 0.268369963008474, 0.293840808923635, 
0.265292615847021, 0.284877650452703, 0.288489982425585, 0.308091238007185, 
0.293717426235974, 0.32592194887984, 0.338412771293948, 0.350846104458317, 
0.356272283339529, 0.370347056778704, 0.382654429147362, 0.369430730787539, 
0.372441983776321, 0.361351375839648, 0.344977819864015, 0.319767447218298, 
0.287227401102985, 0.27992459169837, 0.254755471546481, 0.235073918619419, 
0.218851647037059, 0.200831019463993, 0.193694838156897, 0.161601503933666, 
0.124303054667547, 0.0958409896728516, 0.0780327055761674, 0.0513861047753143, 
0.0283209486273594, 0.0194614768269646, 0.00176492702816183, 
-0.014141077913234, -0.0282550671163316, -0.0493991056446287, 
-0.0528930576312948, -0.0810173182759198, -0.077760794874191, 
-0.0473015931177909, -0.0450452016998044, -0.0281348266839445, 
-0.01518254558033, -0.00674676896027744, 0.0129225861866435, 
0.0123610034540321, 0.0174143690314598, 0.0286345050729782, 0.036483863120999, 
0.067268687430117, 0.065591233088698, 0.0987821919502341, 0.111547105481758, 
0.144403698576412, 0.0812178704976439, 0.14395770137412, 0.27271854092554, 
0.280595281532593, 0.330069513549122, 0.296964629741233, 0.332685305630141, 
0.335964539842118, 0.32482297615365, 0.322604178832509, 0.126165135128718, 
0.332304356256898, 0.109306932287851, 0.205018303653607, 0.133107512709973, 
0.100450497004381, 0.0362729467176992, 0.0304681925676769, 0.0134944449452262, 
0.0211145189774021, 0.0187924564046024, 0.0170162883912387, 0.0311100997773854
), .Tsp = c(1980, 2009.66666666667, 12), class = "ts")

What I need is to accumulate January, February and March and keep that amount. Then I accumulate April, May and June and keep that amount. I keep repeating this process until I accumulate all quarters.

Could you help me build a function that simplifies this process?

I managed to do it in excel, but it takes a lot of work.

Answer:

Try this:

library(zoo)
jt=rollapply(juros,3,function(x)prod(1+x)-1,by=3,align="right")

jurost=ts(na.omit(as.numeric(jt)),end=c(2009,3),frequency = 4)
jurost

plot(jurost)
Scroll to Top