"backtest" <- function(m1,rt,orig,h,xre=NULL,fixed=NULL){ # m1: is a model object # orig: is the forecast origin # rt: the time series # xre: the independent variables # h: forecast horizon # fixed: parameter constriant # # 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) 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) 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 } 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) }