R: merge.xts and specific fill function - r

After merging two unequal time series I want to fill the blanks with a custom function. Lets say my Series1 is daily data and Series2 is monthly data. So now Series1 has for example 30 data points for one month and Series2 only one. If I make a left join Series 2 has 29 NAs which I don't like. Ideally I would like a fill function so that Series 2 takes always the previous value to fill these 29 days.
So for example if the 31. of January has a value of 10 and the 28th of February a value of 15, February 1-27. should have a value of 10 as well. Of course in the beginning this doesn't work (since the first row is probably also a NA), so the first row should take the value of the first row containing a value at all.
At the moment I have this, but still, all NAs are present:
Test<-merge.xts(Series1, Series2, join="left", fill=function(x) x[index(x)-1,])
Series1:
structure(c(1.51762156049755, 1.52103159497526, 1.51401262063846,
1.5226927459172, 1.52933295052158, 1.52409353403389, 1.52292452830189,
1.5268928035982, 1.53555449785816, 1.54004946727549, 1.54031650339111,
1.53987556561086, 1.53733857383492, 1.52781969068276, 1.5303624813154,
1.53149347601615, 1.53200449185851, 1.53034081463009, 1.52689961175818,
1.52616010353115, 1.52004035586536, 1.52604263206673, 1.53170366207736,
1.53332707472775, 1.5400318381871, 1.53717071341521, 1.53998696583186,
1.53676880222841, 1.53316818056702, 1.53512014787431, 1.54153071688263,
1.53692449355433, 1.53382906453686, 1.53159514756473, 1.5344496294263,
1.53717866027826, 1.53445133065986, 1.53503822351656, 1.5306399132321,
1.53633694255827, 1.53748747380887, 1.54019086070839, 1.54068532372772,
1.53600669892073, 1.53977166385926, 1.53468288606184, 1.53986928104575,
1.54024911693623, 1.5402127262549, 1.54151119402985, 1.53934776549289,
1.53958085476343, 1.53900838497995, 1.53818540787939, 1.53465613216017,
1.53500719942405, 1.53537650054565, 1.53317195624888, 1.53192246131958,
1.53136958262882, 1.53666845974538, 1.53503754022167, 1.53098678960901,
1.52377172091382, 1.52796773627915, 1.52584842623527, 1.52760075397182,
1.52793296089385, 1.52820374854273, 1.52947558770344, 1.52752869440459,
1.52590880810595, 1.51771286513362, 1.52378827099884, 1.52171596056488,
1.52387303280875, 1.52663662867745, 1.53114232706069, 1.52827140549273,
1.52923132443161, 1.52939594909482, 1.53232585173925, 1.53195117573147,
1.53853103261361, 1.53776866137519, 1.54085533920156, 1.5410640956972,
1.54313041923661, 1.54222657292872, 1.54302034987504, 1.54211182336182,
1.54181785998761, 1.5424089337942, 1.53578353604795, 1.53286652078775,
1.53120629370629, 1.53219713608012, 1.53192052980132, 1.53522245762712,
1.53543098889476, 1.53283647523016, 1.5296408481177, 1.52531916716648,
1.52295699845811, 1.52777060191165, 1.52890571231934, 1.5233980665583,
1.52386256533288, 1.51978021978022, 1.52140011865412, 1.51797040169133,
1.51707941929974, 1.52089868588385, 1.52408100748809, 1.52491920394625,
1.52068065032432, 1.52637418914305, 1.52848101265823, 1.52656088306313,
1.52858618908214, 1.53068778514246, 1.52826643894108, 1.52470085470085,
1.51927185710623, 1.52041166380789, 1.51975945017182, 1.52318452637941,
1.51831155433287, 1.51966908661151, 1.52143645470753, 1.52183128444256,
1.52286417239331, 1.52149627623561, 1.52065908330545, 1.51957958976098,
1.52554186145346, 1.52094733242134, 1.51794915836482, 1.51173708920188,
1.51222222222222, 1.5101414692347, 1.5068328319725, 1.50393081761006,
1.50417972831766, 1.50391986062718, 1.50638741635526, 1.50589880276151,
1.51000264387063, 1.50961116475029, 1.50934456435904, 1.50983477576711,
1.51314636283961, 1.50903004140604, 1.51011752231157, 1.50968426638366,
1.50718251520226, 1.50750460809269, 1.50457827082233, 1.50718301061836,
1.51371392834807, 1.51775147928994, 1.51589595375723, 1.51878256100905,
1.51964269437608, 1.52107244513819, 1.51828822238478, 1.51868515287852,
1.52112289685443, 1.52031478770132, 1.5218941402322, 1.51964269437608,
1.51789300712069, 1.51745137247773, 1.51548186148772, 1.51610254538819,
1.51619929213177, 1.51333333333333, 1.51241134751773, 1.51200286635614,
1.51837734821672, 1.5163433908046, 1.49981738495252, 1.50498640072529,
1.5011387446479, 1.49350888500138, 1.4836323284631, 1.48080845540515,
1.47762023908813, 1.47091566935708, 1.44464775846295, 1.46478356566398,
1.46516563624619, 1.47632234837995, 1.48080808080808, 1.47685016405396,
1.48288833837967, 1.48791693466875, 1.48385916780979, 1.48779368575624,
1.4842056932966, 1.48020986745214, 1.48406538215688, 1.48219003370684,
1.4840747090138, 1.48181569592562, 1.47840712792072, 1.48482921511628,
1.48070841239722, 1.47882236069719, 1.47693552738063, 1.47952903398448,
1.47818343722173, 1.48081910042028, 1.47554444841128, 1.47437042328987,
1.47387958352196, 1.46947082767978, 1.47113912651959, 1.47202166064982,
1.47102365047843, 1.47226211849192, 1.47248814529838, 1.46853962839961,
1.46421559878636, 1.46491463305623, 1.46394424090787, 1.47141221037794,
1.46876654314452, 1.46473285134897, 1.46621860629643, 1.45898901098901,
1.45649677590319, 1.4541381128097, 1.45816872969889, 1.46286215978929,
1.46461267605634, 1.46386925795053, 1.46151797603196, 1.46911608093717,
1.47140552169236, 1.4750490108715, 1.47230138938368, 1.47392733410322,
1.47497537827917, 1.47591916674085, 1.48151776966242, 1.47590146376294,
1.47583108715184, 1.47547136091502, 1.47256621169665, 1.47307171853857,
1.47527795353882, 1.47582605564059, 1.46818468184682, 1.46878890272097,
1.48522318688065, 1.48453427065026, 1.48568912373404, 1.4814585908529,
1.48118303373771, 1.47687244262587, 1.47909624621953, 1.48514136031072,
1.48368539325843, 1.47950599606229, 1.47334107350183, 1.47758127902822,
1.47985739750446, 1.48092011412268, 1.47403176869534, 1.48108736475007,
1.47305653710247, 1.46450017661604, 1.4681413589495, 1.46912050964431,
1.46845174973489, 1.47360950944735, 1.46758608573436, 1.46957056292263,
1.47418043421849, 1.47130794416681, 1.47095489568003, 1.47372954349699,
1.47756961155036, 1.47673216132368, 1.47682004001044, 1.47401301518438,
1.47194032439934, 1.47180647406892, 1.47518534670737, 1.47624474053296,
1.47794826830338, 1.48057829646403, 1.48357504805172, 1.48148471615721,
1.47989206128134, 1.47923238696109, 1.47960337479342, 1.47915397336583,
1.47995097180879, 1.47630640813842, 1.47675825125281, 1.47637181928337,
1.47504781777082, 1.47135191275749, 1.47813993915689, 1.47672594142259,
1.47480059602069, 1.47183284845279, 1.46386701662292, 1.47050586381936,
1.46995971273428, 1.46776454099509, 1.46059482834701, 1.45992231638418,
1.46362994350282, 1.4642195358687, 1.46497830514478, 1.46292372881356,
1.46326046879115, 1.46075594141892, 1.4626918018413, 1.46522991013001,
1.46767729569611, 1.46556834030683, 1.46354350123283, 1.46293202005101,
1.46216192405955, 1.46279539664412, 1.46416652028807, 1.46635751159332,
1.46744206538021, 1.46897280168999, 1.46536662843025, 1.46557031043884,
1.46789797713281, 1.46835554770942, 1.4694150120203, 1.46747460345749,
1.46710702490404, 1.46860547847741, 1.46663705019991, 1.4664345652562,
1.46345186781609, 1.46563852813853, 1.46283081925752, 1.45655110310671,
1.45227952506118, 1.45321531791908, 1.4547789396441, 1.45564738292011,
1.45421278931479, 1.45517865219358, 1.45266890970265, 1.45443743716296,
1.45503465888362, 1.45726148569365, 1.45540762356374, 1.45618509746766,
1.45435302779312, 1.45287885766928, 1.45133394664213, 1.45226409852764,
1.45390070921986, 1.45874769797422, 1.45672988399926, 1.46178846689572,
1.4674146797569, 1.4640179910045, 1.46468609865471, 1.47339173024395,
1.47045561296383, 1.47032863849765, 1.47437233538607, 1.47061043494669,
1.47251605591235, 1.47473215132265, 1.47768657420511, 1.47433962264151,
1.47615894039735, 1.47619047619048, 1.47506661591169, 1.47083612680778,
1.47052580800772, 1.4673786407767, 1.46719083673073, 1.46737852664577,
1.4680161147686, 1.47064637280095, 1.46837200079318, 1.47009818506397,
1.46631153201144, 1.46435925090695, 1.46418085731063, 1.46629705281587,
1.47042504706232, 1.47244016287615, 1.46962801741195, 1.46572500987752,
1.47040745514028, 1.46655971122029, 1.46671388101983, 1.46569960713206,
1.46444107233182, 1.45887708649469, 1.45496722138174, 1.4528824285573,
1.45116001194862, 1.4471463022508, 1.44598993785144, 1.45799803729146,
1.45748550083554, 1.45195033727637, 1.44973909618982, 1.44844597927972,
1.45353852185846, 1.45797913446677, 1.45808966861598, 1.46286266924565,
1.45828482731859, 1.4618320610687, 1.46203029706866, 1.46219309400372,
1.46284480219888, 1.46597735105859, 1.46784424709671, 1.4689243417833,
1.46860898567785, 1.47238907188529, 1.47246010120669, 1.47172011661808,
1.46688286163522, 1.46971327918583, 1.47072714749582, 1.47229862475442,
1.47179285222014, 1.46633416458853, 1.46399523903987, 1.46048587010412,
1.45797329143755, 1.45885579937304, 1.45979140267083, 1.46490971205466,
1.46888496270122, 1.46831875607386, 1.46836546846236, 1.46927047823123,
1.46807470421433, 1.462829499457, 1.46497003046084, 1.46442900479499,
1.46273932253314, 1.46406951767233, 1.4673116388156, 1.46543100912033,
1.45540647198106, 1.46271003242606, 1.45876085240726, 1.45973718012054,
1.46011549378487, 1.46333792018872, 1.46617056692451, 1.46380829785127,
1.4638067061144, 1.46371087192653, 1.46229022704837, 1.46666666666667,
1.4661108386464, 1.46767617938264, 1.46891393044492, 1.47142439879272,
1.46808094632906, 1.46796059689847, 1.46733815763739, 1.46692037470726,
1.4646265866378, 1.46480534801416, 1.46492177506642, 1.4623687858982,
1.46242774566474, 1.46307385229541, 1.4626074785043, 1.4633068968979,
1.46385298869144, 1.46180344478217, 1.46254927726675, 1.46241896272285,
1.46647171523646, 1.46721558389397, 1.46642431586388, 1.46720484359233,
1.46822373696872, 1.46890958245719, 1.46962101463806, 1.47268740031898,
1.47340742210756, 1.47341746993938, 1.47524262230145, 1.47560369671072,
1.47479367604653, 1.47198963317384, 1.47108097327483, 1.47302572315084,
1.4712827696618, 1.47083753784057, 1.47290739991913, 1.47313237221494,
1.47367359289893, 1.47733523479678, 1.47741935483871, 1.47505622572071,
1.46778337272634, 1.46253469010176, 1.46209942481512, 1.46357003391224,
1.45595482546201, 1.45030384179627, 1.45351356929109, 1.45500778412039,
1.44706984490476, 1.45556604763404, 1.45198866617693), .indexTZ = "UTC", .indexCLASS = "Date", tclass = "Date", tzone = "UTC", class = c("xts",
"zoo"), index = structure(c(978307200, 978393600, 978480000,
978566400, 978652800, 978912000, 978998400, 979084800, 979171200,
979257600, 979516800, 979603200, 979689600, 979776000, 979862400,
980121600, 980208000, 980294400, 980380800, 980467200, 980726400,
980812800, 980899200, 980985600, 981072000, 981331200, 981417600,
981504000, 981590400, 981676800, 981936000, 982022400, 982108800,
982195200, 982281600, 982540800, 982627200, 982713600, 982800000,
982886400, 983145600, 983232000, 983318400, 983404800, 983491200,
983750400, 983836800, 983923200, 984009600, 984096000, 984355200,
984441600, 984528000, 984614400, 984700800, 984960000, 985046400,
985132800, 985219200, 985305600, 985564800, 985651200, 985737600,
985824000, 985910400, 986169600, 986256000, 986342400, 986428800,
986515200, 986774400, 986860800, 986947200, 987033600, 987120000,
987379200, 987465600, 987552000, 987638400, 987724800, 987984000,
988070400, 988156800, 988243200, 988329600, 988588800, 988675200,
988761600, 988848000, 988934400, 989193600, 989280000, 989366400,
989452800, 989539200, 989798400, 989884800, 989971200, 990057600,
990144000, 990403200, 990489600, 990576000, 990662400, 990748800,
991008000, 991094400, 991180800, 991267200, 991353600, 991612800,
991699200, 991785600, 991872000, 991958400, 992217600, 992304000,
992390400, 992476800, 992563200, 992822400, 992908800, 992995200,
993081600, 993168000, 993427200, 993513600, 993600000, 993686400,
993772800, 994032000, 994118400, 994204800, 994291200, 994377600,
994636800, 994723200, 994809600, 994896000, 994982400, 995241600,
995328000, 995414400, 995500800, 995587200, 995846400, 995932800,
996019200, 996105600, 996192000, 996451200, 996537600, 996624000,
996710400, 996796800, 997056000, 997142400, 997228800, 997315200,
997401600, 997660800, 997747200, 997833600, 997920000, 998006400,
998265600, 998352000, 998438400, 998524800, 998611200, 998870400,
998956800, 999043200, 999129600, 999216000, 999475200, 999561600,
999648000, 999734400, 999820800, 1000080000, 1000166400, 1000252800,
1000339200, 1000425600, 1000684800, 1000771200, 1000857600, 1000944000,
1001030400, 1001289600, 1001376000, 1001462400, 1001548800, 1001635200,
1001894400, 1001980800, 1002067200, 1002153600, 1002240000, 1002499200,
1002585600, 1002672000, 1002758400, 1002844800, 1003104000, 1003190400,
1003276800, 1003363200, 1003449600, 1003708800, 1003795200, 1003881600,
1003968000, 1004054400, 1004313600, 1004400000, 1004486400, 1004572800,
1004659200, 1004918400, 1005004800, 1005091200, 1005177600, 1005264000,
1005523200, 1005609600, 1005696000, 1005782400, 1005868800, 1006128000,
1006214400, 1006300800, 1006387200, 1006473600, 1006732800, 1006819200,
1006905600, 1006992000, 1007078400, 1007337600, 1007424000, 1007510400,
1007596800, 1007683200, 1007942400, 1008028800, 1008115200, 1008201600,
1008288000, 1008547200, 1008633600, 1008720000, 1008806400, 1008892800,
1009152000, 1009238400, 1009324800, 1009411200, 1009497600, 1009756800,
1009843200, 1009929600, 1010016000, 1010102400, 1010361600, 1010448000,
1010534400, 1010620800, 1010707200, 1010966400, 1011052800, 1011139200,
1011225600, 1011312000, 1011571200, 1011657600, 1011744000, 1011830400,
1011916800, 1012176000, 1012262400, 1012348800, 1012435200, 1012521600,
1012780800, 1012867200, 1012953600, 1013040000, 1013126400, 1013385600,
1013472000, 1013558400, 1013644800, 1013731200, 1013990400, 1014076800,
1014163200, 1014249600, 1014336000, 1014595200, 1014681600, 1014768000,
1014854400, 1014940800, 1015200000, 1015286400, 1015372800, 1015459200,
1015545600, 1015804800, 1015891200, 1015977600, 1016064000, 1016150400,
1016409600, 1016496000, 1016582400, 1016668800, 1016755200, 1017014400,
1017100800, 1017187200, 1017273600, 1017360000, 1017619200, 1017705600,
1017792000, 1017878400, 1017964800, 1018224000, 1018310400, 1018396800,
1018483200, 1018569600, 1018828800, 1018915200, 1019001600, 1019088000,
1019174400, 1019433600, 1019520000, 1019606400, 1019692800, 1019779200,
1020038400, 1020124800, 1020211200, 1020297600, 1020384000, 1020643200,
1020729600, 1020816000, 1020902400, 1020988800, 1021248000, 1021334400,
1021420800, 1021507200, 1021593600, 1021852800, 1021939200, 1022025600,
1022112000, 1022198400, 1022457600, 1022544000, 1022630400, 1022716800,
1022803200, 1023062400, 1023148800, 1023235200, 1023321600, 1023408000,
1023667200, 1023753600, 1023840000, 1023926400, 1024012800, 1024272000,
1024358400, 1024444800, 1024531200, 1024617600, 1024876800, 1024963200,
1025049600, 1025136000, 1025222400, 1025481600, 1025568000, 1025654400,
1025740800, 1025827200, 1026086400, 1026172800, 1026259200, 1026345600,
1026432000, 1026691200, 1026777600, 1026864000, 1026950400, 1027036800,
1027296000, 1027382400, 1027468800, 1027555200, 1027641600, 1027900800,
1027987200, 1028073600, 1028160000, 1028246400, 1028505600, 1028592000,
1028678400, 1028764800, 1028851200, 1029110400, 1029196800, 1029283200,
1029369600, 1029456000, 1029715200, 1029801600, 1029888000, 1029974400,
1030060800, 1030320000, 1030406400, 1030492800, 1030579200, 1030665600,
1030924800, 1031011200, 1031097600, 1031184000, 1031270400, 1031529600,
1031616000, 1031702400, 1031788800, 1031875200, 1032134400, 1032220800,
1032307200, 1032393600, 1032480000, 1032739200, 1032825600, 1032912000,
1032998400, 1033084800, 1033344000, 1033430400, 1033516800, 1033603200,
1033689600, 1033948800, 1034035200, 1034121600, 1034208000, 1034294400,
1034553600, 1034640000, 1034726400, 1034812800, 1034899200, 1035158400,
1035244800, 1035331200, 1035417600, 1035504000, 1035763200, 1035849600,
1035936000, 1036022400, 1036108800, 1036368000, 1036454400, 1036540800,
1036627200, 1036713600, 1036972800, 1037059200, 1037145600, 1037232000,
1037318400, 1037577600, 1037664000, 1037750400, 1037836800, 1037923200,
1038182400, 1038268800, 1038355200, 1038441600, 1038528000, 1038787200,
1038873600, 1038960000, 1039046400, 1039132800, 1039392000, 1039478400,
1039564800, 1039651200, 1039737600, 1039996800, 1040083200, 1040169600,
1040256000, 1040342400, 1040601600, 1040688000, 1040774400, 1040860800,
1040947200, 1041206400, 1041292800), tzone = "UTC", tclass = "Date"), .Dim = c(522L,
1L), .Dimnames = list(NULL, "Series1"))
Series2:
structure(c(100, 100.32, 100.57, 100.82, 100.98, 101.01, 101.16,
101.3, 101.75, 102.07, 102.12, 102.3, 102.44, 102.59, 102.62,
102.74, 102.84, 103.09, 103.25, 103.31, 103.35, 103.48, 103.6,
103.72, 103.84, 103.96, 104.1, 104.35, 104.52, 104.69, 104.82,
104.96, 104.9, 105.03, 105.08, 105.27, 105.46, 105.55, 105.78,
105.94, 106.11, 106.36, 106.52, 106.6, 106.7, 106.92, 107.1,
107.27, 107.39, 107.41, 107.54, 107.72, 107.96, 108.13, 108.3,
108.43, 108.56, 108.68, 108.77), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", class = c("xts",
"zoo"), index = structure(c(1010102400, 1010707200, 1011312000,
1011916800, 1012435200, 1012521600, 1013126400, 1013731200, 1014336000,
1014854400, 1014940800, 1015545600, 1016150400, 1016755200, 1017360000,
1017964800, 1018569600, 1019174400, 1019779200, 1020124800, 1020384000,
1020988800, 1021593600, 1022198400, 1022803200, 1023408000, 1024012800,
1024617600, 1025222400, 1025827200, 1026432000, 1027036800, 1027641600,
1028073600, 1028246400, 1028851200, 1029456000, 1030060800, 1030665600,
1031270400, 1031875200, 1032480000, 1033084800, 1033344000, 1033689600,
1034294400, 1034899200, 1035504000, 1036022400, 1036108800, 1036713600,
1037318400, 1037923200, 1038528000, 1039132800, 1039737600, 1040342400,
1040947200, 1041292800), tzone = "UTC", tclass = "Date"), .Dim = c(59L,
1L), .Dimnames = list(NULL, "Series2"))

na.locf() will do the job:
Test<-merge.xts(Series1, Series2, join="left", fill=na.locf())
This function "Last Observation Carried Forward" fills the NA with the last knowne value.
Hope this helps people landing here, years after you asked the question.

Related

Arima model with Rolling Origin in R

I am working on a data set with 14 variables. I have used the Arima model with Rolling Origin but applying the rolling origin method on each variable every time is a bit slow and want to automate the process. I have tried to automate the process so that it gives me outputs for 14 models but it gives me an error. Please any help is appreciated.
Data:
structure(list(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, 525916800, 533779200, 541555200,
549504000, 557452800, 565315200, 573177600, 581126400, 589075200,
596937600, 604713600, 612662400, 620611200, 628473600, 636249600,
644198400, 652147200, 660009600, 667785600, 675734400), tzone = "UTC", class = c("POSIXct",
"POSIXt")), 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, -3.4743890895067,
4.63823913990992, 0.857370960463727, 1.78620594713658, 0.527472527472524,
-4.05973562947765, -0.136726966764838, 3.16657890117607, 5.95161125667812,
8.01002055498458, 10.5501040737437, 13.4138468987035, 2.93371279497212,
8.84291046495554, -6.87764606265876, 2.90741287990725, 3.71548486856639,
1.23317430567388, -1.1153443739474, 4.31313207880924), 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, 1.01487759630426, 1.54843356139717, 4.15602836879435,
4.43619773934357, -0.309698451507728, -1.45519947678222, -1.09839057574248,
9.08267346664877, 11.8913598474363, 13.9511229623114, 9.71243848306475,
7.66524473371739, 6.46801731884651, -2.26736490763654, -4.35729847494552,
-2.93870179974964, -7.72353426221536, -7.01127302722023, 2.02543627323513,
2.51245245873873), 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,
1.69729803867784, 0.792339593378065, 2.82330902522246, 2.20899212700891,
1.48327338701976, -1.78151365931687, 1.8457608174996, 5.06380710500736,
7.57132625044768, 9.28561520321818, 9.51969943135663, 11.3671132539057,
10.5960954085668, -1.43026516363364, 3.55308627832826, 3.99351008518014,
-1.44138713566414, -0.165494414563527, 2.01304344107922, 1.70645628251555
), 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, 1.76616504051596, 3.78627839708797,
3.9017974572556, 3.85473176612416, 0.0696479874633737, 1.45578980947134,
2.96698585107904, 12.8612275490659, 16.8142463597009, 10.6860102754148,
5.80782620275077, 2.65911542610573, -2.54295171544163, 4.66512121048756,
-3.66911045104132, -1.75382312052187, -3.61743042705271, -5.070772474025,
-1.21063610003222, 1.9530155970429), 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,
1.7360199527435, 3.3224734685978, 4.23339889482064, 5.79267379518974,
4.39964893406187, 0.374237288135615, 4.31199848701807, 13.9164443523531,
18.0050929925879, 6.07502745611839, 3.93976822755839, 4.07004176642259,
3.48434981192908, -1.92610381813166, 0.438451356717408, -0.103780578206083,
-3.0952145377791, -1.72381519612015, -2.02143896779759, 4.40768347678723
), 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, 2.80551592962435,
5.64292321924145, 4.17550004608719, 9.7903026013095, 5.88709352460008,
3.07862089961185, 8.83080444493668, 14.1609281183215, 14.9330678829839,
-2.38242974223737, 1.8287757399192, 1.22633166874738, -5.71564382892894,
-5.25820956533587, -9.72515856236787, 0.957479010339489, -3.50481300299826,
-3.45549395738277, -0.828308094308001, -0.331408094033985), 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, 4.50826657576274, 4.92742395824838, 5.38770910645244,
7.13653626341212, 6.15524925576032, 1.08283352245096, 6.66955322492704,
9.69075574665124, 11.4606033194907, 3.4233015677836, 1.10095233565968,
1.65461280649144, -3.58737650679069, -5.85546129756061, -4.98846560711691,
-2.32068359558401, -5.55914140928629, -4.66925504224286, -1.07093896112692,
2.07357059157311), 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,
6.34025700005186, 6.1931790459772, 8.10781836281492, 6.14401677315165,
5.88313802952244, 0.112183931227468, 4.21036727396348, 5.85740693754756,
8.61496319123439, 2.24246818616477, 2.39678510128783, 1.57885756155336,
-2.68472955079939, -5.09925369345585, -6.23990242127901, -2.51851513733724,
-2.72874133732908, -5.45172276846427, 0.20833593462305, 2.61721355963614
), 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, 7.42869193863026,
6.70069362648866, 6.67699006500675, 7.43184006668679, 5.53177257525084,
-1.06737656081638, 1.7605678920595, 5.86902048679756, 6.75919979067056,
0.943616938313976, 1.29679498499027, 1.95787891003782, -1.64030775806797,
-2.62806236080178, -2.6208912592328, -4.49717565910836, -5.18403877531433,
-5.57502752084625, -0.947552316580683, 0.978175016770521), 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, 4.40418977202712, 2.87285774987016, 3.86424654076504,
5.69560126372535, 5.04170063334797, 1.07854257457266, 6.75066443547593,
13.56963706108, 16.2190250397843, 2.62121000419169, -0.940827274460141,
2.85066318466084, -0.886020125887025, -6.46387832699618, -3.51150320013839,
-0.306262698697259, 0.555963495227118, -7.19650681052728, -1.76899526612503,
0.528003461834023), 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,
1.14433241461116, 3.01472870890965, 0.7768290641219, 3.81433365451707,
-0.140822531605095, -2.99349379827568, 4.11669475005782, 4.95668454288706,
12.973544973545, 15.3990258523792, 9.25324675324674, 6.63977924007642,
0.236872486962066, -0.381277677383487, 0.681750224259938, -2.67091690260756,
-5.39078074779283, -3.51337404317537, 0.996191624080064, 2.8524564276044
), 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, 0.802862378916138,
0.473263498109834, 1.33268231036562, 0.742609336470062, 0.427651014264418,
-2.00028015128168, -2.46419484863213, 3.18590814502184, 4.33732886439812,
3.78406337625565, 4.59302783096821, 9.65541455585091, 7.16082700576343,
2.74890619997868, -6.81926759861247, 3.2880071333036, 2.69558648969462,
-2.78454942837929, 1.79123210602768, 2.88825864878425), 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, 1.07655502392344, -1.70701093778018, -2.34959319931409,
6.56454324677751, -1.80912979454455, -4.90966221523961, 0.319176899102556,
1.67315466387184, -2.88259765121672, 2.95678544351781, -0.54123711340205,
4.15355569540591, -1.90510040874357, 0.923946519801462, 4.1035398865513,
-2.3519674449081, -5.50238389546177, 7.24670179766041, 2.75090864790844,
0.446509889559553), 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,
3.33948434056075, 3.50933756603259, 3.25378570059421, 5.14920870654849,
3.36548010504709, -0.177206541696886, 1.65971553844507, 8.51865098567251,
11.0759984490113, 5.32351247098249, 3.99880682100659, 4.55095927082668,
0.864171188197283, -2.04898834977862, -3.10383660120637, -1.01415357182659,
-2.94496091613858, -4.06343734981687, -0.677156948752485, 1.59717017296902
)), row.names = c(NA, -50L), class = c("tbl_df", "tbl", "data.frame"
))
This manual code works:
y <- data2$NORTH
ourCall <- "predict(arima(x=data,order=c(1,0,0)),n.ahead=h)"
ourValue <- c("pred")
returnedValues1 <- ro(y, h=4, origins = 8,
call=ourCall, value=ourValue)
returnedValues1$actuals
returnedValues1$holdout
returnedValues1$pred
But this doesn't:
y <- data2
y %<>% dplyr::select(-Date)
ourCall <- "predict(arima(x=data,order=c(1,0,0)),n.ahead=h)"
ourValue <- c("pred")
ar_ro_model_4 = apply(y,2,function(x){
return(
list(
returnedValues1 <- ro(y, h=4, origins = 8,
call=ourCall, value=ourValue)
))
} )
ar_ro_model_4

Spatial weight matrix and spatial lags in panel data

I have a panel dataset C_sf1 of 922 cities and 155 variables that looks like this:
structure(list(FID = c("AT001C1", "AT002C1"), E_pc_2000 = c(10.2,
10.2), E_pc_2001 = c(10.7, 10.7), E_pc_2002 = c(10.8, 10.8),
E_pc_2003 = c(11.5, 11.5), E_pc_2004 = c(11.4, 11.4), E_pc_2005 = c(11.5,
11.5), geometry = structure(list(structure(list(list(structure(c(16.4197,
16.4176, 16.4156, 16.4065, 16.4038, 16.4018, 16.4015, 16.4011,
16.398, 16.3951, 16.3941, 16.394, 16.3946, 16.3948, 16.3944,
16.3921, 16.3917, 16.3914, 16.3907, 16.3889, 16.3887, 16.3894,
16.3894, 16.3889, 16.3885, 16.3866, 16.3858, 16.3834, 16.3826,
16.3802, 16.3797, 16.3797, 16.3799, 16.3815, 16.3826, 16.3838,
16.3833, 16.3846, 16.3838, 16.3853, 16.3855, 16.3801, 16.3793,
16.3784, 16.378, 16.3773, 16.3763, 16.3755, 16.3751, 16.3752,
16.3783, 16.3782, 16.3775, 16.3763, 16.3723, 16.3694, 16.3691,
16.369, 16.3692, 16.3696, 16.3692, 16.3685, 16.3657, 16.3631,
16.3619, 16.3603, 16.3596, 16.3587, 16.3585, 16.3575, 16.3553,
16.3545, 16.3533, 16.3529, 16.3495, 16.3475, 16.3465, 16.3454,
16.3446, 16.3445, 16.3444, 16.3446, 16.3458, 16.348, 16.346,
16.3445, 16.3437, 16.3408, 16.3348, 16.3306, 16.329, 16.3236,
16.3212, 16.3193, 16.3179, 16.316, 16.3136, 16.3099, 16.3074,
16.3056, 16.3043, 16.3031, 16.3015, 16.2998, 16.2962, 16.2918,
16.2888, 16.2881, 16.2866, 16.2865, 16.2869, 16.2884, 16.2898,
16.2909, 16.2909, 16.291, 16.2902, 16.2856, 16.282, 16.2785,
16.2718, 16.2688, 16.2657, 16.2644, 16.2639, 16.2638, 16.2645,
16.2652, 16.268, 16.269, 16.2695, 16.2694, 16.2689, 16.2664,
16.2655, 16.2654, 16.2654, 16.265, 16.264, 16.2635, 16.2595,
16.2582, 16.2578, 16.2569, 16.2564, 16.2518, 16.2488, 16.2474,
16.2454, 16.2439, 16.2427, 16.2413, 16.2387, 16.2374, 16.2373,
16.2376, 16.2387, 16.2397, 16.2402, 16.2398, 16.2367, 16.2335,
16.2301, 16.2264, 16.2242, 16.2221, 16.2198, 16.2166, 16.2149,
16.2137, 16.2104, 16.2085, 16.2067, 16.2057, 16.2049, 16.2046,
16.2051, 16.2051, 16.2047, 16.2009, 16.1996, 16.1978, 16.1961,
16.1959, 16.196, 16.1957, 16.1951, 16.1951, 16.1958, 16.1969,
16.1982, 16.1984, 16.1978, 16.1967, 16.1955, 16.1938, 16.1901,
16.1848, 16.184, 16.184, 16.1839, 16.1841, 16.1844, 16.1847,
16.1854, 16.1856, 16.1869, 16.1886, 16.1932, 16.1951, 16.1965,
16.1975, 16.1978, 16.1965, 16.1961, 16.1962, 16.1968, 16.1971,
16.1977, 16.1994, 16.2008, 16.2035, 16.2057, 16.2067, 16.2077,
16.2085, 16.2073, 16.2065, 16.2063, 16.2054, 16.2042, 16.2027,
16.2037, 16.2086, 16.2088, 16.207, 16.2055, 16.2048, 16.2048,
16.2047, 16.2045, 16.204, 16.2035, 16.2016, 16.1997, 16.1986,
16.1975, 16.1961, 16.1947, 16.193, 16.1913, 16.1906, 16.19,
16.1894, 16.1885, 16.1875, 16.1867, 16.1858, 16.1851, 16.1844,
16.1842, 16.1845, 16.1848, 16.1856, 16.1864, 16.1888, 16.1911,
16.1936, 16.1946, 16.1956, 16.1961, 16.1967, 16.1969, 16.1971,
16.197, 16.1969, 16.1971, 16.198, 16.1988, 16.1992, 16.1996,
16.2006, 16.2016, 16.2032, 16.2048, 16.2105, 16.2114, 16.2181,
16.2238, 16.2239, 16.2226, 16.2197, 16.2193, 16.2179, 16.2154,
16.2137, 16.211, 16.2104, 16.2104, 16.2106, 16.2113, 16.2125,
16.2134, 16.216, 16.2184, 16.2194, 16.2199, 16.2205, 16.2208,
16.2215, 16.2241, 16.2314, 16.2348, 16.2369, 16.2376, 16.2364,
16.2385, 16.2383, 16.2312, 16.2265, 16.2186, 16.2169, 16.2154,
16.2151, 16.216, 16.2193, 16.2209, 16.2235, 16.2245, 16.2291,
16.2307, 16.2328, 16.2348, 16.2377, 16.2418, 16.2456, 16.2484,
16.2496, 16.25, 16.2512, 16.2522, 16.2527, 16.2534, 16.2559,
16.2569, 16.2594, 16.2599, 16.2668, 16.2717, 16.2729, 16.2818,
16.2834, 16.2846, 16.2915, 16.2975, 16.2985, 16.2985, 16.2986,
16.2987, 16.299, 16.2991, 16.2992, 16.2992, 16.2989, 16.3003,
16.3008, 16.3061, 16.3073, 16.3103, 16.3121, 16.3125, 16.3127,
16.314, 16.3146, 16.3178, 16.3183, 16.3187, 16.3197, 16.3206,
16.3222, 16.325, 16.3266, 16.3319, 16.3324, 16.3348, 16.3362,
16.3387, 16.3425, 16.3429, 16.3443, 16.3453, 16.3463, 16.349,
16.351, 16.3524, 16.3527, 16.3546, 16.3548, 16.3564, 16.3585,
16.3587, 16.3605, 16.3649, 16.3688, 16.3731, 16.3814, 16.3816,
16.3818, 16.3848, 16.3857, 16.3873, 16.3936, 16.3945, 16.3971,
16.398, 16.3994, 16.403, 16.405, 16.4081, 16.4088, 16.4099,
16.4109, 16.4115, 16.4119, 16.4119, 16.4121, 16.4123, 16.4139,
16.4186, 16.4192, 16.4208, 16.421, 16.4215, 16.4224, 16.4232,
16.424, 16.4251, 16.4268, 16.4272, 16.4296, 16.4333, 16.4351,
16.436, 16.4366, 16.4366, 16.4362, 16.4359, 16.4352, 16.4351,
16.4348, 16.4346, 16.4341, 16.4337, 16.4332, 16.4325, 16.4321,
16.4335, 16.4347, 16.437, 16.4437, 16.4457, 16.4492, 16.4517,
16.4536, 16.4551, 16.4645, 16.4658, 16.467, 16.4681, 16.4715,
16.4724, 16.4735, 16.4751, 16.4762, 16.477, 16.4774, 16.4776,
16.4795, 16.4811, 16.4845, 16.4884, 16.4926, 16.4943, 16.4976,
16.4992, 16.5036, 16.5087, 16.5105, 16.5114, 16.5118, 16.5142,
16.5196, 16.5284, 16.5309, 16.5331, 16.5378, 16.5401, 16.5404,
16.5426, 16.5473, 16.5506, 16.5529, 16.5623, 16.5684, 16.575,
16.576, 16.5762, 16.5767, 16.5769, 16.5771, 16.5775, 16.578,
16.578, 16.5782, 16.578, 16.578, 16.5772, 16.5767, 16.5757,
16.5747, 16.5742, 16.5743, 16.5743, 16.5744, 16.5742, 16.5737,
16.5733, 16.5725, 16.5714, 16.5706, 16.5695, 16.5689, 16.5674,
16.5666, 16.5655, 16.5647, 16.5643, 16.5636, 16.563, 16.5628,
16.5623, 16.5616, 16.5609, 16.5605, 16.5602, 16.5597, 16.559,
16.5581, 16.5575, 16.5567, 16.5561, 16.5553, 16.5548, 16.5544,
16.5543, 16.5545, 16.5549, 16.5551, 16.5553, 16.5552, 16.5548,
16.5542, 16.5535, 16.5529, 16.5524, 16.552, 16.5514, 16.5507,
16.5501, 16.5499, 16.5497, 16.5495, 16.5485, 16.5482, 16.5478,
16.5468, 16.5466, 16.5466, 16.5471, 16.5477, 16.5484, 16.5492,
16.5494, 16.5498, 16.5499, 16.5501, 16.5501, 16.55, 16.5498,
16.5497, 16.5495, 16.5492, 16.5486, 16.5478, 16.547, 16.5463,
16.5452, 16.5442, 16.543, 16.5424, 16.5416, 16.5411, 16.541,
16.5412, 16.5413, 16.5417, 16.5423, 16.5432, 16.5437, 16.5441,
16.5444, 16.5445, 16.5444, 16.5441, 16.5434, 16.5429, 16.5422,
16.5419, 16.5412, 16.5404, 16.5393, 16.5384, 16.5371, 16.5365,
16.5364, 16.5364, 16.5364, 16.5369, 16.5373, 16.538, 16.5387,
16.5392, 16.5396, 16.5398, 16.5396, 16.538, 16.5365, 16.5365,
16.5365, 16.5365, 16.5367, 16.5366, 16.5365, 16.5365, 16.5365,
16.5366, 16.5367, 16.5368, 16.5368, 16.5369, 16.537, 16.5371,
16.5372, 16.5373, 16.5373, 16.5374, 16.5376, 16.5377, 16.5378,
16.538, 16.5381, 16.5382, 16.5383, 16.5384, 16.5385, 16.5385,
16.5385, 16.5385, 16.5386, 16.5386, 16.5389, 16.5393, 16.5399,
16.54, 16.54, 16.5401, 16.5401, 16.5401, 16.5399, 16.5396,
16.5391, 16.5386, 16.5386, 16.5385, 16.5384, 16.5383, 16.5382,
16.538, 16.5378, 16.5375, 16.5373, 16.5371, 16.5369, 16.5367,
16.5364, 16.5362, 16.5363, 16.5364, 16.5365, 16.5375, 16.5377,
16.5385, 16.54, 16.5406, 16.5413, 16.5423, 16.543, 16.5439,
16.545, 16.5456, 16.5461, 16.5467, 16.5469, 16.5465, 16.5467,
16.548, 16.55, 16.5518, 16.5521, 16.552, 16.5502, 16.5488,
16.5484, 16.5482, 16.5479, 16.5474, 16.5463, 16.5445, 16.5423,
16.5417, 16.5414, 16.5424, 16.5419, 16.5416, 16.542, 16.5421,
16.5468, 16.5417, 16.5379, 16.5329, 16.5326, 16.5263, 16.5213,
16.5192, 16.5128, 16.514, 16.5087, 16.5074, 16.5079, 16.5083,
16.5088, 16.5095, 16.5098, 16.5116, 16.5129, 16.5137, 16.5145,
16.5142, 16.5125, 16.5128, 16.5094, 16.5041, 16.5021, 16.4983,
16.4951, 16.494, 16.4898, 16.484, 16.4813, 16.4809, 16.4809,
16.4809, 16.4819, 16.4824, 16.4833, 16.4835, 16.4835, 16.4834,
16.4836, 16.4829, 16.4813, 16.4809, 16.4816, 16.4772, 16.4748,
16.472, 16.4715, 16.4647, 16.4606, 16.4596, 16.452, 16.4483,
16.4402, 16.4401, 16.4401, 16.4402, 16.438, 16.4379, 16.438,
16.4363, 16.4339, 16.4336, 16.4299, 16.4271, 16.4247, 16.4226,
16.4197, 48.3222, 48.3221, 48.3218, 48.3196, 48.3191, 48.3185,
48.3173, 48.3171, 48.3177, 48.3184, 48.3188, 48.3191, 48.3201,
48.3215, 48.3217, 48.3219, 48.3216, 48.321, 48.3206, 48.3205,
48.3202, 48.3195, 48.3191, 48.3186, 48.3184, 48.3188, 48.3187,
48.3174, 48.3169, 48.3154, 48.3147, 48.3137, 48.3128, 48.31,
48.3085, 48.306, 48.3053, 48.3039, 48.3029, 48.302, 48.301,
48.3018, 48.3011, 48.2995, 48.2986, 48.2976, 48.2963, 48.2958,
48.2949, 48.2944, 48.2901, 48.2892, 48.2886, 48.2879, 48.2878,
48.2874, 48.2867, 48.2859, 48.285, 48.2839, 48.2835, 48.2833,
48.284, 48.2846, 48.2846, 48.2842, 48.2842, 48.2842, 48.2842,
48.2843, 48.2843, 48.2844, 48.2848, 48.2852, 48.2894, 48.2896,
48.2897, 48.2895, 48.2887, 48.2883, 48.288, 48.2878, 48.2867,
48.2845, 48.2838, 48.283, 48.2828, 48.2816, 48.2803, 48.2791,
48.2788, 48.2791, 48.2792, 48.279, 48.2785, 48.2776, 48.2754,
48.2737, 48.2728, 48.2725, 48.2725, 48.2726, 48.2732, 48.2734,
48.2728, 48.2717, 48.2706, 48.2701, 48.2679, 48.2668, 48.2663,
48.2658, 48.2654, 48.2646, 48.2641, 48.2634, 48.2628, 48.2607,
48.2595, 48.2586, 48.2564, 48.2559, 48.2556, 48.2552, 48.2548,
48.2544, 48.254, 48.2539, 48.2535, 48.2532, 48.2525, 48.252,
48.2514, 48.25, 48.2491, 48.2485, 48.2479, 48.2475, 48.2471,
48.2467, 48.2432, 48.2418, 48.2407, 48.2397, 48.2395, 48.2401,
48.24, 48.2398, 48.239, 48.2389, 48.2392, 48.2398, 48.2405,
48.2411, 48.242, 48.243, 48.2446, 48.2461, 48.2471, 48.2476,
48.2486, 48.2498, 48.2514, 48.2536, 48.255, 48.2567, 48.2578,
48.2585, 48.2587, 48.2593, 48.2619, 48.2626, 48.2626, 48.2623,
48.2613, 48.2593, 48.2563, 48.2556, 48.255, 48.2533, 48.252,
48.2488, 48.2451, 48.2436, 48.2418, 48.2378, 48.2357, 48.2347,
48.2339, 48.2332, 48.2327, 48.2324, 48.2316, 48.2307, 48.2291,
48.228, 48.2258, 48.2243, 48.2238, 48.2237, 48.2234, 48.2233,
48.2227, 48.2223, 48.222, 48.222, 48.222, 48.2224, 48.2226,
48.2225, 48.2222, 48.2218, 48.2214, 48.2203, 48.2196, 48.2185,
48.2165, 48.2161, 48.2151, 48.2137, 48.2131, 48.2125, 48.2117,
48.211, 48.2095, 48.2087, 48.208, 48.2073, 48.2072, 48.2067,
48.2062, 48.2061, 48.2045, 48.2031, 48.203, 48.1993, 48.1968,
48.1942, 48.1934, 48.1924, 48.1914, 48.1905, 48.1897, 48.1879,
48.1861, 48.1852, 48.1844, 48.183, 48.1816, 48.1802, 48.1789,
48.1783, 48.1777, 48.1762, 48.1746, 48.1731, 48.1727, 48.1724,
48.172, 48.1716, 48.1712, 48.1709, 48.1705, 48.1701, 48.1697,
48.1686, 48.1676, 48.1664, 48.1656, 48.1648, 48.164, 48.1632,
48.1621, 48.1609, 48.1601, 48.1593, 48.1581, 48.1569, 48.1556,
48.1556, 48.1555, 48.1558, 48.156, 48.1561, 48.1562, 48.1556,
48.1554, 48.1547, 48.1536, 48.1524, 48.1529, 48.1534, 48.1526,
48.1521, 48.1515, 48.151, 48.1501, 48.1494, 48.1485, 48.1479,
48.1472, 48.1463, 48.1453, 48.1417, 48.1406, 48.14, 48.1392,
48.1375, 48.1364, 48.1352, 48.1352, 48.1361, 48.1364, 48.1364,
48.1358, 48.1347, 48.1343, 48.1325, 48.1315, 48.1297, 48.1282,
48.1273, 48.1265, 48.1258, 48.1253, 48.1243, 48.1238, 48.1245,
48.1247, 48.1269, 48.1274, 48.1283, 48.129, 48.1297, 48.1301,
48.1308, 48.131, 48.1308, 48.1308, 48.1306, 48.1304, 48.1303,
48.1302, 48.1298, 48.1298, 48.1302, 48.1304, 48.1335, 48.1333,
48.1332, 48.1308, 48.1307, 48.1305, 48.1296, 48.1286, 48.1287,
48.1283, 48.128, 48.1279, 48.1275, 48.1273, 48.1269, 48.1266,
48.1262, 48.1251, 48.1245, 48.1217, 48.121, 48.1197, 48.1197,
48.1217, 48.1231, 48.1256, 48.1269, 48.1261, 48.1269, 48.1276,
48.1292, 48.1307, 48.1333, 48.138, 48.1376, 48.1368, 48.1367,
48.1362, 48.1359, 48.1353, 48.1345, 48.1344, 48.134, 48.1337,
48.1336, 48.1332, 48.1328, 48.1322, 48.1321, 48.1309, 48.1309,
48.1304, 48.1296, 48.1296, 48.1293, 48.1287, 48.1278, 48.1272,
48.1265, 48.1264, 48.1264, 48.126, 48.1259, 48.1257, 48.125,
48.1249, 48.1247, 48.1244, 48.1235, 48.122, 48.1209, 48.1189,
48.1187, 48.1186, 48.1187, 48.1189, 48.1194, 48.1202, 48.1204,
48.1206, 48.1207, 48.1209, 48.1209, 48.1216, 48.1217, 48.122,
48.1226, 48.1229, 48.1229, 48.1225, 48.122, 48.1219, 48.1212,
48.1204, 48.12, 48.1199, 48.1202, 48.1204, 48.1213, 48.1219,
48.123, 48.1232, 48.1237, 48.124, 48.1256, 48.1278, 48.1303,
48.131, 48.1314, 48.1379, 48.138, 48.1381, 48.1384, 48.1384,
48.1387, 48.1389, 48.1393, 48.14, 48.1462, 48.147, 48.1478,
48.1484, 48.1507, 48.1514, 48.1522, 48.1533, 48.1541, 48.1548,
48.1556, 48.1562, 48.157, 48.1568, 48.1561, 48.1554, 48.1586,
48.1581, 48.1572, 48.1568, 48.157, 48.1577, 48.1581, 48.1583,
48.1586, 48.1591, 48.1572, 48.1538, 48.1525, 48.151, 48.147,
48.1452, 48.145, 48.1438, 48.1422, 48.1413, 48.1407, 48.139,
48.1377, 48.1361, 48.1363, 48.1369, 48.1386, 48.1393, 48.1402,
48.1418, 48.1428, 48.1433, 48.144, 48.146, 48.1476, 48.1506,
48.152, 48.1538, 48.1554, 48.1563, 48.1573, 48.1582, 48.159,
48.1597, 48.1604, 48.161, 48.1616, 48.1623, 48.1627, 48.163,
48.1631, 48.1631, 48.1631, 48.1629, 48.1628, 48.1625, 48.1621,
48.1616, 48.1612, 48.161, 48.1609, 48.1609, 48.1613, 48.1616,
48.1622, 48.1629, 48.1638, 48.1641, 48.1643, 48.1644, 48.1644,
48.1642, 48.1639, 48.1636, 48.1633, 48.163, 48.1627, 48.1624,
48.1622, 48.162, 48.1621, 48.1621, 48.1623, 48.1624, 48.1626,
48.163, 48.1635, 48.1642, 48.165, 48.166, 48.1665, 48.1675,
48.1681, 48.1689, 48.1704, 48.1709, 48.1713, 48.1714, 48.1716,
48.1718, 48.172, 48.1723, 48.1727, 48.1732, 48.1737, 48.1742,
48.1749, 48.1756, 48.1761, 48.1767, 48.177, 48.1773, 48.1775,
48.1775, 48.1775, 48.1773, 48.1771, 48.1771, 48.1771, 48.1771,
48.1768, 48.1765, 48.1762, 48.176, 48.1758, 48.1756, 48.1752,
48.1748, 48.1745, 48.1739, 48.1735, 48.1733, 48.1731, 48.1732,
48.1733, 48.1735, 48.1734, 48.1732, 48.173, 48.1727, 48.1727,
48.173, 48.1732, 48.1734, 48.1736, 48.1739, 48.174, 48.174,
48.174, 48.174, 48.1738, 48.1738, 48.1741, 48.1745, 48.1757,
48.1771, 48.1774, 48.1778, 48.179, 48.1803, 48.1814, 48.182,
48.1826, 48.1828, 48.1831, 48.1833, 48.1835, 48.1837, 48.1839,
48.1842, 48.1846, 48.1851, 48.1855, 48.1856, 48.186, 48.1864,
48.1867, 48.1869, 48.1872, 48.1874, 48.1876, 48.1879, 48.188,
48.1882, 48.1884, 48.1887, 48.1892, 48.1895, 48.1897, 48.191,
48.1915, 48.1923, 48.1924, 48.1927, 48.193, 48.1935, 48.1938,
48.1942, 48.1947, 48.1954, 48.1961, 48.1963, 48.1965, 48.1967,
48.1972, 48.1976, 48.1981, 48.1984, 48.1988, 48.1991, 48.1993,
48.1995, 48.1997, 48.2001, 48.2003, 48.201, 48.2014, 48.202,
48.2039, 48.2043, 48.2055, 48.2075, 48.2082, 48.2089, 48.2099,
48.2109, 48.2122, 48.2137, 48.2148, 48.2161, 48.2184, 48.2196,
48.2215, 48.2233, 48.2283, 48.2333, 48.2367, 48.2377, 48.2388,
48.2395, 48.2401, 48.2404, 48.2416, 48.2423, 48.2426, 48.2428,
48.2429, 48.2429, 48.243, 48.2437, 48.2482, 48.2487, 48.2497,
48.2511, 48.2513, 48.2631, 48.2639, 48.2642, 48.2629, 48.262,
48.2632, 48.2645, 48.2669, 48.2679, 48.2712, 48.2734, 48.2737,
48.2752, 48.2761, 48.277, 48.2783, 48.2787, 48.2812, 48.2821,
48.2839, 48.2855, 48.2859, 48.2866, 48.2873, 48.288, 48.2896,
48.2904, 48.2914, 48.2924, 48.2926, 48.2931, 48.294, 48.2944,
48.2938, 48.2926, 48.2922, 48.2898, 48.2876, 48.286, 48.2849,
48.2848, 48.2794, 48.2789, 48.2776, 48.2757, 48.2749, 48.273,
48.2737, 48.2751, 48.2769, 48.2772, 48.2808, 48.2829, 48.283,
48.2864, 48.2882, 48.2919, 48.296, 48.2989, 48.3031, 48.3099,
48.3126, 48.3164, 48.317, 48.3174, 48.3176, 48.3193, 48.3204,
48.3212, 48.3217, 48.3222), .Dim = c(807L, 2L)))), class = c("XY",
"MULTIPOLYGON", "sfg")), structure(list(list(structure(c(15.4008,
15.3968, 15.3961, 15.3958, 15.3971, 15.3978, 15.3973, 15.3957,
15.3919, 15.3911, 15.3904, 15.3823, 15.3817, 15.3814, 15.3781,
15.3778, 15.3758, 15.3736, 15.3723, 15.3696, 15.367, 15.3668,
15.3661, 15.3619, 15.3585, 15.3583, 15.3582, 15.358, 15.3584,
15.3619, 15.364, 15.3638, 15.3634, 15.3626, 15.3522, 15.3515,
15.3513, 15.3512, 15.3517, 15.3526, 15.3533, 15.3581, 15.3594,
15.3602, 15.3607, 15.3642, 15.3651, 15.3652, 15.3652, 15.3652,
15.3675, 15.3718, 15.3728, 15.3739, 15.3723, 15.3682, 15.3682,
15.3684, 15.374, 15.3772, 15.3795, 15.3804, 15.3808, 15.3805,
15.3794, 15.3761, 15.3761, 15.3753, 15.3743, 15.3714, 15.3705,
15.3693, 15.3682, 15.3674, 15.3664, 15.3654, 15.364, 15.3618,
15.3612, 15.3598, 15.3592, 15.3592, 15.3609, 15.3613, 15.366,
15.3662, 15.3684, 15.3687, 15.3687, 15.3673, 15.3674, 15.3675,
15.3681, 15.3693, 15.3718, 15.3764, 15.378, 15.3858, 15.3897,
15.3912, 15.3924, 15.3941, 15.3956, 15.3986, 15.4033, 15.4076,
15.4096, 15.4102, 15.422, 15.4228, 15.434, 15.4373, 15.4396,
15.4404, 15.4409, 15.442, 15.4422, 15.4429, 15.4476, 15.4466,
15.4484, 15.4494, 15.4509, 15.4531, 15.4534, 15.457, 15.4575,
15.4577, 15.4585, 15.4594, 15.4605, 15.4616, 15.4639, 15.4647,
15.4688, 15.4734, 15.4777, 15.48, 15.4846, 15.4862, 15.4882,
15.4888, 15.4875, 15.4884, 15.489, 15.4885, 15.487, 15.4863,
15.486, 15.4836, 15.4833, 15.4829, 15.4834, 15.4849, 15.4867,
15.4865, 15.4846, 15.4901, 15.4934, 15.4982, 15.4982, 15.4981,
15.4979, 15.4995, 15.5034, 15.5001, 15.4988, 15.4981, 15.498,
15.4989, 15.5002, 15.5015, 15.5034, 15.5049, 15.5057, 15.5069,
15.5077, 15.5081, 15.5083, 15.5082, 15.507, 15.5065, 15.5062,
15.506, 15.506, 15.5062, 15.5068, 15.5093, 15.5108, 15.5119,
15.5123, 15.513, 15.5184, 15.5206, 15.522, 15.5255, 15.5264,
15.5271, 15.5308, 15.5336, 15.5327, 15.5307, 15.5287, 15.5277,
15.5264, 15.526, 15.526, 15.5283, 15.5302, 15.5302, 15.5303,
15.5303, 15.5266, 15.5201, 15.5147, 15.5136, 15.5095, 15.5102,
15.5118, 15.5132, 15.5145, 15.5157, 15.5176, 15.5182, 15.5184,
15.5196, 15.5199, 15.5194, 15.5186, 15.5159, 15.5148, 15.5118,
15.5099, 15.5054, 15.5023, 15.4991, 15.4953, 15.4922, 15.4911,
15.4838, 15.4822, 15.4813, 15.48, 15.4792, 15.4783, 15.4771,
15.4682, 15.4651, 15.4637, 15.4624, 15.4596, 15.4459, 15.4423,
15.4369, 15.4298, 15.4268, 15.4248, 15.4223, 15.4189, 15.4173,
15.4137, 15.4108, 15.4089, 15.4073, 15.4044, 15.4008, 47.1349,
47.1327, 47.1323, 47.1322, 47.1287, 47.1254, 47.123, 47.1199,
47.1151, 47.1147, 47.1145, 47.1141, 47.1144, 47.1147, 47.1212,
47.1213, 47.1206, 47.1198, 47.1194, 47.1194, 47.1195, 47.1195,
47.1195, 47.1199, 47.1202, 47.1197, 47.1195, 47.1187, 47.1171,
47.1155, 47.1113, 47.1109, 47.1106, 47.1104, 47.108, 47.1076,
47.1071, 47.1032, 47.1024, 47.1021, 47.102, 47.1028, 47.1028,
47.1026, 47.1022, 47.0977, 47.0962, 47.0948, 47.0946, 47.0914,
47.0877, 47.0878, 47.0876, 47.0872, 47.085, 47.0806, 47.0804,
47.0803, 47.0782, 47.0788, 47.0764, 47.0754, 47.0717, 47.0704,
47.0706, 47.0712, 47.0681, 47.0657, 47.0642, 47.0628, 47.0619,
47.0582, 47.054, 47.0534, 47.0529, 47.0528, 47.0529, 47.0534,
47.0533, 47.0525, 47.0515, 47.0513, 47.0492, 47.0486, 47.0422,
47.0421, 47.0411, 47.0402, 47.039, 47.0326, 47.0322, 47.031,
47.0294, 47.028, 47.0257, 47.0231, 47.0221, 47.0198, 47.0201,
47.0187, 47.0176, 47.0173, 47.0164, 47.0163, 47.0163, 47.0163,
47.0166, 47.0167, 47.0183, 47.0185, 47.0211, 47.0219, 47.0183,
47.0169, 47.0161, 47.0163, 47.0163, 47.0153, 47.0166, 47.0177,
47.0185, 47.0176, 47.0177, 47.0189, 47.019, 47.0182, 47.0171,
47.0166, 47.015, 47.013, 47.0125, 47.012, 47.0129, 47.0132,
47.0136, 47.0153, 47.0167, 47.0172, 47.0193, 47.0197, 47.0203,
47.0205, 47.0213, 47.0217, 47.0219, 47.0227, 47.0219, 47.0217,
47.0223, 47.0246, 47.0249, 47.0255, 47.0256, 47.026, 47.0265,
47.0268, 47.0308, 47.0328, 47.0339, 47.0363, 47.0366, 47.0374,
47.0378, 47.0402, 47.0412, 47.0456, 47.0473, 47.0491, 47.0525,
47.0533, 47.0543, 47.057, 47.0589, 47.0608, 47.0622, 47.066,
47.0673, 47.068, 47.0689, 47.07, 47.074, 47.075, 47.0756,
47.0767, 47.0778, 47.0786, 47.079, 47.0807, 47.082, 47.0837,
47.0863, 47.0903, 47.092, 47.0931, 47.0941, 47.0969, 47.0973,
47.0974, 47.0973, 47.0984, 47.0998, 47.1008, 47.1018, 47.1023,
47.1034, 47.1043, 47.1052, 47.1063, 47.1072, 47.1075, 47.1103,
47.1107, 47.1095, 47.1075, 47.1069, 47.1068, 47.1074, 47.1081,
47.1095, 47.1118, 47.1144, 47.1157, 47.1171, 47.1176, 47.1177,
47.1189, 47.1201, 47.1213, 47.1234, 47.1263, 47.127, 47.1289,
47.1292, 47.1289, 47.1288, 47.1286, 47.1245, 47.1271, 47.1272,
47.1295, 47.1295, 47.1292, 47.1285, 47.1276, 47.1269, 47.1269,
47.1285, 47.1305, 47.1309, 47.1309, 47.1305, 47.1278, 47.127,
47.1276, 47.1279, 47.1281, 47.128, 47.128, 47.1277, 47.128,
47.1309, 47.1332, 47.1338, 47.1339, 47.1336, 47.1349), .Dim = c(266L,
2L)))), class = c("XY", "MULTIPOLYGON", "sfg"))), class = c("sfc_MULTIPOLYGON",
"sfc"), precision = 0, bbox = structure(c(xmin = 15.3512,
ymin = 47.012, xmax = 16.5782, ymax = 48.3222), class = "bbox"), crs = structure(list(
input = "WGS 84", wkt = "GEOGCRS[\"WGS 84\",\n DATUM[\"World Geodetic System 1984\",\n ELLIPSOID[\"WGS 84\",6378137,298.257223563,\n LENGTHUNIT[\"metre\",1]]],\n PRIMEM[\"Greenwich\",0,\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n CS[ellipsoidal,2],\n AXIS[\"latitude\",north,\n ORDER[1],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n AXIS[\"longitude\",east,\n ORDER[2],\n ANGLEUNIT[\"degree\",0.0174532925199433]],\n ID[\"EPSG\",4326]]"), class = "crs"), n_empty = 0L)), row.names = 1:2, class = c("sf",
"data.frame"), sf_column = "geometry", agr = structure(c(FID = NA_integer_,
E_pc_2000 = NA_integer_, E_pc_2001 = NA_integer_, E_pc_2002 = NA_integer_,
E_pc_2003 = NA_integer_, E_pc_2004 = NA_integer_, E_pc_2005 = NA_integer_
), .Label = c("constant", "aggregate", "identity"), class = "factor"))
My task is to calculate the spatial weights matrix and the spatial lags associated with the future dependent variable of my model: log(E).
I built the spatial weights matrix for the wide dataframe.
library(spdep)
coord <- st_coordinates(st_centroid(C_sf1))
nb2.knn1 <- knn2nb(knearneigh(coord,k=1,longlat=T))
all.linkedT <- max(unlist(nbdists(nb2.knn1, coord,longlat=T)))
all.linkedT
nb3 <- dnearneigh(coord, 0, all.linkedT,row.names=C_sf$FDI,longlat=TRUE)
n.nb3 <- nbdists(nb3, coord,longlat=TRUE);n.nb3
n.nb3.gl <- lapply(n.nb3, function(x) 1/x); n.nb3.gl
nb3.listw.gl <- nb2listw(nb3, glist=n.nb3.gl,zero.policy=F)
My problem is that I want to shift to a long format (17k+ observations), keeping the spatial weight matrix. The long format would allow me to have a variable called E (and not E_2001, E_2002, etc.) from which I can calculate the logarithm.
I tried to use the same code for the dataframe C_sf_long (obtained with reshape), but R returns me a spatial weight matrix of 0 distances. The reason is that in the panel dataset, the same coordinates are repeated for each time unit, so distance and neighbour functions only recognize a city as the neighbour of itself.
The code for the spatial lags is the following:
lag.E<- lag.listw(nb2listw(nb3),C_sf1$E)
but, as you can see, C_sf1 does not have the variable E, because it is still in the wide format.
So, I want C_sf1 to be in long format, in order to compute the spatial lags, but still I want to keep the spatial weight matrix computed with C_sf1 in wide format.

extract item from list in R using tidyverse

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')

Rollapply for rolling regression with different window sizes, save coefficients in matrixes

I'm trying to perform a rolling linear regression, whit overlapping windows and for different window sizes. I want to save the result in two tables, one with window size and slopes and one wiht window size a and intercepts.
My data set is now saved as a xts object. I'm trying to make a for-loop to generate the different window-sizes and then do a rolling regression between the two variables, with a overlapping window using rollapply. Then I would like that the coefficients for every regression get saved as a row in a matrix, that can be built on, so that every window size corresponds to one row. I have looked at a lot of previous questions, but I can not get it right.
I have hourly data for 2 variables, this is the data for the x-variable:
> dput(Xq$x)
structure(c(339.76625, 176.7196875, 142.8063125, 118.5785625,
102.0514375, 86.01156251, 86.99050001, 85.0089375, 380.0010626,
2114.279375, 2157.76875, 2442.575, 2562.6375, 2537.7125, 2581.888125,
1947.7575, 834.7918751, 765.1525001, 989.698125, 1433.2, 429.9081251,
388.333125, 1500.995, 1523.816625, 1529.090126, 439.746875, 1029.03625,
1470.07825, 1454.844813, 1448.8455, 413.7550001, 334.03125, 566.7362501,
1609.184375, 1509.266876, 1403.1425, 1142.695625, 795.138125,
1562.881875, 933.190625, 549.085625, 500.135, 535.83, 444.3493751,
356.386875, 268.1875001, 141.1878125, 119.658875, 162.0425625,
153.6944375, 161.0025, 151.37025, 155.9567501, 123.64825, 145.664125,
141.4480625, 232.2009375, 913.6480001, 2227.08125, 1870.618751,
2190.606251, 1357.81625, 824.555625, 699.99, 703.695, 620.74625,
581.2375001, 393.391875, 349.96625, 188.5445625, 154.03125, 142.0825,
339.7915625, 139.8941875, 154.6279376, 171.6016875, 151.1387501,
159.7665, 168.62025, 180.7596875, 447.2746875, 1550.11625, 1887.275,
1446.883125, 699.590625, 983.9925, 1319.62875, 769.505625, 812.713125,
638.1437501, 585.8312501, 626.7875, 1924.293125, 629.855625,
349.88375, 1038.54875, 1588.82225, 548.3475, 1053.368125, 964.825625,
990.324375, 1327.160625, 395.5862501, 421.8154376, 1308.551875,
1334.045, 1633.408125, 886.2662501, 1618.7975, 1325.59625, 847.0693751,
667.501875, 594.0531251, 249.920625, 210.8869376, 224.3725001,
209.6325, 213.64875, 210.3175001, 194.54, 90.1085625, 71.12575001,
70.69412501, 69.6075, 68.404625, 67.88837501, 67.317125, 67.220375,
67.696625, 273.2732501, 606.475625, 1277.3425, 913.24875, 612.5462501,
571.42125, 503.27375, 316.68375, 208.7436251, 127.283, 126.12375,
102.504625, 78.7938125, 70.0183125, 69.97900001, 77.7586875,
76.60543751, 94.5235625, 103.0095625, 105.1938125, 103.8090625,
104.005625, 104.544875, 105.2036875, 104.6611875, 375.7004375,
793.7725001, 782.544375, 257.065625, 387.1487501, 571.1912501,
356.6406251, 243.404375, 220.698125, 182.5680626, 161.060625,
166.5178125, 137.9805, 129.6253125, 99.4291875, 75.88075001,
65.769875, 62.62937501, 59.697875, 57.68675, 56.67275, 56.69425001,
57.296, 638.95125, 1210.92875, 990.09, 907.895625, 1122.704375,
569.139375, 295.6725, 263.21, 236.3885625, 153.935, 148.0899375,
161.72675, 102.1535, 115.8895, 114.2579375, 95.0023125, 90.72425,
84.2715, 71.06631251, 63.39812501, 75.2416875, 73.9420625, 72.26237501,
402.8870625, 1148.9775, 1058.86875, 896.47875, 890.9687501, 833.645625,
670.310625, 298.576875, 306.538125, 386.403125, 196.8343751,
174.0815, 156.6158126, 117.9345625, 105.095, 90.24362501, 87.08800001,
86.1628125, 105.3179375, 107.312625, 114.089, 104.85825, 95.9281875,
126.2874375, 529.7325626, 1533.605625, 1089.700625, 760.47, 1905.04125,
1744.460625, 1346.120625, 847.90625, 459.4625, 472.11, 474.5068751,
481.83875, 937.3618751, 267.1339376, 137.7895625, 124.0425625,
188.9461875, 108.8915, 111.67825, 84.319125, 127.1240625, 117.519125,
104.16225, 105.2490625, 158.5540625, 1215.278063, 1275.396875,
1301.14375, 1108.9225, 1280.390625, 1434.5225, 1038.926875, 599.3218751,
665.22875, 646.935, 487.604375, 236.8148126, 300.6626876, 830.2973126,
1286.612313, 403.0063751, 405.9081875, 156.5465625, 101.7661875,
100.2301875, 144.424625, 126.781125, 143.940625, 365.5354376,
849.313125, 1729.3175, 1762.6625, 1733.968751, 1275.4375, 1332.930625,
620.5625, 504.115625, 538.4556251, 433.5725, 337.094375, 225.183125,
184.7196251, 156.7545, 130.23325, 134.0796875, 145.196125, 153.243,
141.096375, 414.8042501, 304.7252501, 204.1889375, 129.7909375,
555.0276251, 1734.235625, 1780.209375, 1462.7825, 967.4743751,
1248.791875, 1001.655625, 662.92875, 546.801875, 393.72375, 308.85125,
259.985625, 280.439375, 274.1312501, 310.0375001, 270.3, 201.9288125,
219.6061875, 208.372625, 156.2023125, 162.5355626, 167.823875,
151.3928126, 151.42875, 293.7148125, 434.9101875, 943.9243751,
995.955, 614.055, 1387.864375, 1752.330626, 1238.1725, 1260.93,
565.383125, 253.179375, 275.66625, 280.4912501, 222.684375, 338.079875,
201.9365625, 136.7436875, 146.5479375, 142.794625, 150.3969375,
134.6478125, 88.22068751, 70.6445, 71.73737501, 243.176, 1559.0925,
1527.626876, 1260.648125, 1251.564375, 714.1075001, 1124.58,
901.6018751, 551.9075, 303.1025, 355.435625, 372.32375, 235.62125,
185.39475, 161.7701876, 155.4823751), index = structure(c(1438380000,
1438383600, 1438387200, 1438390800, 1438394400, 1438398000, 1438401600,
1438405200, 1438408800, 1438412400, 1438416000, 1438419600, 1438423200,
1438426800, 1438430400, 1438434000, 1438437600, 1438441200, 1438444800,
1438448400, 1438452000, 1438455600, 1438459200, 1438462800, 1438466400,
1438470000, 1438473600, 1438477200, 1438480800, 1438484400, 1438488000,
1438491600, 1438495200, 1438498800, 1438502400, 1438506000, 1438509600,
1438513200, 1438516800, 1438520400, 1438524000, 1438527600, 1438531200,
1438534800, 1438538400, 1438542000, 1438545600, 1438549200, 1438552800,
1438556400, 1438560000, 1438563600, 1438567200, 1438570800, 1438574400,
1438578000, 1438581600, 1438585200, 1438588800, 1438592400, 1438596000,
1438599600, 1438603200, 1438606800, 1438610400, 1438614000, 1438617600,
1438621200, 1438624800, 1438628400, 1438632000, 1438635600, 1438639200,
1438642800, 1438646400, 1438650000, 1438653600, 1438657200, 1438660800,
1438664400, 1438668000, 1438671600, 1438675200, 1438678800, 1438682400,
1438686000, 1438689600, 1438693200, 1438696800, 1438700400, 1438704000,
1438707600, 1438711200, 1438714800, 1438718400, 1438722000, 1438725600,
1438729200, 1438732800, 1438736400, 1438740000, 1438743600, 1438747200,
1438750800, 1438754400, 1438758000, 1438761600, 1438765200, 1438768800,
1438772400, 1438776000, 1438779600, 1438783200, 1438786800, 1438790400,
1438794000, 1438797600, 1438801200, 1438804800, 1438808400, 1438812000,
1438815600, 1438819200, 1438822800, 1438826400, 1438830000, 1438833600,
1438837200, 1438840800, 1438844400, 1438848000, 1438851600, 1438855200,
1438858800, 1438862400, 1438866000, 1438869600, 1438873200, 1438876800,
1438880400, 1438884000, 1438887600, 1438891200, 1438894800, 1438898400,
1438902000, 1438905600, 1438909200, 1438912800, 1438916400, 1438920000,
1438923600, 1438927200, 1438930800, 1438934400, 1438938000, 1438941600,
1438945200, 1438948800, 1438952400, 1438956000, 1438959600, 1438963200,
1438966800, 1438970400, 1438974000, 1438977600, 1438981200, 1438984800,
1438988400, 1438992000, 1438995600, 1438999200, 1439002800, 1439006400,
1439010000, 1439013600, 1439017200, 1439020800, 1439024400, 1439028000,
1439031600, 1439035200, 1439038800, 1439042400, 1439046000, 1439049600,
1439053200, 1439056800, 1439060400, 1439064000, 1439067600, 1439071200,
1439074800, 1439078400, 1439082000, 1439085600, 1439089200, 1439092800,
1439096400, 1439100000, 1439103600, 1439107200, 1439110800, 1439114400,
1439118000, 1439121600, 1439125200, 1439128800, 1439132400, 1439136000,
1439139600, 1439143200, 1439146800, 1439150400, 1439154000, 1439157600,
1439161200, 1439164800, 1439168400, 1439172000, 1439175600, 1439179200,
1439182800, 1439186400, 1439190000, 1439193600, 1439197200, 1439200800,
1439204400, 1439208000, 1439211600, 1439215200, 1439218800, 1439222400,
1439226000, 1439229600, 1439233200, 1439236800, 1439240400, 1439244000,
1439247600, 1439251200, 1439254800, 1439258400, 1439262000, 1439265600,
1439269200, 1439272800, 1439276400, 1439280000, 1439283600, 1439287200,
1439290800, 1439294400, 1439298000, 1439301600, 1439305200, 1439308800,
1439312400, 1439316000, 1439319600, 1439323200, 1439326800, 1439330400,
1439334000, 1439337600, 1439341200, 1439344800, 1439348400, 1439352000,
1439355600, 1439359200, 1439362800, 1439366400, 1439370000, 1439373600,
1439377200, 1439380800, 1439384400, 1439388000, 1439391600, 1439395200,
1439398800, 1439402400, 1439406000, 1439409600, 1439413200, 1439416800,
1439420400, 1439424000, 1439427600, 1439431200, 1439434800, 1439438400,
1439442000, 1439445600, 1439449200, 1439452800, 1439456400, 1439460000,
1439463600, 1439467200, 1439470800, 1439474400, 1439478000, 1439481600,
1439485200, 1439488800, 1439492400, 1439496000, 1439499600, 1439503200,
1439506800, 1439510400, 1439514000, 1439517600, 1439521200, 1439524800,
1439528400, 1439532000, 1439535600, 1439539200, 1439542800, 1439546400,
1439550000, 1439553600, 1439557200, 1439560800, 1439564400, 1439568000,
1439571600, 1439575200, 1439578800, 1439582400, 1439586000, 1439589600,
1439593200, 1439596800, 1439600400, 1439604000, 1439607600, 1439611200,
1439614800, 1439618400, 1439622000, 1439625600, 1439629200, 1439632800,
1439636400, 1439640000, 1439643600, 1439647200, 1439650800, 1439654400,
1439658000, 1439661600, 1439665200, 1439668800, 1439672400), tzone = "UTC", tclass = c("chron",
"dates", "times")), class = c("xts", "zoo"), .indexCLASS = c("chron",
"dates", "times"), tclass = c("chron", "dates", "times"), .indexTZ = "UTC", tzone = "UTC", .Dim = c(360L,
1L), .Dimnames = list(NULL, "x"))
And my y-variable:
> dput(Xq$y)
structure(c(-0.274050833, -0.236638333, -0.1994325, -0.174091667,
-0.153273333, -0.136978333, -0.124748333, -0.117348333, -0.147061667,
-0.346170833, -0.517939167, -0.575585833, -0.595914167, -0.563639167,
-0.53403, -0.5500525, -0.505650833, -0.471713333, -0.472485833,
-0.547393333, -0.390174167, -0.321545, -0.29781, -0.26912, -0.345084167,
-0.367618333, -0.279933333, -0.256805, -0.2514675, -0.314349167,
-0.3594375, -0.33482, -0.369094167, -0.4801075, -0.554780833,
-0.600498333, -0.604796667, -0.544491667, -0.636653333, -0.568401667,
-0.485494167, -0.453199167, -0.417475, -0.38417, -0.341585833,
-0.2821625, -0.248325, -0.2230575, -0.2449075, -0.2385375, -0.231885,
-0.214125, -0.194190833, -0.178575833, -0.1677675, -0.1615725,
-0.1739675, -0.2432125, -0.402414167, -0.448185833, -0.563599167,
-0.5855025, -0.55586, -0.516350833, -0.47892, -0.4603325, -0.434146667,
-0.37602, -0.320976667, -0.267863333, -0.250915, -0.241764167,
-0.271475, -0.2170225, -0.206605, -0.2088, -0.214511667, -0.2086825,
-0.203060833, -0.192895, -0.212629167, -0.319143333, -0.42647,
-0.4623275, -0.467844167, -0.506395833, -0.507088333, -0.496953333,
-0.511156667, -0.487846667, -0.4455525, -0.398383333, -0.565926667,
-0.429720833, -0.314555, -0.418586667, -0.578256667, -0.414858333,
-0.345411667, -0.3088925, -0.304373333, -0.334221667, -0.305029167,
-0.273269167, -0.315901667, -0.409731667, -0.500245833, -0.505959167,
-0.54742, -0.574725, -0.548458333, -0.5560675, -0.50246, -0.411618333,
-0.35965, -0.331884167, -0.312573333, -0.298478333, -0.289144167,
-0.274429167, -0.2218225, -0.198025833, -0.191955833, -0.1780825,
-0.157910833, -0.135935, -0.116965833, -0.099886667, -0.0864975,
-0.188904167, -0.325656667, -0.444044167, -0.5050425, -0.54236,
-0.547500833, -0.5399775, -0.521490833, -0.444911667, -0.388053333,
-0.327650833, -0.2478, -0.2026775, -0.1693425, -0.140848333,
-0.130440833, -0.103501667, -0.088843333, -0.09344, -0.126231667,
-0.158463333, -0.181145, -0.185095, -0.11657, -0.0349225, -0.223260833,
-0.431800833, -0.516, -0.487566667, -0.49941, -0.541773333, -0.511953333,
-0.4404425, -0.4021125, -0.380635, -0.3301275, -0.302468333,
-0.311290833, -0.2774125, -0.223738333, -0.192925, -0.176034167,
-0.158151667, -0.136234167, -0.117050833, -0.101201667, -0.08872,
-0.0855675, -0.257761667, -0.424794167, -0.528980833, -0.56834,
-0.591263333, -0.56574, -0.53178, -0.5410825, -0.552130833, -0.501294167,
-0.44576, -0.402855833, -0.316163333, -0.2515275, -0.213383333,
-0.186723333, -0.174315833, -0.170100833, -0.154446667, -0.147075833,
-0.169245, -0.188474167, -0.187344167, -0.2586975, -0.440815,
-0.534344167, -0.598391667, -0.613878333, -0.624658333, -0.583091667,
-0.508740833, -0.518310833, -0.51102, -0.4203825, -0.364895833,
-0.31302, -0.27689, -0.254130833, -0.232273333, -0.218198333,
-0.22501, -0.235655833, -0.242728333, -0.260448333, -0.263113333,
-0.243530833, -0.222301667, -0.274098333, -0.3974225, -0.484443333,
-0.506165, -0.6136375, -0.631805, -0.596274167, -0.539795, -0.446769167,
-0.398489167, -0.3986925, -0.4098625, -0.484515833, -0.375961667,
-0.291685, -0.273963333, -0.2621025, -0.2054525, -0.177625833,
-0.1564625, -0.141883333, -0.130685833, -0.121943333, -0.112834167,
-0.136975, -0.3204725, -0.458004167, -0.5262175, -0.527530833,
-0.520574167, -0.5800325, -0.561841667, -0.4909225, -0.4641625,
-0.443126667, -0.405705833, -0.3098625, -0.286379167, -0.30477,
-0.395341667, -0.38505, -0.356219167, -0.263050833, -0.219625833,
-0.197383333, -0.2019775, -0.226838333, -0.2333075, -0.2890475,
-0.3341175, -0.43458, -0.520441667, -0.577875, -0.568123333,
-0.551936667, -0.463691667, -0.468790833, -0.4747725, -0.4125925,
-0.394731667, -0.380213333, -0.308688333, -0.279549167, -0.249766667,
-0.233964167, -0.229904167, -0.244835833, -0.232436667, -0.215466667,
-0.198559167, -0.184533333, -0.185376667, -0.246823333, -0.395918333,
-0.4956775, -0.540474167, -0.5402375, -0.577863333, -0.561466667,
-0.503130833, -0.455221667, -0.4401875, -0.4187675, -0.389215,
-0.345275, -0.3378175, -0.348759167, -0.325149167, -0.29995,
-0.289409167, -0.291635, -0.301183333, -0.274799167, -0.2443375,
-0.2254225, -0.21272, -0.245265, -0.295081667, -0.356848333,
-0.4258325, -0.4329175, -0.487074167, -0.595525, -0.59333, -0.564645,
-0.464548333, -0.4294775, -0.425609167, -0.403769167, -0.353311667,
-0.282888333, -0.2464575, -0.231771667, -0.2275425, -0.224230833,
-0.218419167, -0.1902275, -0.161319167, -0.143495833, -0.133691667,
-0.181291667, -0.36056, -0.46681, -0.5194575, -0.532989167, -0.4899375,
-0.533224167, -0.4976575, -0.428966667, -0.412929167, -0.416463333,
-0.366666667, -0.316356667, -0.3023825, -0.282655, -0.267275833
), index = structure(c(1438380000, 1438383600, 1438387200, 1438390800,
1438394400, 1438398000, 1438401600, 1438405200, 1438408800, 1438412400,
1438416000, 1438419600, 1438423200, 1438426800, 1438430400, 1438434000,
1438437600, 1438441200, 1438444800, 1438448400, 1438452000, 1438455600,
1438459200, 1438462800, 1438466400, 1438470000, 1438473600, 1438477200,
1438480800, 1438484400, 1438488000, 1438491600, 1438495200, 1438498800,
1438502400, 1438506000, 1438509600, 1438513200, 1438516800, 1438520400,
1438524000, 1438527600, 1438531200, 1438534800, 1438538400, 1438542000,
1438545600, 1438549200, 1438552800, 1438556400, 1438560000, 1438563600,
1438567200, 1438570800, 1438574400, 1438578000, 1438581600, 1438585200,
1438588800, 1438592400, 1438596000, 1438599600, 1438603200, 1438606800,
1438610400, 1438614000, 1438617600, 1438621200, 1438624800, 1438628400,
1438632000, 1438635600, 1438639200, 1438642800, 1438646400, 1438650000,
1438653600, 1438657200, 1438660800, 1438664400, 1438668000, 1438671600,
1438675200, 1438678800, 1438682400, 1438686000, 1438689600, 1438693200,
1438696800, 1438700400, 1438704000, 1438707600, 1438711200, 1438714800,
1438718400, 1438722000, 1438725600, 1438729200, 1438732800, 1438736400,
1438740000, 1438743600, 1438747200, 1438750800, 1438754400, 1438758000,
1438761600, 1438765200, 1438768800, 1438772400, 1438776000, 1438779600,
1438783200, 1438786800, 1438790400, 1438794000, 1438797600, 1438801200,
1438804800, 1438808400, 1438812000, 1438815600, 1438819200, 1438822800,
1438826400, 1438830000, 1438833600, 1438837200, 1438840800, 1438844400,
1438848000, 1438851600, 1438855200, 1438858800, 1438862400, 1438866000,
1438869600, 1438873200, 1438876800, 1438880400, 1438884000, 1438887600,
1438891200, 1438894800, 1438898400, 1438902000, 1438905600, 1438909200,
1438912800, 1438916400, 1438920000, 1438923600, 1438927200, 1438930800,
1438934400, 1438938000, 1438941600, 1438945200, 1438948800, 1438952400,
1438956000, 1438959600, 1438963200, 1438966800, 1438970400, 1438974000,
1438977600, 1438981200, 1438984800, 1438988400, 1438992000, 1438995600,
1438999200, 1439002800, 1439006400, 1439010000, 1439013600, 1439017200,
1439020800, 1439024400, 1439028000, 1439031600, 1439035200, 1439038800,
1439042400, 1439046000, 1439049600, 1439053200, 1439056800, 1439060400,
1439064000, 1439067600, 1439071200, 1439074800, 1439078400, 1439082000,
1439085600, 1439089200, 1439092800, 1439096400, 1439100000, 1439103600,
1439107200, 1439110800, 1439114400, 1439118000, 1439121600, 1439125200,
1439128800, 1439132400, 1439136000, 1439139600, 1439143200, 1439146800,
1439150400, 1439154000, 1439157600, 1439161200, 1439164800, 1439168400,
1439172000, 1439175600, 1439179200, 1439182800, 1439186400, 1439190000,
1439193600, 1439197200, 1439200800, 1439204400, 1439208000, 1439211600,
1439215200, 1439218800, 1439222400, 1439226000, 1439229600, 1439233200,
1439236800, 1439240400, 1439244000, 1439247600, 1439251200, 1439254800,
1439258400, 1439262000, 1439265600, 1439269200, 1439272800, 1439276400,
1439280000, 1439283600, 1439287200, 1439290800, 1439294400, 1439298000,
1439301600, 1439305200, 1439308800, 1439312400, 1439316000, 1439319600,
1439323200, 1439326800, 1439330400, 1439334000, 1439337600, 1439341200,
1439344800, 1439348400, 1439352000, 1439355600, 1439359200, 1439362800,
1439366400, 1439370000, 1439373600, 1439377200, 1439380800, 1439384400,
1439388000, 1439391600, 1439395200, 1439398800, 1439402400, 1439406000,
1439409600, 1439413200, 1439416800, 1439420400, 1439424000, 1439427600,
1439431200, 1439434800, 1439438400, 1439442000, 1439445600, 1439449200,
1439452800, 1439456400, 1439460000, 1439463600, 1439467200, 1439470800,
1439474400, 1439478000, 1439481600, 1439485200, 1439488800, 1439492400,
1439496000, 1439499600, 1439503200, 1439506800, 1439510400, 1439514000,
1439517600, 1439521200, 1439524800, 1439528400, 1439532000, 1439535600,
1439539200, 1439542800, 1439546400, 1439550000, 1439553600, 1439557200,
1439560800, 1439564400, 1439568000, 1439571600, 1439575200, 1439578800,
1439582400, 1439586000, 1439589600, 1439593200, 1439596800, 1439600400,
1439604000, 1439607600, 1439611200, 1439614800, 1439618400, 1439622000,
1439625600, 1439629200, 1439632800, 1439636400, 1439640000, 1439643600,
1439647200, 1439650800, 1439654400, 1439658000, 1439661600, 1439665200,
1439668800, 1439672400), tzone = "UTC", tclass = c("chron", "dates",
"times")), class = c("xts", "zoo"), .indexCLASS = c("chron",
"dates", "times"), tclass = c("chron", "dates", "times"), .indexTZ = "UTC", tzone = "UTC", .Dim = c(360L,
1L), .Dimnames = list(NULL, "y"))
My code looks like this:
library(zoo)
mat_slope=matrix(nrow=i)
mat_intercept=matrix(nrow=i)
for(i in seq(from=24, to=240, by=24)){
mov.reg<- rollapplyr(Xq,
width= i,
by=24,
FUN = function(y,x)
{
coefficients(lm(formula=y~x, data = Xq))
},
by.column=FALSE)
mat_slope[i] <- coefficients(mov.reg)[2]
mat_intercept[i] <-coefficients(mov.reg)[1]
mat_intercept=rbind(i,mat_intercept)
mat_slope[n,i]=rbind(i, mat_slope)
}
I either get an empty matrix as result, or the following error message:
Error in merge.xts(res, xts(, idx, attr(data, "frequency"))) :
(list) object cannot be coerced to type 'double'
If anyone have some inputs or ideas it would be very appreciated! Thank's a lot!
mov.reg is not an lm objct, so it does not make sense to take coefficients of it. Also there is a reference to chron in the dput output so we need to load the chron package.
Xq is not specified but rather two separate xts objects are shown so we have assumed that those two are called x and y. Then create a single zoo object z from them. Now define a Coef function which takes a matrix with x and y columns and performs the indicated regression returning the coefficients. Also define roll which takes the width as input and runs rollapplyr with the desired arguments. Now use lapply to run roll with each width producing a list of zoo objects. This list has one component for each element in widths consisting of a zoo object with intercept and slope columns. Finally extract the intercepts from each component in the list and form a zoo object and do the same for the slopes. Note that if L is a list of zoo objects then do.call("merge", L) will produce a single zoo object from it.
library(xts) # this also loads zoo
library(chron)
# inputs are xts objects x and y
z <- cbind(x = as.zoo(x)[, 1], y = as.zoo(y)[, 1])
Coef <- function(m) coef(lm(y ~ x, as.data.frame(m)))
roll <- function(w) rollapplyr(z, w, Coef, by = 24, by.column = FALSE)
widths <- seq(24, 240, 24)
names(widths) <- widths # lapply will use these names for its output
L <- lapply(widths, roll)
intercepts <- do.call("merge", lapply(L, "[", TRUE, 1)) # extract 1st columns
slopes <- do.call("merge", lapply(L, "[", TRUE, 2)) # 2nd columns
Alternately do the rollapplyr twice replacing the last 3 lines with:
intercepts <- do.call("merge", lapply(widths, function(w) roll(w)[, 1]))
slopes <- do.call("merge", lapply(widths, function(w) roll(w)[, 2]))

How to calculate rolling Geometric Mean since inception in R

How can I calculate the rolling geometric mean of the following xts time-series called RET :
RET <- structure(c(0.235313703701719, 0.0842795890067098, -0.233550157364016,
0.193002483647028, -0.0689226509620672, 0.179534446046147, -0.127248883358903,
-0.0743470123083659, -0.135639827536771, -0.164152686819086,
-0.0204543751350705, 0.0664461087883466, 0.0272380829324071,
0.227128887694961, -0.178562010800951, 0.395443526993695, 0.314881406042804,
0.181818850951155, -0.206752910000026, 0.0263153570715485, -0.175020412517093,
-0.01581249554214, -0.15172107661987, 0.107045745638528, 0.0906084021475712,
0.132978183241953, -0.00909419932203037, 0.0140911196519935,
0.213807819676506, -0.18193702641405, -0.227678899382835, 0.301040730720012,
0.0601578951831836, 0.100516179349791, -0.404945499516844, -0.062500075,
-0.203765311964576, -0.07692312083335, -0.0836064900993851, 0.221099166706208,
-0.0815457861285995, 0.316192911249967, -0.0486118279242403,
-0.173325434709271, 0.59325308527751, -0.00727384538023071, -0.0506951075961164,
0.546153054783499, 0.251190702854411, 0.0541671297506965, -0.231331680522771,
0.343474297824453, -0.0996647581834428, -0.16666622673471, -0.0108547028404369,
0.181845988421045, -0.126238743703789, 0.115831564907218, -0.086580986998594,
-0.100940420552943, -0.206575364601145, 0.131537865642544, -0.165064660739094,
-0.0281081354328821, -0.121059125713363, 0.123960117666879, -0.0262060896817153,
0.233629847113372, -0.0283857077499662, 0.0414875891677511, -0.139489434074546,
-0.291593674635723, -0.0394733826899257, -0.154468409162332,
-0.0228881657209516, 0.0583326253535777, -0.304457954370351,
-0.26778991307437, -0.33333268430204, 0.0600079321234409, 0.438760787944855,
-0.223958720648733, 0.7370589189836, 0.090349969129766, -0.267827471745488,
0.00221064777031615, -0.704447751043575, -0.131282309489721,
0, 0, -0.378290758732488, -0.0216356021418299, 0.116571563338307,
-0.105867472732954, -0.172970309117826, 0.0636995840642282, 0.0364789030575694,
0.0952821024300478, 0.230263394963683, -0.0485614318546264, -0.115799574888004,
-0.0120447695973589, 0.760532897132213, -0.289305826263387, 0.0411514151084162,
0.0673184054668599, -0.233772989321894, -0.0892615248786987,
-0.159921094234639, -0.0449699589101911, 0.59114150445647, -0.00487865744023711,
0.0392264443174142, 0.083080680760974, -0.604569467038781, -0.239333037134679,
-0.225872722618789, -0.474746305159428, -0.490736714052568, 0.228916653763629,
0.0308321929055187, 0.0943914873080436, -0.0712560264787054,
0.0798695334307741, -0.135961317412806, 0.0412890533446434, -0.0991550904983131,
0.300616722621631, -0.0418878938161028, -0.380127803498024, -0.0611938814657424,
0.01369237597809, -0.251729062508919, -0.0775118446872033, -0.314275408058115,
0.0876429361863786, 0.062043712399028, 0.134824568530155, 0.0158774971284649,
0.716661259261616, 0.0709424520436908, -0.227571455891104, -0.176479546620475
), index = structure(c(299401200, 307263600, 315388800, 323251200,
331110000, 339058800, 347011200, 354787200, 362646000, 370594800,
378547200, 386323200, 394182000, 402130800, 410083200, 417859200,
425718000, 433666800, 441532800, 449395200, 457254000, 465116400,
473241600, 480844800, 488703600, 496825200, 504777600, 512553600,
520412400, 528361200, 536313600, 544089600, 551948400, 559897200,
567849600, 575712000, 583570800, 591519600, 599385600, 607248000,
615106800, 622969200, 630835200, 638697600, 646556400, 654418800,
662544000, 670060800, 678006000, 686127600, 694080000, 701942400,
709801200, 717750000, 725702400, 733478400, 741337200, 749286000,
757238400, 765014400, 772873200, 780822000, 788688000, 796550400,
804409200, 812271600, 820137600, 8.28e+08, 835858800, 843980400,
851932800, 859708800, 867567600, 875516400, 883468800, 891244800,
899103600, 907052400, 915004800, 922780800, 930639600, 938588400,
946540800, 954403200, 962262000, 970124400, 977990400, 985852800,
993711600, 1001574000, 1009699200, 1017216000, 1025161200, 1033282800,
1041235200, 1049011200, 1056870000, 1064818800, 1072771200, 1080633600,
1088492400, 1096441200, 1104393600, 1112169600, 1120028400, 1127977200,
1135843200, 1143705600, 1151564400, 1159426800, 1167292800, 1175151600,
1183014000, 1190876400, 1199001600, 1206860400, 1214722800, 1222671600,
1230624000, 1238396400, 1246258800, 1254207600, 1262160000, 1269932400,
1277794800, 1285743600, 1293696000, 1301468400, 1309330800, 1317279600,
1325145600, 1333004400, 1340866800, 1348729200, 1356854400, 1364367600,
1372316400, 1380438000, 1388390400, 1396162800, 1404025200, 1411974000,
1419926400), tclass = c("POSIXlt", "POSIXt")), .indexCLASS = c("POSIXlt",
"POSIXt"), .indexTZ = "", tclass = c("POSIXt", "POSIXct"), tzone = "", class = c("xts",
"zoo"), .Dim = c(143L, 1L), .Dimnames = list(NULL, "-1,-1"))
I have tried using rollapply from library(zoo) & also mean.geometric from library("PerformanceAnalytics) but it is incorrect:
rollapply(RET,1:nrow(RET), mean.geometric)
Transferred from comments.
RET is an xts object and rollapply.xts in the xts package does not support vector widths.
Try this to cause it to use rollapply.zoo in the zoo package:
rollapplyr(as.zoo(RET), 1:nrow(RET), mean.geometric)

Resources