<div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">Hi</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">Used example provided by Alexios in some thread</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">===========================</div><div xmlns="http://www.w3.org/1999/xhtml"><div>library(rugarch) </div><div>library(xts) </div><div> </div><div>data(sp500ret) </div><div> </div><div>spx<-xts(sp500ret, as.Date(rownames(sp500ret))) </div><div>xreg<-xts(rnorm(nrow(spx)), index(spx)) </div><div>colnames(xreg)<-"xreg" </div><div> </div><div># assume xreg is an actual series, so we lag it </div><div># as we would do in a real application </div><div>xreg = lag(xreg,1) </div><div> </div><div>inputs<-na.omit(cbind(spx, xreg, join="left")) </div><div> </div><div># real time forecasting </div><div>spec<-ugarchspec(mean.model=list(external.regressors=inputs[1:2000,2])) </div><div>fit<-ugarchfit(spec, inputs[1:2000,1]) </div><div> </div><div># 2 ways to do real-time forecasting (ugarchforecast and ugarchfilter) </div><div># Example: forecast points 2001:2020 </div><div>xforc = xts(matrix(NA, ncol=2, nrow=20), index(inputs[2001:2020])) </div><div>sforc = xts(matrix(NA, ncol=2, nrow=20), index(inputs[2001:2020])) </div><div>for(i in 1:20){ </div><div>  # Forecast(T+1)|Information(T) </div><div> </div><div>  # 1. Create a similar spec as you used in estimation </div><div>  # and add the lagged regressor upto time T </div><div>  specf1<-ugarchspec(mean.model=list(external.regressors=inputs[1:(2000+i-1),2])) </div><div>  # Pass the estimated coefficients from the estimation upto time 2000 </div><div>  setfixed(specf1)<-as.list(coef(fit)) </div><div> </div><div>  # 2. Forecast using ugarchforecast on a specification with fixed parameters </div><div>  # where n.old is used in order to recreate the correct start-up conditions </div><div>  # used in the fitting routine </div><div>  f1<-ugarchforecast(specf1, inputs[1:(2000+i-1),1], n.ahead=1, n.old=2000) </div><div>  </div><div>  # 3. Forecast using ugarchfilter on a specification with fixed parameters. </div><div>  # For this method, append a new row to the end of the data with zeros, </div><div>  # as you would do with related filters. This forces the routine to </div><div>  # output the value at time T+1 </div><div>  newdat<-rbind(inputs[1:(2000+i-1),],xts(matrix(0, nrow=1, ncol=2), </div><div>                                          tail(move(index(inputs[1:(2000+i-1)])),1))) </div><div>  specf2<-ugarchspec(mean.model=list(external.regressors=newdat[,2])) </div><div>  setfixed(specf2)<-as.list(coef(fit)) </div><div>  f2<-ugarchfilter(specf2, newdat[,1], n.old=2000) </div><div>  </div><div>  # fitted = estimated conditional mean values for uGARCHfit objects </div><div>  # fitted = forecast/filtered conditional mean values for uGARCHforecast/uGARCHfilter objects </div><div>  xforc[i,1] = as.numeric(fitted(f1)) </div><div>  xforc[i,2] = as.numeric(tail(fitted(f2),1)) </div><div>  # sigma = conditional sigma values (fitted/forecast etc) </div><div>  sforc[i,1] = as.numeric(sigma(f1)) </div><div>  sforc[i,2] = as.numeric(tail(sigma(f2),1)) </div><div>} </div><div># check </div><div>all.equal(xforc[,1], xforc[,2]) </div><div>all.equal(sforc[,1], sforc[,2]) </div><div># check that the 1-ahead forecast directly from the fitted object is also </div><div># the same </div><div>all.equal(as.numeric(xforc[1,1]), as.numeric(fitted(ugarchforecast(fit, </div><div>                                                                   n.ahead=1)))) </div><div>all.equal(as.numeric(sforc[1,1]), as.numeric(sigma(ugarchforecast(fit, </div><div>                                                                  n.ahead=1)))) </div><div># check the filter values vs the fitted values (i.e. why we use the n.old argument) </div><div>all.equal(fitted(fit), fitted(f2)[1:2000]) </div><div>all.equal(sigma(fit), sigma(f2)[1:2000])</div></div><div xmlns="http://www.w3.org/1999/xhtml">==========================</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml">But, when running it i get an error:</div><div xmlns="http://www.w3.org/1999/xhtml"> </div><div xmlns="http://www.w3.org/1999/xhtml"><div>> fit<-ugarchfit(spec, inputs[1:2000,1])</div><div>Error in pars[idx["mxreg", 1]:idx["mxreg", 2], 1] <- fit.mean[i] : </div><div>  replacement has length zero</div></div>