"foreden" <- function(m1,rt,orig,h,xre=NULL,fixed=NULL){ # This program perform density evaluation of out-of-sample forecasts. # At this moment, only 1-step ahead forecast is allowed. # # m1: is a model object # orig: is the forecast origin # rt: the time series # xre: the independent variables # h: forecast horizon # fixed: parameter constraint # # The program requires the library fSeries or FinTS. # # regor=c(m1$arma[1],m1$arma[6],m1$arma[2]) seaor=list(order=c(m1$arma[3],m1$arma[7],m1$arma[4]),perioa=m1$arma[5]) T=length(rt) if(orig > T)orig=T if(h < 1) h=1 rmse=rep(0,h) nori=T-orig err=matrix(0,nori,h) u=rep(0,nori) jlast=T-1 for (n in orig:jlast){ jcnt=n-orig+1 x=rt[1:n] mm=arima(x,order=regor,seasonal=seaor,xreg=xre,fixed=fixed) se=sqrt(mm$sigma2) if (is.null(xre)) nx=NULL else nx=xre[(n+1):(n=h)] fore=predict(mm,h,newxreg=nx) pred=fore$pred obsd=rt[(n+1):(n+h)] err[jcnt,]=obsd-pred u[jcnt]=pnorm(err[jcnt,1],0,se) } for (i in 1:h){ iend=nori-i+1 tmp=err[1:iend,i] rmse[i]=sqrt(sum(tmp^2)/iend) } print("RMSE of out-of-sample forecasts") print(rmse) backtest<-list(origin=orig,error=err,rmse=rmse,trans=u) }