<div xmlns="http://www.w3.org/1999/xhtml">Thanks a lot -- that works for me</div><div><br /></div><div><br /></div><div>03.12.2018, 17:10, "alexios galanos" <alexios@4dscape.com>:</div><blockquote type="cite"><p>The external.regressors input must be a matrix not an xts object e.g. use:<br />spec<-ugarchspec(mean.model=list(external.regressors=coredata(inputs[1:2000,2])))<br /><br />This will probably be relaxed in a future release.<br /><br />Regards,<br /><br />Alexios<br /><br />On 12/3/18 4:45 AM, Владимир Иванов wrote:<br /></p><blockquote> Hi<br /> Used example provided by Alexios in some thread<br /> ===========================<br /> library(rugarch)<br /> library(xts)<br /> data(sp500ret)<br /> spx<-xts(sp500ret, as.Date(rownames(sp500ret)))<br /> xreg<-xts(rnorm(nrow(spx)), index(spx))<br /> colnames(xreg)<-"xreg"<br /> # assume xreg is an actual series, so we lag it<br /> # as we would do in a real application<br /> xreg = lag(xreg,1)<br /> inputs<-na.omit(cbind(spx, xreg, join="left"))<br /> # real time forecasting<br /> spec<-ugarchspec(mean.model=list(external.regressors=inputs[1:2000,2]))<br /> fit<-ugarchfit(spec, inputs[1:2000,1])<br /> # 2 ways to do real-time forecasting (ugarchforecast and ugarchfilter)<br /> # Example: forecast points 2001:2020<br /> xforc = xts(matrix(NA, ncol=2, nrow=20), index(inputs[2001:2020]))<br /> sforc = xts(matrix(NA, ncol=2, nrow=20), index(inputs[2001:2020]))<br /> for(i in 1:20){<br />    # Forecast(T+1)|Information(T)<br />    # 1. Create a similar spec as you used in estimation<br />    # and add the lagged regressor upto time T<br /><br /> specf1<-ugarchspec(mean.model=list(external.regressors=inputs[1:(2000+i-1),2])) <br /><br />    # Pass the estimated coefficients from the estimation upto time 2000<br />    setfixed(specf1)<-as.list(coef(fit))<br />    # 2. Forecast using ugarchforecast on a specification with fixed <br /> parameters<br />    # where n.old is used in order to recreate the correct start-up <br /> conditions<br />    # used in the fitting routine<br />    f1<-ugarchforecast(specf1, inputs[1:(2000+i-1),1], n.ahead=1, <br /> n.old=2000)<br />    # 3. Forecast using ugarchfilter on a specification with fixed <br /> parameters.<br />    # For this method, append a new row to the end of the data with zeros,<br />    # as you would do with related filters. This forces the routine to<br />    # output the value at time T+1<br />    newdat<-rbind(inputs[1:(2000+i-1),],xts(matrix(0, nrow=1, ncol=2),<br /><br /> tail(move(index(inputs[1:(2000+i-1)])),1)))<br />    specf2<-ugarchspec(mean.model=list(external.regressors=newdat[,2]))<br />    setfixed(specf2)<-as.list(coef(fit))<br />    f2<-ugarchfilter(specf2, newdat[,1], n.old=2000)<br />    # fitted = estimated conditional mean values for uGARCHfit objects<br />    # fitted = forecast/filtered conditional mean values for <br /> uGARCHforecast/uGARCHfilter objects<br />    xforc[i,1] = as.numeric(fitted(f1))<br />    xforc[i,2] = as.numeric(tail(fitted(f2),1))<br />    # sigma = conditional sigma values (fitted/forecast etc)<br />    sforc[i,1] = as.numeric(sigma(f1))<br />    sforc[i,2] = as.numeric(tail(sigma(f2),1))<br /> }<br /> # check<br /> all.equal(xforc[,1], xforc[,2])<br /> all.equal(sforc[,1], sforc[,2])<br /> # check that the 1-ahead forecast directly from the fitted object is also<br /> # the same<br /> all.equal(as.numeric(xforc[1,1]), as.numeric(fitted(ugarchforecast(fit,<br /><br />   n.ahead=1))))<br /> all.equal(as.numeric(sforc[1,1]), as.numeric(sigma(ugarchforecast(fit,<br /><br /> n.ahead=1))))<br /> # check the filter values vs the fitted values (i.e. why we use the <br /> n.old argument)<br /> all.equal(fitted(fit), fitted(f2)[1:2000])<br /> all.equal(sigma(fit), sigma(f2)[1:2000])<br /> ==========================<br /> But, when running it i get an error:<br />  > fit<-ugarchfit(spec, inputs[1:2000,1])<br /> Error in pars[idx["mxreg", 1]:idx["mxreg", 2], 1] <- fit.mean[i] :<br />    replacement has length zero<br /><br /> _______________________________________________<br /> <a href="mailto:R-SIG-Finance@r-project.org">R-SIG-Finance@r-project.org</a> mailing list<br /> <a href="https://stat.ethz.ch/mailman/listinfo/r-sig-finance">https://stat.ethz.ch/mailman/listinfo/r-sig-finance</a><br /> -- Subscriber-posting only. If you want to post, subscribe first.<br /> -- Also note that this is not the r-help list where general R questions should go.<br /><br /></blockquote></blockquote>