2
$\begingroup$

I have the code below which trains a model with some predictors, forecasts it one step, appends the forecasted value on the original training data and then tries to feed that back in and train and forecast another model ahead one step. The first time it trains and forecasts the model works fine, but the "ModelCast2" step below where it tries to train and forecast a 2nd time throws the warning below and gives a one step forecast of "NA". I don't really see the difference between the first forecast process and the 2nd. If anyone can pleases point out what's causing the error and the NA value, I'd be grateful.

Warning:
Warning message:
In forecast.Arima(FV_Arima.fit, xreg = FV_xregVal1, h = ForecastLength) :
  Upper prediction intervals are not finite.


Code:
Model_Forecast<-function(DataDF,ForecastVariable,Predictors=NULL,ForecastLength,RF_Num)
{
  ##Partitioning Time Series
  FV_EndTrain1<-(length(DataDF[,ForecastVariable])-(RF_Num)*ForecastLength) 
  FV_ValStart1<-FV_EndTrain1+1
  FV_ValEnd1<-FV_ValStart1+(RF_Num)*ForecastLength-1 ##FV_ValStart1+(RF_Num+1)*ForecastLength-1

  FV_tsTrain1 <-DataDF[,ForecastVariable][1:FV_EndTrain1] #RF_Num110RF_Num
  FV_tsValidation<-DataDF[,ForecastVariable][FV_ValStart1:FV_ValEnd1]


  ##BoxCox

  lambda1 <- BoxCox.lambda(FV_tsTrain1)

  if ( is.null(Predictors) ) 
  { 
    ##Predictors
    FV_xreg1<-NULL
    FV_xregTrain1<-NULL
    FV_xregVal1<-NULL

    ##Train Model
    FV_Arima.fit <- auto.arima(FV_tsTrain1, lambda = lambda1,stepwise = FALSE,approximation = FALSE)

    ##Forecast Model

    FV_Acast<-forecast(FV_Arima.fit, h=ForecastLength) 


  }
  else {
    ##Predictors
    FV_xreg1<-DataDF[,names(DataDF)!=ForecastVariable] 
    FV_xregTrain1<-FV_xreg1[1:FV_EndTrain1,Predictors]
    FV_xregVal1<-FV_xreg1[FV_ValStart1:(FV_ValStart1+ForecastLength-1),Predictors] 


    ## With Predictors

    FV_Arima.fit <- auto.arima(FV_tsTrain1, lambda = lambda1, xreg=FV_xregTrain1,stepwise = FALSE,approximation = FALSE)

    ##Forecast Model

    FV_Acast<-forecast(FV_Arima.fit,xreg=FV_xregVal1, h=ForecastLength) 
  }

  MF_ReturnList<-list("FV_Model"=FV_Arima.fit,
                      "FV_Forescast"=FV_Acast$mean,
                      "FV_Validation"=FV_tsValidation, 
                      "FV_tsTrain1"=FV_tsTrain1,
                      "FV_xregTrain1"=FV_xregTrain1,
                      "FV_xregVal1"=FV_xregVal1,
                      "FV_xreg1"=FV_xreg1,
                      "Predictors"=Predictors,
                      "FV_Train"=DataDF[,ForecastVariable]) 

  MF_ReturnList
}


DataDF=SampleData
ForecastVariable="Field1"
Predictors=c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
ForecastLength=1
RF_Num=24

nDataDF<-DataDF
FVar<-ForecastVariable
Pred<-Predictors
FL<-ForecastLength
RNum<-RF_Num

## Forecast Model one step 1st time
ModelCast<-Model_Forecast(DataDF=nDataDF,ForecastVariable=FVar,Predictors=Pred,ForecastLength=FL,RF_Num=RNum)

## Append 1st forecast to end of 1st training set
Train2<-append(ModelCast$FV_tsTrain1,ModelCast$FV_Forescast)

## Creating data.frame for second forecast
DataDf2<-cbind(Train2,nDataDF[1:length(Train2),Pred])
names(DataDf2)[names(DataDf2)=="Train2"] <- FVar


## Second Model forecast
ModelCast2<-Model_Forecast(DataDF=DataDf2,ForecastVariable=FVar,Predictors=ModelCast$Predictors,ForecastLength=FL,RF_Num=0) #FV_Forescast is NA


Sample Data:

dput(droplevels(SampleData))
structure(list(Field1 = c(48, 46, 45, 39, 31, 22, 21, 22, 23, 
29, 33, 45, 52, 63, 74, 78, 82, 84, 75, 81, 85, 82, 75, 68, 57, 
48, 37, 27, 28, 28, 29, 27, 29, 33, 42, 52, 69, 68, 81, 85, 91, 
95, 100, 101, 97, 88, 90, 79, 73, 63), Monday = c(0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0), Tuesday = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), Wednesday = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0), Thursday = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
Friday = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0), Saturday = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1)), .Names = c("Field1", "Monday", 
"Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"), row.names = c(NA, 
50L), class = "data.frame")
$\endgroup$
3
  • $\begingroup$ Hello @user3476463, I got the same issue, and it's really annoying. Have you found any solutions to this problem? Please let me know. $\endgroup$ – Übel Yildmar May 17 '17 at 20:04
  • $\begingroup$ @ÜbelYildmar If I remember right, I think the trick is in ModelCast2 you have to set RF_NUM=1. $\endgroup$ – user3476463 May 18 '17 at 2:43
  • $\begingroup$ My time-series data was not the same length. Not it's working. Thanks. $\endgroup$ – Übel Yildmar May 20 '17 at 7:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.