I am trying a loop for the function "bnd" (beveridge&nelson decomposition) on 7 different GDP series but I don't seem lucky because instead of getting two columns for each country (namely, trend and cycle), I get just two columns that "merge" all the countries. Before this loop I ran another one to set my data as time series and it works well. I don't think that "bnd" function is not suitable for a loop. I hope you can help me. Here the data (just two countries):
library('tseries')
library('tis')
library('remotes')
library('tsm')
df<-data.frame(dati[,1],dati[,2])```
names(df) <- c('USA','UK')
dput(df)
structure(list(USA = c(15.0405485762856, 15.0573809005081, 15.0763953681697,
15.0958166414917, 15.1134954385154, 15.122491569432, 15.1347017979415,
15.1379850092362, 15.1488419139525, 15.1600006620611, 15.1817467513144,
15.1882813132358, 15.2091449321336, 15.2199731412124, 15.2354753677698,
15.2385595360757, 15.2624690650642, 15.2750226488582, 15.2970117866263,
15.3197907962428, 15.3438415630993, 15.3472519084356, 15.3556852980351,
15.3638555532846, 15.3726724363791, 15.3732849583167, 15.3827018067186,
15.3902137245124, 15.4104034633952, 15.4269735115053, 15.4346933585039,
15.4386186718348, 15.4541485319914, 15.4571839936372, 15.463767396926,
15.4588786143873, 15.4573926612869, 15.458810852301, 15.4679844000527,
15.4572101301261, 15.4840077592963, 15.489404291006, 15.497600456837,
15.4999554745998, 15.5181779668429, 15.540621805313, 15.5500213999882,
15.5666273672419, 15.5910742220914, 15.6019019054576, 15.5966290020499,
15.6060764879613, 15.5974416641194, 15.5998156150008, 15.5903193297934,
15.5864303648738, 15.5741826618656, 15.5813026913411, 15.5982744285801,
15.6116496551524, 15.6338881459843, 15.6411983170559, 15.6466705219479,
15.6538928406601, 15.6656385103982, 15.684893038146, 15.7027756945523,
15.7027975660452, 15.705984259801, 15.7438988396621, 15.7539038516524,
15.7672557804011, 15.7690520028498, 15.7701207220015, 15.7775219997698,
15.7800196135149, 15.7831598306752, 15.7623398236292, 15.7611506303989,
15.7796281166227, 15.7990329990351, 15.791594622768, 15.8034987306626,
15.7925434791094, 15.7768861346312, 15.7814377260506, 15.7776074473486,
15.7780067922283, 15.7910992538048, 15.8136002508023, 15.8333937486618,
15.854041306431, 15.8734004004826, 15.8905307433255, 15.9001252099752,
15.9083003305448, 15.9179445433314, 15.9267105626027, 15.941866374142,
15.9492718063979, 15.9585635430848, 15.9630572706425, 15.9725820889845,
15.9779384752624, 15.9853625628321, 15.9960937561775, 16.0047324553226,
16.0217572336601, 16.0269101973349, 16.0399633065327, 16.0458054953898,
16.0590424572033, 16.0691571556469, 16.0767608652358, 16.0841423619861,
16.0861108909309, 16.0969795162019, 16.1006020291683, 16.1012670372421,
16.0921215503467, 16.0874321340242, 16.0951990813546, 16.1002393240273,
16.1037182522379, 16.1156176888601, 16.1264028528002, 16.1362370574014,
16.1466146701033, 16.1482872735988, 16.1540945747856, 16.1588563667646,
16.1723658348503, 16.1820212338768, 16.1954807060924, 16.2013088418771,
16.2126989718105, 16.21624025548, 16.2192192227199, 16.2276900193541,
16.2344572799817, 16.2419188413636, 16.2584597572751, 16.2673896803369,
16.2777199586844, 16.2841548539144, 16.3006382674734, 16.3130691630708,
16.3216206596996, 16.3315658411832, 16.3407813193069, 16.3532291960434,
16.3692588344529, 16.3786790434386, 16.3863412004173, 16.3993528291662,
16.4162058658977, 16.4198169396351, 16.4379629471809, 16.4392976089703,
16.4455071854765, 16.4426513348683, 16.448479873787, 16.4443208561178,
16.4470408672589, 16.4557479921495, 16.4617870456656, 16.4662229060029,
16.467769935101, 16.4733022708858, 16.4818680775659, 16.4987075880863,
16.5101238444331, 16.5154471237304, 16.5230389960427, 16.5324478738768,
16.5424147799567, 16.55342359533, 16.5580285891719, 16.5669024383357,
16.5731972158331, 16.5864123808604, 16.5887466517247, 16.5902917853112,
16.5987743586431, 16.6011265212089, 16.6068388984735, 16.6122569077906,
16.6183169278538, 16.6125533518584, 16.6177039357858, 16.6122756734865,
16.5904000242768, 16.5791095758879, 16.577668640747, 16.581303138957,
16.5922270957242, 16.5960675092837, 16.6052455450035, 16.6125906165414,
16.6175970885915, 16.6151897808266, 16.6223140759301, 16.6220362507157,
16.6335624183687, 16.6413618512186, 16.6456546391189, 16.6470032114808,
16.6481412960746, 16.6569590560035, 16.65819226004, 16.6659958840795,
16.6739466819342, 16.6711156264096, 16.6845616356196, 16.6966970255319,
16.7023091852386, 16.7117559733593, 16.7184993176822, 16.7221188032863,
16.7237211426864, 16.729369296134, 16.7324836249347, 16.7379128206864,
16.7441860860144, 16.7498268368571, 16.7540871886875, 16.7613490932745,
16.7708604894622, 16.7801341473643, 16.7867981734508, 16.7920363860969,
16.795314222412, 16.802539284274, 16.806239591354, 16.8125885415187,
16.8184337448169), UK = c(13.1810632123547, 13.1854261052651,
13.1803243542997, 13.1785858860068, 13.1829300891261, 13.1925849823629,
13.1985396340906, 13.1949987778968, 13.1995697244227, 13.2420205765932,
13.2467417359085, 13.2693293965264, 13.2752017128014, 13.2927532707995,
13.2967217915718, 13.3116692258944, 13.3089367620903, 13.3097423492698,
13.3180590239938, 13.3241842437859, 13.3258790564668, 13.3317661486634,
13.3348677523761, 13.3306792645766, 13.3441466308576, 13.3581282541431,
13.3626964171079, 13.367605480453, 13.403577291031, 13.399643729882,
13.4193157631532, 13.4231206664774, 13.4208394621971, 13.428602096582,
13.4341511334164, 13.4389963896051, 13.4325786797203, 13.4560107331397,
13.4659310118941, 13.4745704939483, 13.4688431425527, 13.4864720725675,
13.5039186096762, 13.507660503724, 13.5090980199436, 13.5352637916093,
13.5380844927547, 13.5536485503339, 13.6022152806533, 13.6034989809873,
13.5939701194416, 13.5896501197756, 13.5618833454811, 13.5761740989271,
13.5825715340081, 13.5680164974583, 13.5685981726999, 13.5520392937077,
13.5486135575393, 13.5600142993627, 13.5757162804241, 13.5740624076444,
13.5865037006537, 13.6074945317768, 13.6077789739949, 13.6008085855243,
13.6077500513625, 13.6247607350949, 13.6336403718249, 13.6452223588725,
13.6588772654464, 13.6679052723595, 13.6643493977878, 13.7075738574043,
13.6851271764794, 13.6956500155275, 13.6861126991878, 13.665906080457,
13.6649186417788, 13.6539045180125, 13.6526470878404, 13.6546913345063,
13.6654056573626, 13.6666562458703, 13.6685995104384, 13.6792025325712,
13.6817888123941, 13.688792329382, 13.7069367229177, 13.7154520368595,
13.7263388584202, 13.7349417037604, 13.7418410689019, 13.7330467418365,
13.7394944524803, 13.7590767501089, 13.7672433957944, 13.78820030453,
13.789073264745, 13.791487032014, 13.8027634650623, 13.8101572506104,
13.8146983382952, 13.8324051352622, 13.8416575047612, 13.8562200875388,
13.8802265006008, 13.8914872026226, 13.9091519470015, 13.9150294330898,
13.9301448789597, 13.9387193702087, 13.9431170924187, 13.9496793368158,
13.9506753647664, 13.9516259709347, 13.9583433038203, 13.9634667012705,
13.9529716868778, 13.9495252232684, 13.9466028492655, 13.9453413967776,
13.9431550090059, 13.944890737897, 13.9449629945934, 13.9437821475864,
13.9501312662546, 13.9570727894313, 13.9642803695923, 13.9692257666423,
13.9770732205457, 13.9836885255713, 13.9953769831509, 14.0069789054354,
14.0183422621475, 14.024389102803, 14.0286258732171, 14.0323152266167,
14.0415138610078, 14.0428001231583, 14.0519403499356, 14.0554955471329,
14.064267560111, 14.0719425777462, 14.0856545330355, 14.0942500287053,
14.1015726942665, 14.113379186136, 14.1220202556352, 14.1308044633406,
14.1376098450501, 14.1476531999577, 14.1539405296866, 14.1560015494078,
14.1742225211691, 14.1885289090483, 14.1961187571344, 14.2016997200613,
14.2044294603581, 14.2060022527066, 14.2202065585524, 14.2285379452733,
14.2364740749638, 14.2401792083942, 14.2445976339217, 14.2496488433675,
14.2572511556101, 14.2658051373865, 14.2724209635553, 14.2817389471675,
14.291990358079, 14.3003981848027, 14.3058212894003, 14.3093933374033,
14.3110505285343, 14.3143352921273, 14.3227043146742, 14.3350604226302,
14.3463863326829, 14.3612748430701, 14.3652603916933, 14.3677726240586,
14.368800074652, 14.3739820444078, 14.3832721978594, 14.3894525901468,
14.3971854964449, 14.4018851596092, 14.4072955463837, 14.401732636551,
14.3859721044043, 14.3651021785073, 14.3474750919849, 14.345038128183,
14.3457481786539, 14.3487441977113, 14.3550953594828, 14.3650660119783,
14.3716762731291, 14.3723093195178, 14.3785843885694, 14.3795785174956,
14.3826749290694, 14.3845321746896, 14.390947134677, 14.3901582582339,
14.4022402303171, 14.4007014063196, 14.4071265077582, 14.4124720392108,
14.4218850497372, 14.4271800906179, 14.4337480987979, 14.4402837329372,
14.4460267359323, 14.4516042781311, 14.4568580948825, 14.4639638621813,
14.468254591812, 14.4757011386904, 14.4773632393449, 14.4826566398059,
14.4871542106227, 14.4936293324316, 14.4993772378892, 14.501888001734,
14.5052868184714, 14.509271153093, 14.5098658718512, 14.5151188320087,
14.5209731317829, 14.5231119220664, 14.5296716436497, 14.5291058575806,
14.5342093495298, 14.5316194393166)), class = "data.frame", row.names = c(NA,
-236L))
This is the loop that I ran before:
loop.ts <- 1:2
k <- names(df)
x <- df[,]
for(i in loop.ts){
k=ts(x,start = 1961,frequency = 4)
}
This is the invicted one:
loop.bnd <- 1:2
y <- names(df)
for(j in loop.bnd){
y = bnd(k[,],nlag=2)
}
I tried several ways,i.e. by using df[,]
instead of k[,]
but I get the same two-columns-output instead two for each country. Where did I wrong?
It's always a red flag when you loop over
i
but don't usei
inside the loop. Try this:Notice that I use
i
on the left-hand side of the assignment so I don't overwrite results from previous iterations, and on the right-hand side so that the input changes each iteration.We can also use the
apply
family shorthand for the same result:Both of the above methods will give a
list
of results. You can combine the data frames in each list element if you'd like, with, e.g.,dplyr::bind_rows(results, .id = "country")