ARDL model in R

510 views Asked by At

I have this data frame on which I want to apply the ARDL model but every time I run it, it gives me an error. If anyone could please help me or point out what I am doing wrong would be highly appreciated. Error:'list' object cannot be coerced to type 'double' If I remove the prediction part of the code in the loop out then it runs otherwise no.

Data:

structure(list(Industrialproduction = c(1.65801981343852, 1.79541527049647, 
-0.0326429293424051, 0.104752527715549, -0.992082392187777, -2.26823002723453, 
-2.33809212404366, -3.02972688245404, -2.14713572609871, -1.29947561814794, 
0.104752527715549, 0.228175565411677, 0.305023871901719, 0.218860619170459, 
0.216531882610155, 0.139683576120113, 0.25146293101472, 0.249134194454415, 
0.626389517223712, 1.13405408737005, 0.58214352257793, -0.0629165046263609, 
-0.619484542539089, -0.652086854383349, 0.591458468819148, 2.0259601899666, 
1.73021064680795, 0.561184893535192, 0.207216936368938, -0.489075295162048
), Householdconsumption = c(-1.5532531908672, -1.52804903107083, 
-1.51957878064746, -1.50015918211582, -1.4800165134261, -1.47578138821441, 
-1.46235294242126, -1.45274643889231, -1.43477298067686, -1.42299726667364, 
-1.41225451003912, -1.39892935998284, -1.38694705450587, -1.37909657850372, 
-1.36525494976309, -1.34924411054818, -1.33457611591258, -1.32538279533112, 
-1.31143787085362, -1.30255443748276, -1.29181168084824, -1.27838323505509, 
-1.27476788426463, -1.25586476441735, -1.24336598025603, -1.22942105577852, 
-1.21051793593124, -1.1953334626113, -1.17581056834279, -1.15804370160108
), Investmentgrowth = c(1.47348593810751, 2.17792802452104, 2.57620375293532, 
3.11977876989162, 2.03003410582649, 1.238671909303, 0.670447905897604, 
0.0127091622297187, -0.222104866574793, -0.1974558801257, -0.215618291193452, 
-0.31551155206609, 0.0762776009668517, 0.37206543835596, 0.593906316397791, 
0.867639797490343, 1.12321086751514, 0.272172177483322, 0.191738642754705, 
0.619852617923151, 0.675637166202676, 1.31910544403161, 1.23348264899792, 
0.702880782804304, 1.61748791157326, 0.308496999618827, 0.395417109728784, 
0.290334588551075, -0.659300047277115, -0.117022345397083), ConsumerPriceIndex = c(-2.03033282052684, 
-1.98504568168304, -1.91626049909952, -1.88507218649954, -1.82055937550506, 
-1.75220143008044, -1.72955786065854, -1.7026419196476, -1.65991820375721, 
-1.60053223866958, -1.57447077197644, -1.53473771619839, -1.51166690961758, 
-1.46381634782035, -1.46381634782035, -1.45057199589433, -1.43860935544502, 
-1.40400314557381, -1.3800778646752, -1.36341561547795, -1.35188021218754, 
-1.31727400231633, -1.29933004164237, -1.28394950392183, -1.24549815962049, 
-1.19380246339312, -1.19380246339312, -1.18183982294381, -1.16816823385889, 
-1.14595190159589), Employment = c(-1.02390309005026, -0.997493370038682, 
-0.974856467171615, -0.96353801573808, -0.978629284316127, -1.00881182147222, 
-1.07294971292891, -1.14840605581914, -1.21631676442034, -1.28045465587703, 
-1.32950127875568, -1.38232071877884, -1.41250325593493, -1.45023142738004, 
-1.5030508674032, -1.55587030742636, -1.58228002743794, -1.56341594171538, 
-1.49173241596967, -1.43136734165749, -1.38232071877884, -1.34081973018921, 
-1.30686437588861, -1.2691362044435, -1.24272648443192, -1.22386239870936, 
-1.20499831298681, -1.19745267869778, -1.18990704440876, -1.17481577583072
), Unemploymentrate = c(-0.815370914670033, -0.815370914670033, 
-0.772756573338144, -0.687527890674367, -0.517070525346812, -0.303998818687369, 
0.0795302532996298, 0.463059325286628, 0.803974055941737, 1.05966010393307, 
1.18750312792874, 1.3153461519244, 1.40057483458818, 1.44318917592007, 
1.57103219991573, 1.6988752239114, 1.74148956524329, 1.82671824790707, 
1.86933258923895, 1.91194693057084, 1.99717561323462, 1.95456127190273, 
1.91194693057084, 1.86933258923895, 1.78410390657518, 1.74148956524329, 
1.74148956524329, 1.74148956524329, 1.74148956524329, 1.78410390657518
), Stockmarketindex = c(-1.66493184730628, -1.66463355820282, 
-1.6694061838583, -1.6887949755837, -1.67417880951378, -1.66493184730628, 
-1.64285845364968, -1.63748924978726, -1.63152346771791, -1.61601243433759, 
-1.62615426385549, -1.62078505999307, -1.60616889392316, -1.58976299323243, 
-1.56082895019607, -1.53875555653946, -1.51548900646899, -1.47790457943206, 
-1.48088747046674, -1.47074564094884, -1.41615873501425, -1.43345950301538, 
-1.40631519459982, -1.36544958742475, -1.32577713666355, -1.32816344949129, 
-1.3189164872838, -1.2792440365226, -1.17722916313666, -1.1805103432748
), Economicgrowth = c(-0.419475860074214, 1.77103185704958, -1.24091625399564, 
0.219422224086891, -0.693289324714688, -1.14964509911548, -0.282569127753977, 
-0.738924902154767, -0.282569127753977, -0.14566239543374, 0.26505780152697, 
-0.191297972873819, -0.14566239543374, 0.26505780152697, -0.14566239543374, 
0.0825154917666544, 0.630142421047602, 0.128151069206733, 0.26505780152697, 
0.173786646646812, 0.0825154917666544, -0.647653747274609, 0.0368799143265754, 
0.675777998487681, 0.128151069206733, 0.72141357592776, -0.191297972873819, 
-0.14566239543374, 0.26505780152697, 0.0825154917666544), Consumptiongrowth = c(1.49948934326176, 
2.80290969177971, 2.43863500922448, 3.0742184397245, 3.29809558837824, 
1.78028441106475, 1.98708618397371, 1.30407115418265, 1.06311862978413, 
1.41221520056623, 1.19213257985578, 1.30976294609757, 0.943590999570694, 
0.717816586945313, 0.803193465669196, 0.856316856875168, 0.915132039996066, 
0.932207415740842, 0.877186760563229, 0.550857357440829, 0.359233696305002, 
0.484453118433365, 0.0879249483602165, 0.410459823539332, 0.437021519142318, 
0.419946143397541, 0.898056664251289, 0.717816586945313, 0.896159400279647, 
0.962563639287112), Governmentexpenditure = c(-1.40005492084802, 
-1.38413625639177, -1.36971935273328, -1.35410104043658, -1.33277603710839, 
-1.31355349889707, -1.28802356533516, -1.2715041965598, -1.26880102712384, 
-1.2558858842631, -1.24627461515744, -1.24417215004058, -1.22765278126522, 
-1.21503799056404, -1.2159390470427, -1.20692848225614, -1.18259995733244, 
-1.17989678789647, -1.18139854869423, -1.17058587095036, -1.17509115334364, 
-1.14956121978173, -1.13814783771876, -1.13093938588951, -1.13274149884682, 
-1.12853656861309, -1.1210277646243, -1.11622213007147, -1.09820100049835, 
-1.07867811012748), Longtermgovernmentbondyield = c(1.40229182288022, 
1.52084996657255, 1.52084996657255, 2.1284604529957, 2.10623080105339, 
1.8221852484572, 1.74561644732258, 1.8221852484572, 1.79254571253412, 
2.04695172920723, 2.32358739782265, 2.3705166630342, 1.87899435897644, 
1.81477536447643, 1.26150402724559, 1.27879375653405, 1.22939452999558, 
1.13306603824557, 1.19728503274558, 1.07872688905325, 1.0515573144571, 
1.28126371786097, 1.19234511009173, 1.10342650232249, 1.21704472336097, 
1.14294588355326, 1.05649723711094, 1.08119685038018, 0.811971065745526, 
0.752691993899364), BankRate = c(1.46586697149636, 1.35154387389459, 
1.66960408302535, 1.97274804858215, 2.29546045675764, 2.29546045675764, 
2.09326712428386, 1.92386866579365, 1.57025981463491, 1.25236650053715, 
1.3619748134568, 1.79713275011286, 1.62283175002842, 1.46140252936374, 
1.11755703763521, 0.767494705927617, 1.00504892351727, 0.836192873884299, 
0.743461821176297, 0.639548801257612, 0.614285065637952, 0.59615609267884, 
1.0233656533885, 0.831874464905546, 1.44483819733896, 1.35112663631211, 
1.16530987895099, 1.12197975600959, 1.27587783831036, 0.9050787987531
), ConsumerConfidenceIndex = c(0.846829650502804, 1.60472118016078, 
0.469774413325411, -0.621585188209513, -1.60430771883233, -1.68373613752742, 
-1.63528611498984, -1.66413766753932, -1.70435864332681, -1.66902678210495, 
-1.59673510529459, -1.59602090595954, -0.999706472923775, 0.0694026684451194, 
0.236084189726601, 0.627318384290896, 0.856229774103751, 1.00626414911988, 
1.18224076468833, 1.53172580694677, 1.23795881575313, 0.203420073079754, 
0.031697144727374, 0.197538431497049, -0.4751060554715, -0.723547646218374, 
-0.38459179268613, -0.144363494292252, -0.345373846847121, -0.204466519215588
), RealPersonalDisposableIncome = c(-1.61847984374121, -1.5861635599299, 
-1.57225712600034, -1.49848239094777, -1.53946529757862, -1.54497109045523, 
-1.52492940490883, -1.54175692919285, -1.53951841454553, -1.57492545682787, 
-1.59099234107728, -1.58530147051583, -1.59103377902389, -1.5730264259375, 
-1.58525138856384, -1.58529970465885, -1.58192644597089, -1.52325368596072, 
-1.53501582928257, -1.52925478110938, -1.50631522176011, -1.51590689703409, 
-1.46151870482198, -1.40729486382567, -1.3789215787237, -1.3693640758863, 
-1.36306692499173, -1.34201378673398, -1.29684320450097, -1.22228202649678
), PersonalDisposableIncome = c(-1.63374935499688, -1.61912533368493, 
-1.59170800880699, -1.55197024453754, -1.5482842190616, -1.53111502598353, 
-1.51272050030567, -1.496600325591, -1.47962148432076, -1.47543914833698, 
-1.46603002900108, -1.44363362658138, -1.43430337270118, -1.41678955114361, 
-1.41553667304378, -1.4009808145215, -1.39475625982839, -1.36509006246891, 
-1.3676348839483, -1.35648303480995, -1.33818372341837, -1.33674498698592, 
-1.3094343652563, -1.28373893947937, -1.27703100460648, -1.26432160075519, 
-1.25275608335059, -1.23583637919461, -1.22110869865322, -1.18194367906969
), SPPricechange = c(1.26871538917159, 0.851034944363406, -0.128615553459435, 
0.296659081254357, -2.70557732809419, 0.554861538044873, 0.509296398611252, 
-0.98675901279262, 0.782687235212975, -0.391880803520353, -1.49050694319765, 
-0.890565940654977, -0.386818010249951, -0.642489070405266, 0.0637705908158519, 
0.256156735091138, 0.709276732792142, 0.689025559710533, 0.129586903331082, 
-0.351378457357135, 0.213122992292719, 0.314378857700765, -0.217214435691474, 
0.43841729282562, -0.202026055880268, 0.177683439399903, -1.00701018587423, 
1.01304432901628, 1.20796186992677, 0.228311372103926), HouseStarts = c(-0.752212186140825, 
1.99649271779397, 1.8436488194287, 1.69950335430372, -0.603096187735683, 
0.0865653048881011, -0.685109986858511, -1.57110754404907, -1.21198651455668, 
-0.257644124763769, -0.214151958562269, -0.962217217228067, -0.0128453607153271, 
0.914159096036641, 0.712852498189699, 0.0766242383277583, 1.13783309364436, 
1.96915478475302, 1.07942932760234, 0.407164701459157, 0.581133366265157, 
1.28073592544928, 0.633323965706957, -0.424156989649512, -0.316047890805783, 
1.23848639256783, 0.96137916219827, 0.337577235536758, -0.0811901933176841, 
1.76039238698582), HouseCompleted = c(0.727121448038834, 1.51512215827386, 
1.27770235093872, 4.62342810215839, 1.63310378032383, 1.60397251562013, 
1.87343671412934, 2.03511523323486, 1.11893695830357, 0.288695914248185, 
0.362980639242614, 0.395025030416681, -0.395888806288709, -0.695940832736795, 
-0.378410047466491, 0.467853192175925, -0.349278782762793, 0.483875387762959, 
0.46930975541111, 1.47288182445349, 0.598943883342565, 0.362980639242614, 
0.829080874501776, 1.38985772004796, 0.143039590729697, 0.281413098072261, 
0.138669901024142, 0.973280634785079, -0.238579976888742, 0.502810709820363
), TermSpread = c(-0.982486215892947, -1.35440528815526, -1.77852352845439, 
-2.1243430166983, -2.91385543325514, -2.47668740094681, -1.8046231124728, 
-0.891137671828519, 0.83143487338641, 1.14462988160731, -0.499643911552399, 
-0.127724839290084, 0.43994111311029, 0.727036537312779, 0.916258521446237, 
0.629163097243749, 0.37469215306427, 0.857534457404817, 0.968457689483053, 
0.968457689483053, 1.18377925763492, 1.444775097819, 0.335542777036658, 
0.981507481492256, -1.19780778404481, -0.89766256783312, -0.656241415662847, 
-0.538793287580011, -1.4718534162381, -0.1799240073269), BuildingPermits = c(-0.757294212433685, 
1.98548228249596, 1.83296803797682, 1.68913346590998, -0.608499827536959, 
0.0796742026103982, -0.690336739230158, -1.5744233761582, -1.21607689919859, 
-0.263792835859544, -0.220394473597999, -0.966846304496574, -0.0195220539874191, 
0.905483038787227, 0.704610619176647, 0.0697545769506165, 1.12867461613232, 
1.95820331193156, 1.0703968153811, 0.399582130138359, 0.573175579184539, 
1.27126923499168, 0.625253613898393, -0.429946565660888, -0.322070636610762, 
1.22911082593761, 0.95260126067119, 0.330144750519887, -0.0877194803984184, 
1.74989117307615), Date = structure(c(289094400, 297043200, 304992000, 
312854400, 320716800, 328665600, 336614400, 344476800, 352252800, 
360201600, 368150400, 376012800, 383788800, 391737600, 399686400, 
407548800, 415324800, 423273600, 431222400, 439084800, 446947200, 
454896000, 462844800, 470707200, 478483200, 486432000, 494380800, 
502243200, 510019200, 517968000), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), NORTH = c(4.06976744186047, 5.51675977653633, 
7.2799470549305, 4.75015422578655, 4.59363957597172, 3.15315315315317, 
1.2008733624454, -0.377562028047452, -0.108283703302655, 0.650406504065032, 
0.969305331179318, 0.106666666666688, 3.09003729355352, 2.11886304909562, 
2.32793522267207, 5.68743818001977, -1.46934955545156, 3.95611702127658, 
5.19438987619354, -0.0912012507600199, 2.81677896109541, 3.97412590369087, 
1.30118326353028, 3.31553807249226, 1.32872294960955, 2.93700394923507, 
0.908853875665812, 1.81241002546971, -1.3414545718222, 4.81772747317361
), YORKSANDTHEHUMBER = c(4.0121120363361, 5.45851528384282, 9.52380952380951, 
6.04914933837431, 3.03030303030299, 5.42099192618225, 2.78993435448577, 
-0.53219797764768, 1.97966827180309, 1.15424973767052, 0.466804979253115, 
-1.96179659266907, 2.42232754081095, 0.719794344473031, -0.306278713629415, 
3.37941628264209, 2.74393263992076, 3.91920555341303, 1.91585099967527, 
0.892125625853447, 2.91888477848958, 3.78293078507868, 0.109815847271484, 
6.83486625601216, 0.722691730511011, 3.56008625759656, -0.227160867754524, 
2.69419041475355, -1.17134094520194, 2.78546324684064), NORTHWEST = c(6.57894736842105, 
6.95256660168939, 6.50060753341436, 5.5904164289789, 4.59211237169096, 
4.70041322314051, 2.96003946719288, -1.38955438428365, 0.242954324586984, 
2.18128938439167, -0.853889943073994, -2.15311004784691, 0.929095354523226, 
2.51937984496125, 0.189035916824195, 2.21698113207546, 2.51499769266268, 
3.5066396578888, 1.77437592415414, 0.948636868643719, 4.60125296308836, 
3.95775160859537, -0.237455720347246, 4.218042765725, 2.79306600771276, 
2.22545984338008, 0.709042970141798, 0.258269945161875, 0.663420142564747, 
2.23655612423752), EASTMIDS = c(4.98489425981872, 8.20143884892085, 
6.91489361702127, 5.22388059701494, 5.61465721040189, 4.64465584778958, 
2.03208556149733, 0.314465408805028, 2.82131661442007, 0, 2.79471544715448, 
-0.939199209095414, -1.14770459081835, 2.97829379101462, -0.68627450980392, 
3.40572556762095, 3.42243436754175, 4.89223242719342, 0.730408764905171, 
2.10107893242476, 2.31025926242835, 5.01798109893785, 0.382256908497274, 
4.64894882982943, 3.04374194526571, 2.25491999264298, 0.651125980286367, 
1.40105078809108, 2.87265165133409, 3.59418899472349), WESTMIDS = c(4.65838509316771, 
4.74777448071216, 8.66855524079319, 6.56934306569344, 3.22896281800389, 
3.17535545023698, 0.643086816720257, -1.36923779096303, 1.61962054604351, 
2.00364298724953, -0.491071428571428, -2.78151637505608, 0, 2.39963082602676, 
0.540784136998647, 1.83774092335275, 4.66989436619718, 1.82498633362771, 
2.51909973157134, 0.644511581067457, 3.9503702221333, 3.15724626520867, 
0.548671245147809, 4.19837410445824, 3.20983256145349, 1.12526319422872, 
1.4028740144042, 0.434226470984247, -0.194389516372279, 2.32714328889485
), EASTANGLIA = c(6.74525212835624, 8.58895705521476, 8.47457627118643, 
10.7291666666667, 4.8447789275635, 4.84522207267835, -0.299529311082601, 
1.45922746781116, 0.88832487309645, 0.29350104821803, -0.877926421404701, 
1.64487557992411, -2.69709543568468, 3.49680170575694, 3.25504738360115, 
2.39425379090184, 2.98519095869059, 4.36691137516082, 3.57868020304568, 
1.66275772744776, 3.79450451070863, 4.52162951167727, 2.28203256419209, 
4.17054552224914, 3.2439678284182, 4.76643873164257, 0.955633279171614, 
2.91614381581101, 0.848198902642676, 5.02010671012167), OUTERSEAST = c(6.7110371602884, 
7.53638253638255, 9.47317544707589, 8.56512141280351, 3.82269215128102, 
2.11515863689776, 1.64940544687381, -1.73584905660378, 1.34408602150539, 
1.78097764304659, 0.446760982874161, -1.26019273535953, 0.150150150150159, 
3.11094452773611, 1.4176663031625, 2.54480286738352, 5.56448794127927, 
4.89371564797033, 3.88257575757575, 1.85961713764815, 5.54859495256845, 
4.29879599796508, 2.00525702517411, 3.63679834232127, 3.44509381728699, 
3.46664684309643, 1.93988743863012, 2.50440502760482, 2.96578121060713, 
4.47634947134114), OUTERMET = c(4.54545454545458, 6.58505698607005, 
7.36633663366336, 7.08225746956843, 4.3747847054771, 1.68316831683168, 
1.00616682895164, -1.28534704370181, 2.01822916666665, 0.797702616464613, 
0.949667616334271, -0.940733772342415, 1.10794555238999, 2.19160926737633, 
2.84926470588237, 2.62138814417631, 5.02467343976781, 5.65213786241397, 
3.22555328833776, 3.73552294786995, 5.05948745510956, 4.28797321179426, 
2.86300392436674, 2.60339894216597, 4.28031183318191, 3.43199821714381, 
3.34554286721641, 3.04770569170409, 1.65167650683293, 4.62120252591965
), LONDON = c(8.11719500480309, 10.3065304309196, 6.32299637535239, 
7.65151515151515, 1.30190007037299, 2.1535255296978, -0.204012240734436, 
-0.306643952299836, 0.786056049213951, 1.18684299762631, 1.00536193029493, 
-2.85335102853352, 2.76639344262296, 2.06048521103356, 1.23738196027352, 
2.70183338694115, 3.30410272471031, 5.76322570865546, 4.73255747291176, 
1.98428989791171, 6.03563952552197, 4.88977753030802, 2.12581135535556, 
4.43247330120026, 5.42986425339366, 3.96781115879828, 3.43247538648888, 
4.0668901660281, 4.09587727708534, 4.81707991010573), SOUTHWEST = c(6.17577197149644, 
7.71812080536912, 7.63239875389407, 9.45489628557649, 2.46804759806079, 
2.19354838709679, 1.72558922558922, 0.248241621845247, 1.48576145274456, 
2.03334688897925, -0.677560781187733, -2.3274478330658, 1.80772391125718, 
2.42130750605327, 1.85185185185186, 0.928433268858785, 5.95247221157533, 
4.38447346525341, 3.30272049904696, 2.25107353730542, 3.86823714688802, 
2.04371722787289, 3.04596811639065, 4.19057346270538, 2.45646407565451, 
2.17525889239081, 2.83400809716597, 1.58015962290428, 2.77894958869438, 
4.08650146221331), WALES = c(6.09418282548476, 8.35509138381203, 
7.40963855421687, 7.01065619742007, 1.15303983228513, 3.47150259067357, 
-0.150225338007013, 0.852557673019058, 0.944803580308295, -1.13300492610835, 
0.946686596910786, -2.17176702862782, 3.98587285570131, 0.485201358563789, 
3.62143891839691, 1.63094128611373, 1.61852361302152, 4.32251951450617, 
1.28887158859911, 0.68747598104105, 3.71925360474978, 4.66941979801284, 
1.44927536231884, 1.05121293800539, 1.67663757954501, 2.9419480568152, 
-0.422309596621509, 2.67987715706347, 0.0249243368346056, 2.03260714794249
), SCOTLAND = c(5.15222482435597, 4.12026726057908, 5.40106951871658, 
8.67579908675796, -0.280112044817908, 2.94943820224719, 1.04592996816735, 
1.21512151215122, 1.33392618941751, 3.59806932865292, 0.974163490046604, 
0.125838926174496, 1.46627565982404, 3.42691990090835, -0.838323353293421, 
1.97262479871176, 3.40702724042636, 4.30649410147751, 2.44866586142527, 
1.93997856377279, 2.09581887638873, 4.22573890357352, 0.833278440155458, 
4.15155969296095, 2.01655899140689, 1.93980755633434, 0.325693606755129, 
0.796561260069754, -0.381713535919834, 2.90974405029185), NIRELAND = c(4.54545454545454, 
4.94752623688156, 4.42857142857145, 2.96397628818967, 6.06731620903454, 
0.0835073068893502, -1.66875260742594, -2.96987696224015, -1.18058592041975, 
-0.884955752212393, -1.74107142857143, -0.545206724216265, 1.96436729100047, 
-0.224014336917564, -1.84104176021554, 1.6010978956999, 1.42278253039172, 
1.97993429814437, 1.29287828660979, 1.61158623060724, 2.28387751649466, 
1.84005954349984, 1.79057208981284, 2.22177901874749, 2.88757950598978, 
-0.731975575530031, 3.07939176281808, -0.0593031875463392, -1.05696484201158, 
3.40717418194087), UK = c(5.76890543055322, 7.20302836425676, 
7.39543442582184, 7.22885986848197, 3.23472252213347, 2.95766398929048, 
1.20271423347285, -0.554061107319231, 0.98913965036942, 1.55113136643479, 
0.373986300291293, -1.61195434757029, 1.59052858167903, 2.07573082205217, 
1.17628969016684, 2.44680851063832, 2.84453345201007, 4.10010457610617, 
2.88208396840793, 1.58922558922557, 3.67559326527908, 3.90013106997858, 
1.36611181194425, 4.12505691303686, 2.02017257462689, 2.93167985827357, 
1.54068234183715, 2.12149379408387, 0.594313861969269, 3.83755588673622
)), row.names = c(NA, 30L), class = "data.frame")

Code:

library(tidyverse)
library(GGally)
library(Amelia)
library(inspectdf)
library(ggcorrplot)
library(ggplot2)
library(reshape2)
library(tseries)
library(dplyr)
library(caret)
library(tidyverse)
library(ARDL)
library(dLagM)
library(forecast)

in_sampleARDL <- data %>% 
  dplyr::filter(Date < '2020-03-01')

out_sampleARDL <-data %>% 
  dplyr::filter(Date >= '2020-03-01')

# Model Building
    
# Create the formulas

indep_vars <- expression(Industrialproduction, Householdconsumption, Investmentgrowth, ConsumerPriceIndex, Employment, Unemploymentrate, 
                         Stockmarketindex, Economicgrowth, Consumptiongrowth, Governmentexpenditure, Longtermgovernmentbondyield,
                         BankRate, ConsumerConfidenceIndex, RealPersonalDisposableIncome, PersonalDisposableIncome, SPPricechange, 
                         HouseStarts, HouseCompleted, TermSpread, BuildingPermits)

dep_vars   <- expression(NORTH, YORKSANDTHEHUMBER, NORTHWEST, EASTMIDS, WESTMIDS, EASTANGLIA, OUTERSEAST, OUTERMET, LONDON,
                         SOUTHWEST, WALES, SCOTLAND, NIRELAND, UK)

# Formulae with diff()

formulae <- unlist(lapply(dep_vars, \(x) lapply(indep_vars, \(y) bquote(.(x)~diff(.(y))))))
length(formulae)

# Without diff()

formulae2 <- unlist(lapply(dep_vars, \(x) lapply(indep_vars, \(y) bquote(.(x)~.(y)))))
length(formulae2)

result <- vector('list', length = length(formulae))
names(result) <- formulae2

# Loop for H = 4

for (i in seq_along(formulae)){
  
  # auto_ardl
  result[[i]][[1]] <- auto_ardl(formula(formulae2[[i]]),
                                data = in_sampleARDL, max_order = 4, selection = 'BIC')
  # prediction
  result[[i]][[2]] <- forecast(ardlDlm(formula = formula(formulae[[i]]), data = in_sampleARDL, p = 3),
                               x = out_sampleARDL |> select(sub("\\s~.*", "", formula(formulae[[i]]))) |> pull(), h = 4)
  # error
  result[[i]][[3]] <- mean((out_sampleARDL |> select(sub("\\s~.*", "", formula(formulae[[i]]))) |> pull() |> (\(x) x[1:4])() - result[[i]][[2]][["forecasts"]])^2)

  # set names
  names(result[[i]]) <- c('auto_ardl','forecast','error')

}
print(result[[i]])
1

There are 1 answers

2
IRTFM On BEST ANSWER

traceback() shows that this error is coming from the forecast call, and that it occurs with i==1, so look at the first parameter to forecast :: ardlDlm(formula = formula(formulae[[i]]), data = in_sampleARDL, p = 3) and realize that it is not something that forecast is designed to work with. forecast was expecting an atomic numeric vector.

Looking at the output of ardlDlm(formula = formula(formulae[[1]]), data = in_sampleARDL, p = 3), it appears that you really want numeric vectors contained in the $data leaf of that much longer list and in particular probably want only the i-th column, so try this:

for (i in seq_along(formulae)){
    
    # auto_ardl
    result[[i]][[1]] <- auto_ardl(formula(formulae2[[i]]),
                                  data = in_sampleARDL, 
                                  max_order = 4, selection = 'BIC')
    # prediction
    # 
    result[[i]][[2]] <- forecast(ardlDlm(formula = formula(formulae[[i]]), 
                    #---------------extract one col------------------\/-\/-\/-
                                         data = in_sampleARDL, p = 3)$data[[i]],
                        x = out_sampleARDL |> 
                              select(sub("\\s~.*", "", formula(formulae[[i]]))) |> 
                              pull(), h = 4)
    # error
    result[[i]][[3]] <- mean((out_sampleARDL |> select(sub("\\s~.*",
                             "", formula(formulae[[i]]))) |> 
                             pull() |> (\(x) x[1:4])() - 
                                  result[[i]][[2]][["forecasts"]])^2)
    
    # set names
    names(result[[i]]) <- c('auto_ardl','forecast','error')
    
}

Note that you only printed the last value in the much longer result object. The last such value looks like:

print(result[[i]])
$auto_ardl
$auto_ardl$best_model

Time series regression with "ts" data:
Start = 5, End = 30

Call:
dynlm::dynlm(formula = full_formula, data = data, start = start, 
    end = end)

Coefficients:
          (Intercept)               L(UK, 1)        BuildingPermits  L(BuildingPermits, 1)  L(BuildingPermits, 2)  
              1.59718               -0.04719                0.90441               -0.04269                0.19583  
L(BuildingPermits, 3)  L(BuildingPermits, 4)  
              0.63773                0.02544  


$auto_ardl$best_order
[1] 1 4

$auto_ardl$top_orders
   UK BuildingPermits       BIC
1   1               4  90.18992
2   2               4  93.11884
3   3               4  96.02905
4   1               3  98.36867
5   4               4  99.15721
6   3               3 100.20359
7   2               3 100.53056
8   2               2 104.78506
9   1               2 104.85999
10  1               1 106.10666


$forecast
  Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
1            NaN   NaN   NaN   NaN   NaN
2            NaN   NaN   NaN   NaN   NaN
3            NaN   NaN   NaN   NaN   NaN
4            NaN   NaN   NaN   NaN   NaN

$error
[1] NaN