[R-SIG-Finance] rugarch xts causes error

alexios galanos @lexio@ @ending from 4d@c@pe@com
Mon Dec 3 15:10:40 CET 2018


The external.regressors input must be a matrix not an xts object e.g. use:
spec<-ugarchspec(mean.model=list(external.regressors=coredata(inputs[1:2000,2])))

This will probably be relaxed in a future release.

Regards,

Alexios

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



More information about the R-SIG-Finance mailing list