mpiktas / midasr

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

two step ahead forecast #73

Closed p-wegmueller closed 4 years ago

p-wegmueller commented 4 years ago

Simple question: why does this not work? I wish to produce a 2-step ahead forecast of 2005Q1 GDP growth. The last observation for GDP is 2004Q3. Suppose we have high-frequency data until January 2005. Hence, I wish that my midas-function returns a backcast for 2004Q4 and a nowcast for 2005Q1. What am I doing wrong. The output I get is

fct Qtr1 Qtr2 Qtr3 Qtr4 2004 0.3506384 2005 NA NA NA NA 2006 NA NA NA NA 2007 NA NA NA NA

Question apart: The forcasts seem extremly sensitive to choosing k in mls(x, k, m, ...). Can you please clarify to me what I have to keep in mind when specifying k? Is there an optimal k or is it chosen arbitrarily?

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

px <- 5 py <- 2 frq <- 3 param <- 0

target.aligned <- structure(c(-0.567827346649608, -0.187166344088308, 0.0852351424492248, 0.19161574655624, 0.380373573599102, 0.0867945298276718, -0.0666257746568255, -0.136515159532247, 0.546807112731273, 1.02232246369174, 0.948337019571843, 0.789574311964536, 0.890028197897985, 0.657567103350853, 0.246365105008084, -0.199427903791149, -0.0252306077548781, 0.548147101281038, 0.930685514637974, 1.21554545364118, 1.18620581211364, 0.557498057498051, 0.692526126683024, 0.396159269455443, 0.529312848735009, 0.0731813948183779, -0.605916710195165, -0.0554191310662477, 0.126196235145648, 0.467865292988701, 0.299369897644008, 0.0483247422680355, -1.10051426785495, 0.0737371319128544, 0.543530267363312, 0.524412296564192, 0.438359795873922, 0.427962746502764, 0.352925716645713), .Tsp = c(1995, 2004.5, 4), class = "ts")

indic.aligned <- structure(c(0.914557182836136, 0.714047975128888, 0.619112969748766, 0.261654510340764, 0.0259244074018232, 0.10957310301566, -0.0625631790215829, -0.223553459302259, -0.321664371338846, -0.697916272550625, -0.601890063113876, -0.698297144882488, -0.920733922582342, -0.900290531210944, -1.02660098638598, -1.25466704338573, -1.31719056944774, -1.1743849775728, -1.27973863873126, -1.12994732280183, -1.22738228715176, -1.09616802703341, -0.97417778024851, -0.947342883651247, -0.872590526711992, -0.748055453556246, -0.587101626169829, -0.431949706204048, -0.0205535428298588, -0.133487413470313, 0.303710419893004, 0.25253554082085, 0.292741492778642, 0.501749164135931, 0.522123317960335, 0.314440154144137, 0.510356748635304, 0.278951809960741, 0.28554973797987, 0.217676532329101, 0.173692359654957, 0.103546523722473, 0.134818969976803, -0.00253783743004714, -0.187012679950886, -0.42997266251972, -0.541283467284759, -0.44364094014713, -0.566716347775996, -0.487976302338131, -0.563054861831321, -0.198118904472875, -0.252386120518527, -0.187097140764065, 0.204957386408065, 0.127590617667925, 0.40140598867144, 0.762233141317838, 0.69677855456798, 0.865536873327358, 0.793502696392672, 0.906114352453717, 1.22042085528092, 1.30352428848269, 1.08898596599883, 1.52780733389772, 0.962181195523059, 0.979782843320623, 1.18889384225922, 1.1637696969842, 1.20578692366885, 1.10571519513774, 0.72479916607504, 0.717087287426338, 0.456250193934409, -0.145102710490288, -0.195119005150977, -0.21854825878964, -0.653357329389725, -0.523181432020582, -0.603632563687, -1.42005600534092, -1.40689872820929, -1.43806140287734, -1.04066690403268, -0.717935210170339, -0.706582627841045, -0.461225892107602, -0.151929968050662, -0.325534010180375, -0.518544021623121, -0.594877463476131, -0.82655976149186, -0.795720389207105, -0.539128672175106, -0.827513487035207, -0.654070522458878, -0.753103291515414, -0.901990497021026, -1.0787981761802, -0.999764885210968, -1.06413058477954, -0.79065105705157, -0.560760840443467, -0.554702476786947, -0.0381279960513528, 0.027867995797351, 0.008286902735628, 0.303682567330605, 0.167647425736424, 0.478795570820515, 0.790726432472928, 0.681821578169263, 0.56908309319664, 0.701755329193296, 0.566315128182265, 0.689466883979508), .Tsp = c(1995, 2004.66666666667, 12), class = "ts")

fct_indic <- c(0.56394586923066, 0.353214726696848, 0.447859518011765, 0.411940949856209, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)

midas.fit <- midas_u(target.aligned ~ mls(indic.aligned, param:((px + 2) * frq - 1), frq) + mls(target.aligned, 1:py, 1)) fct <- forecast(midas.fit, newdata = list(indic.aligned = fct_indic), method = "static")$mean fct `

p-wegmueller commented 4 years ago

I solved it. The answer is quite simple: use pencil and paper first, then code...

` param_constr <- nealmon h <- 2 # Forecast horizon frq <- 3 # Frequency ratio py <- 1 # lags of dependent variable px <- 0 # lags of independent variable

midas.fit <- midas_r(target.aligned ~ mls(target.aligned, h:(py+h), 1) + mls(indic.aligned, frqh + 0:(px+frqh), frq, param_constr), start = list(indic.aligned = c(1, 0.5))) fct <- midasr::forecast(midas.fit, newdata = list(indic.aligned = fct_indic), method = "static")$mean fct `