mpiktas / midasr

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

midasR does not update forecast #71

Closed p-wegmueller closed 4 years ago

p-wegmueller commented 5 years ago

I have some quarterly target variable, say GDP, and monthly indicator, say PMI, and I would like to produce a one period ahead nowcast. Now it appears to me, that adding monthly information does not change at all my forecast. The advantage of midas should exactly be, that by adding new high frequency information my prediction becomes more accurate. What am I doing wrong? You can find a minimum working example attached (you might need to install the package “tsbox”).

rm(list=ls())
library(midasr)
library(tsbox)

target <- structure(c(0.266428630623139, 0.70410495439146, 0.0988518736981359, 
                      0.260824411014937, -0.274261605078896, -0.28800784225822, 0.528326109457433, 
                      0.759590976781332, 0.72343585506156, 1.17949261346435, 0.918049843976609, 
                      0.693776278596725, 1.19427275038353, -0.108419822736572, -0.335078327372762, 
                      0.069697152225956, 0.905553302451545, 0.622615555763928, 2.36833485908001, 
                      0.766526926095801, 0.425096045600215, 0.764976161118058, 0.982321935881902, 
                      0.0394620447124794, 0.249797697095699, -0.313430808733728, -0.0452871629726737, 
                      0.277059952363246, 0.0704636062030195, 0.160228360644266, -0.383746907881699, 
                      -0.685229692594946, 0.220235848774175, 0.966231798250239, 1.22064427878799, 
                      0.675412514930507, 0.404604036043832, 0.26815489824239, 0.153161663637036, 
                      1.2035131681075, 1.12747356979452, 0.99687827855417, 0.894942811226063, 
                      1.11493923628825, 0.859434359858602, 0.824552133000189, 1.56256925505669, 
                      0.862776980076463, 1.09306661045867, 0.821257431887701, 0.627233985688003, 
                      0.885153904952607, 0.6469971482856, 0.117925881212222, -1.91249845187939, 
                      -1.5626123596029, -0.00740692730666348, 0.992558160195567, 0.255827051005642, 
                      1.0319878164319, 0.964543516701677, 0.449297887519595, 0.710941082442984, 
                      0.464813662790275, 0.495651609426218, -0.323859067182497, 0.611865851879689, 
                      0.0909650820781316, 0.234551399160421, 0.52482296658789, 0.208421744760345, 
                      0.405764942342723, 0.770679986089085, 0.715152606221214, 0.184034097047148, 
                      0.722676230111019, 0.600584358454137, 0.786981419517385, 0.829851298286943, 
                      -0.321798816436347, 0.00189606354776828, 0.65526389982864, 0.389961431087382, 
                      0.555229299682103, 0.301479882219846, 0.418150363475767, -0.129878809720918, 
                      0.35462371317907, 0.718732613695705, 0.779873124338626, 0.800597002156112, 
                      0.805144506155964, 0.707553200175237, -0.177786737607977, 0.328516834475923, 
                      0.562161492852198), .Tsp = c(1995.25, 2019, 4), class = "ts")

indic <- structure(c(82.367947, 89.496324, 89.189468, 95.647959, 93.701762, 
                     95.146106, 88.18249, 90.592763, 86.782091, 92.462421, 91.257196, 
                     91.279717, 99.289801, 95.100359, 99.442463, 97.853204, 97.745476, 
                     99.237442, 98.98951, 102.594965, 109.764865, 111.68937, 110.900144, 
                     111.283934, 110.143938, 109.912474, 111.748272, 111.190325, 111.731327, 
                     114.630142, 116.57042, 110.198205, 109.080133, 105.33659, 101.080511, 
                     102.556235, 100.188429, 103.34515, 98.583811, 100.718893, 98.580131, 
                     97.34064, 96.528458, 90.723127, 93.218808, 98.11689, 98.878931, 
                     97.852869, 95.877329, 103.152777, 107.051899, 105.815595, 109.713549, 
                     111.309712, 110.462385, 111.732401, 111.506771, 107.247282, 106.446001, 
                     107.17918, 107.947376, 105.857208, 105.388225, 104.202513, 102.05195, 
                     99.802293, 101.277483, 101.125562, 99.200576, 96.125685, 91.335578, 
                     91.635707, 90.184192, 86.142072, 87.904143, 87.843951, 90.593168, 
                     87.856153, 78.934333, 80.471311, 86.182948, 90.145084, 99.049327, 
                     103.046862, 98.560773, 104.492707, 99.743196, 95.590096, 96.540858, 
                     94.52008, 94.444179, 94.518136, 95.795096, 97.708164, 95.560877, 
                     93.904501, 93.386614, 96.094258, 97.430763, 102.207355, 103.871928, 
                     108.120462, 110.055042, 113.067471, 113.829004, 110.85791, 110.496113, 
                     108.913823, 111.305105, 110.465186, 108.488768, 107.087444, 105.074281, 
                     103.615195, 100.973765, 98.750259, 100.588758, 99.928921, 102.362053, 
                     101.398401, 98.686876, 97.973689, 99.589141, 101.663807, 102.766792, 
                     105.54765, 107.246374, 107.515149, 108.030854, 107.181679, 107.189498, 
                     106.567958, 108.210952, 107.265111, 105.37065, 104.760064, 103.774935, 
                     103.653534, 103.256605, 104.447976, 105.464151, 104.749773, 104.360637, 
                     105.750277, 102.05045, 98.855829, 99.651295, 100.13595, 99.691824, 
                     97.849374, 96.357744, 98.061828, 98.105721, 97.98543, 91.5158, 
                     87.100658, 85.106782, 83.089881, 82.685573, 82.508489, 80.692245, 
                     80.477073, 79.542071, 72.04227, 71.810699, 72.310899, 76.758523, 
                     80.445426, 86.474779, 97.634793, 109.43118, 107.069073, 115.133871, 
                     119.15478, 116.901099, 119.297175, 116.928228, 111.782267, 115.383136, 
                     114.311518, 116.735754, 115.656777, 109.839587, 109.586402, 108.503497, 
                     106.413587, 103.830018, 105.394389, 105.463282, 101.578095, 101.348806, 
                     101.328136, 99.673043, 98.680178, 93.839655, 90.053374, 84.428219, 
                     81.945922, 84.351076, 89.064789, 93.843095, 97.438036, 98.566281, 
                     102.495499, 101.712136, 99.56499, 95.663425, 97.516904, 100.421771, 
                     101.702413, 102.342301, 100.974185, 102.923983, 106.009953, 105.270138, 
                     105.109622, 102.662513, 100.58347, 101.903344, 106.549291, 105.741942, 
                     108.184049, 106.652851, 107.833646, 106.310688, 105.436943, 105.686813, 
                     103.554297, 104.128832, 99.560094, 99.694678, 99.65034, 100.29515, 
                     97.934917, 98.447491, 97.595788, 98.460995, 95.342349, 87.358498, 
                     88.478451, 88.407437, 103.711831, 100.023866, 99.19327, 100.955785, 
                     101.877029, 102.774259, 99.505622, 98.983513, 101.501759, 101.111827, 
                     102.037397, 103.928513, 101.806773, 101.125499, 102.821669, 101.549022, 
                     101.879505, 104.040293, 101.754453, 102.941313, 103.401315, 105.44494, 
                     106.162723, 107.403911, 105.074682, 106.084109, 108.948054, 105.578998, 
                     104.494556, 106.216286, 108.084991, 110.284335, 106.587204, 104.930514, 
                     102.674238, 103.147818, 99.896339, 100.187951, 101.663663, 98.900712, 
                     102.428731, 100.228196, 98.89637, 96.325601, 96.216014, 93.134112, 
                     97.197047, 96.237591, 94.445182), .Tsp = c(1995.25, 2019.33333333333, 
                                                                12), class = "ts")

# Forecast horizon
h <- 1
# Frequency factor
frq <- frequency(indic)/frequency(target)

# ----- 
# In-Sample Estimation
# High and low frequency need to have same starting date
target.aligned <- ts_span(target, template = indic)
# Align indicator to low frequency target
indic.aligned <- ts_span(indic, 
                         start = ts_summary(target.aligned)$start,
                         end = add_to_date(ts_summary(target.aligned)$end, "2 month"))

if (add_to_date(ts_summary(target.aligned)$end, "2 month") > ts_summary(indic.aligned)$end) {
  warning("Indicator is shorter than target")
}

insample <- length(target.aligned)*frq
actual_q <- ts_summary(target)$end

# U-MIDAS with AR(1) for YLow and up to 6 lags for YHigh
m1 <- midas_r(target.aligned ~ mls(target.aligned, 1, 1) + mls(indic.aligned, (h*frq) + 0:6, frq), start = NULL) 
summary(m1) 

m2 <- midas_r(target.aligned ~ mls(target.aligned, 1, 1) + mls(indic.aligned, (h*frq) + 3:9, frq), start = NULL) 
summary(m2) 

m3 <- midas_r(target.aligned ~ mls(target.aligned, 1, 1) + mls(indic.aligned, 0:6, frq), start = NULL) 
summary(m3) 

m4 <- midas_r(target.aligned ~ mls(target.aligned, 1, 1) + mls(indic.aligned, 3:9, frq), start = NULL) 
summary(m4) 

# ----- 
# Out-of-Sample Forecast

# Extend indic with NAs for forecasting horizon
fct_indic <- indic %>%
  ts_span(start = add_to_date(ts_summary(indic.aligned)$end, "1 month")) %>%
  window(end = data.table::year(ts_summary(target)$end)+1-1/frequency(indic),
         extend = TRUE) %>%
  as.vector()

fct_indic[1:2] <- NA
fct_indic0 <- fct_indic

fct_indic <- indic %>%
  ts_span(start = add_to_date(ts_summary(indic.aligned)$end, "1 month")) %>%
  window(end = data.table::year(ts_summary(target)$end)+1-1/frequency(indic),
         extend = TRUE) %>%
  as.vector()

fct_indic[2] <- NA
fct_indic1 <- fct_indic

fct_indic2 <- indic %>%
  ts_span(start = add_to_date(ts_summary(indic.aligned)$end, "1 month")) %>%
  window(end = data.table::year(ts_summary(target)$end)+1-1/frequency(indic),
         extend = TRUE) %>%
  as.vector()

fct_indic0 # no information from 2019Q2
fct_indic1 # one month of 2019Q2
fct_indic2 # two months of 2019Q2

# Model 1
fct.m10 <- forecast(m1,
                newdata = list(indic.aligned = fct_indic0),
                method = "dynamic")$mean
fct.m11 <- forecast(m1,
                    newdata = list(indic.aligned = fct_indic1),
                    method = "dynamic")$mean
fct.m12 <- forecast(m1,
                    newdata = list(indic.aligned = fct_indic2),
                    method = "dynamic")$mean
fct.m10
fct.m11
fct.m12

# Model 2
fct.m20 <- forecast(m2,
                    newdata = list(indic.aligned = fct_indic0),
                    method = "dynamic")$mean
fct.m21 <- forecast(m2,
                    newdata = list(indic.aligned = fct_indic1),
                    method = "dynamic")$mean
fct.m22 <- forecast(m2,
                    newdata = list(indic.aligned = fct_indic2),
                    method = "dynamic")$mean
fct.m20
fct.m21
fct.m22  

# Model 3
fct.m30 <- forecast(m3,
                    newdata = list(indic.aligned = fct_indic0),
                    method = "dynamic")$mean
fct.m31 <- forecast(m3,
                    newdata = list(indic.aligned = fct_indic1),
                    method = "dynamic")$mean
fct.m32 <- forecast(m3,
                    newdata = list(indic.aligned = fct_indic2),
                    method = "dynamic")$mean
fct.m30
fct.m31
fct.m32  

# Model 4
fct.m40 <- forecast(m4,
                    newdata = list(indic.aligned = fct_indic0),
                    method = "dynamic")$mean
fct.m41 <- forecast(m4,
                    newdata = list(indic.aligned = fct_indic1),
                    method = "dynamic")$mean
fct.m42 <- forecast(m4,
                    newdata = list(indic.aligned = fct_indic2),
                    method = "dynamic")$mean
fct.m40
fct.m41
fct.m42