mpiktas / midasr

R package for mixed frequency time series data analysis.
http://mpiktas.github.io/midasr/
Other
73 stars 34 forks source link

define h if is.na(freqinfo[firstno]) (happens using mlsd) #81

Closed marcburri closed 2 years ago

marcburri commented 2 years ago

If we use mlsd() to link the explanatory variables, we get a forecasting horizon, h, to be NA. This creates an error when we want to forecast. See minimal example below. This pull request solves this issue.

y <- c(-0.36932108133797, 1.59958791364494, 0.465088139067005, 0.546583156752201, 
0.570558582577463, -0.259531666800761, -0.800741483814538, 0.239265491653029, 
0.344480373844469, 0.290218468886905, 0.0695127900113945, -0.0803264640113932, 
-1.13593260958158, 0.520941465354507, 0.629072963620136, 0.82524245675839, 
0.878936309868394, 0.30226735106571, 0.148682309502202, 0.188107997870524, 
0.32833538937429, 0.867759396462087, 0.913165024229468, 0.699649705371397, 
0.751393364620467, 0.732800642760578)

x <- c(0.0592603475427383, -1.5424682795433, -1.54909808135425, -0.743527899980445, 
-0.448759457908747, -0.634487220028, -0.560663523368721, -0.766697995287456, 
-0.935660790466697, -0.901733039515009, -1.35584301403059, -1.19669528575176, 
-1.03421896719237, -0.0772471640567147, -0.653315575532553, -0.598226922740598, 
-0.383187311038663, -0.376710256939126, -0.624213298362408, -1.04581459374376, 
-1.33459569170703, -1.335987161832, -1.49356917143351, -1.24943817380203, 
-1.40140139533028, -1.70240689605337, -1.80679493661965, -2.2958799151144, 
-2.00783826225602, -1.65002699955878, -1.62036286616239, -1.80422948348576, 
-2.98850090625288, -2.89691789700064, -2.84314504088133, -2.65997857519829, 
-2.09384918900564, -1.77366065226305, -2.17391872492993, -2.11869384293538, 
-0.823321022955195, -0.836114991800722, -1.55952341794227, -1.48250428127835, 
-1.38984542125239, -1.47251763469294, -2.13498108559734, -1.04310398434452, 
-0.820783708015493, -1.468785133569, -0.373097047819648, -0.197037954823613, 
-0.671815902363233, -0.400009440912732, -0.801937953819844, -1.43536278275941, 
-0.945306814254031, -1.58346848161341, -1.50665438236158, -0.465499985807274, 
-0.6017357712279, -0.910163614108858, -0.00866541953121413, 0.900720485039342, 
0.436408005555881, 0.966904513764404, 0.215919995384966, -0.0580077636566889, 
-0.986965037246307, -1.0779696923597, -0.733377897739903, -0.890448171101792, 
-0.707997175625666, -1.06034282050603, -0.784660727560514, -0.663152937959982, 
-0.636292065411833, -0.572469232043483, -0.633692134268055, -0.230171487456539, 
-0.103444257738238, 0.270295535892645, 0.318246773179172, 0.370090056932825, 
0.301787521572808, 0.487972912109942, 0.258596233328086, 0.297182398694771, 
2.20885344462677, 3.48084755657939, 2.69499384511476, 3.03883621787113, 
2.47213456369002, 2.67837660436608, 1.77089119947622, 2.37081849217791, 
1.36917005801431, 1.07298676648431, 0.880836822551043, 0.672953570824479, 
0.787775884026219, 1.1203412603053, 0.785741569834754, 0.597222552974119, 
0.777492124846992, 0.632941878301678, 0.187974199405217, 0.305950784038599, 
0.639733666637607, 0.828788028148295, -0.0395968982581865, 0.0823942880665951, 
0.201512363618048, -0.161715201917208, 0.198849208279252, -0.631901395935831, 
-0.941750949385492, -0.984071277854873, -0.844984338340149, -0.990463834188332, 
-0.963109552798302, -1.05754194635016, -0.761260026173029, -1.41910322211392, 
-1.2724402604875, -1.27076789713761, -0.38458272787066, -0.108879725098465, 
0.318046044801448, 0.769352551452237, 1.50514597016011, 1.5667896829065, 
2.36890018414991, 3.33362936772026, 2.20078346044663, 2.92228166592639, 
2.06763039209062, 1.33923881955381, 1.37075323800479, 2.27586254319252, 
2.4165082442125, 2.72721968064311, 3.04518295511999, 2.74873511198165, 
2.93739417919675, 2.09850517441977, 2.1389522114247, 1.70486759501829, 
1.3036645451796, 1.62443457022147, 0.747095746932796, 0.0914839864331372, 
0.841055533606974, 1.11479610336601, 1.12912212960454, 1.69698689495724, 
1.12650992125018, 1.16232140337314, 0.970187014463952, 1.02343894645373, 
1.48468597309136, 1.68093060695359, 2.03509655857277, 1.60350607045973, 
2.18313609058445, 1.66410541472561, 2.08988812046942, 1.7400561102056, 
1.68903206958271, 1.93774927239165, 1.33253686951841, 0.819517526100809, 
0.00574938840055046, 0.0600984555464836, 0.0761310973127767, 
0.111763167367093, 0.142641362336208, -0.653232759482881, -0.355736337767776, 
-0.573250680203924, -0.468664393673089, -0.897113729311318, -0.426140844984352, 
-0.50841612905056, -0.721002792309358, -0.397510452474831, -0.405767206897272, 
-0.40130100806111, -0.982029483416113, -1.24605898717854, -1.20490504440425, 
-1.37931692474709, -0.960359537858566, -1.35719022093317, -0.985281680009873, 
-0.721402733841779, -1.16261378615207, -1.55981943440733, -1.52904820309832, 
-2.26076621351323, -1.80692555138555, -2.18556509036424, -1.67306584539234, 
-2.09865616462189, -2.44781644096461, -2.02223584602317, -2.03978186027063, 
-1.93810258501042, -2.37384462365025, -2.36431564988841, -1.99331378168635, 
-2.24042798478652, -2.6163958185759, -2.3742153750996, -2.46232279375715, 
-2.74075639556436, -2.56657826650246, -2.34964019500193, -1.50116485560086, 
-1.21585091940659, -2.03461224989362, -2.30832941652125, -2.89100122325609, 
-2.5077731883437, -2.65039835040597, -2.82682923890365, -2.57558497705988, 
-2.12577992090801, -2.11509386792352, -2.84384368262408, -2.35981624962147, 
-2.6531347597445, -2.74316098796716, -2.80970088743471, -2.55595140917491, 
-2.33614758113693, -1.96579914852588, -2.79217027042073, -2.84073985396813, 
-2.33859952989744, -1.78329307662742, -2.13229706432071, -2.73031456957734, 
-2.82476439170589, -2.52963873556619, -2.88701787072242, -3.14393619758673, 
-3.40900299827075, -3.05923642470645, -2.49709299003099, -2.68016852009371, 
-2.9335315697112, -2.47400082648602, -2.97995356560346, -2.93937836283811, 
-3.05923312741624, -3.04709887717383, -2.79586986781379, -2.59327692778692, 
-2.4129238366729, -1.77591855602567, -2.75238587650481, -2.9989918708421, 
-2.77575422916838, -2.70244778049138, -2.62442785898879, -2.61497073739761, 
-2.77647982823749, -2.74996692742695, -2.86366096190145, -2.69606383139313, 
-2.89629662397402, -3.01529024559895, -2.97303702555104, -2.70509972767691, 
-2.77897487007366, -2.37040200812875, -2.75443394276511, -2.50167332006146, 
-2.16097305581369, -2.23355803141409, -2.41360764891064, -2.56023684148741, 
-2.29162589379385, -2.69611411919547, -3.26659493035858, -2.15483097611383, 
-2.60477391423598, -2.52294119820338, -2.92795832676622, -3.17228066801027, 
-2.99458429933384, -2.85689277627644, -2.70377413479172, -2.89121888245725, 
-2.12828874082385, -2.54186446094659, -2.80421805614627, -2.64621675619098, 
-3.34668531937375, -3.26514994674159, -2.56740934543747, -2.73989816418992, 
-2.56835252704013, -2.5883644937442, -2.62706871485038, -2.50912208138912, 
-2.80279465839382, -2.80158513032831, -2.21919853627024, -2.61874166781764, 
-2.28600077495484, -2.04358914794345, -2.49066482324152, -2.56368571794961, 
-1.96340731750044, -2.71537679718376, -2.69677101035934, -2.40844225047042, 
-2.62001104129228, -2.50177691046725, -2.63944908976048, -2.96769537899091, 
-3.01911760450651, -2.81158881152494, -2.77923238590708, -3.01896410839988, 
-2.67608552849993, -2.5213964970023, -3.01439763932678, -3.17729202588738, 
-3.02159634112143, -2.56188496437497, -2.20282697399644, -2.06131588802531, 
-1.60246806402225, -1.46045825576379, -2.48553465467559, -2.59339782312045
)

indy <- 1:26

indx  <- c(1L, 14L, 27L, 40L, 54L, 66L, 79L, 93L, 106L, 119L, 132L, 145L, 
158L, 171L, 184L, 197L, 210L, 223L, 236L, 249L, 262L, 275L, 288L, 
301L, 314L, 327L)

xlags <- 0:12

um <- midas_r(y ~ mlsd(y, 1, indy) + mlsd(x, xlags, indx), start = NULL)

yp <- 0.732800642760578

xp <- c(-3.17729202588738, -3.02159634112143, -2.56188496437497, -2.20282697399644, 
-2.06131588802531, -1.60246806402225, -1.46045825576379, -2.48553465467559, 
-2.59339782312045)

fct <- forecast(um,  newdata = list(y = yp, x = xp), method = "static")