Search code examples
rforecastingxtsarima

ARIMA forecasting with xts object


I have two xts objects (one train and one test/validation set) and I would like to use ARIMA models based on the train data set to carry out one-step-ahead forecast on the test dataset (namely, one-step out of sample forecasting). However, whenever I use the "forecast" function, the results seem weird. It's probably because forecast() assumes a time series object and I have xts objects. I was wondering if anyone knows an R command for what I want to accomplish? I would also like to produce a graph like the uploaded one, but once again it turns out very weird due to using xts objects I believe. I would really appreciate your help as I'm really stuck on this step of my analysis!:) My training set:

dput(head(xts.data)) structure(c(2.74983173511717, 2.75110969056266, 2.79971738962803, 2.81540871942271, 2.93438864331294, 3.01504458458636, NA, 0.00127795544549159, 0.0486076990653772, 0.0156913297946755, 0.11897992389023, 0.0806559412734247 ), class = c("xts", "zoo"), index = structure(c(1333324800, 1333411200, 1333497600, 1333584000, 1333929600, 1334016000), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 2L), .Dimnames = list(NULL, c("lVIXCLS", "ldVIXCLS")))

My validation set:

dput(head(validation)) structure(c(3.2846635654062, 3.31890213893533, 3.33077491736561, 3.38371206732114, 3.33434507467431, 3.27184770963431), class = c("xts", "zoo"), index = structure(c(1601510400, 1601596800, 1601856000, 1601942400, 1602028800, 1602115200), tzone = "UTC", tclass = "Date"), .Dim = c(6L, 1L), .Dimnames = list(NULL, "lVIXCLS"))

This is my training set (modelling lVIXCLS with ARIMA)

This is my validation dataset Graph I would like to produce

R-code:

    data<- read_excel("VIXCLS 10 year data.xls")
    data<-na.omit(data)
    date <- as.Date(data$Date, "%m/%d/%Y")
    ts.data<-data
    ts.data$Date<-as.Date(ts.data$Date, format="%m/%d/%Y")
    xts.data2 <- xts(ts.data[2],ts.data$Date)
    ts.data$Date<-as.Date(ts.data$Date, format="%m/%d/%Y")
    xts.data2 <- xts(ts.data[2],ts.data$Date)
    xts.data<-xts.data2$lVIXCLS[0:2139]
    validation<-xts.data2$lVIXCLS[2140:2517]

Solution

  • To be able to apply the functions in {forecast} package to your data, you just need to convert the data to a time series by using as.ts. For example:

    # Your data
    
    train.dat <- structure(c(2.74983173511717, 2.75110969056266, 2.79971738962803, 
                 2.81540871942271, 2.93438864331294, 3.01504458458636, NA, 
                 0.00127795544549159, 0.0486076990653772, 0.0156913297946755, 
                 0.11897992389023, 0.0806559412734247 ), 
                 class = c("xts", "zoo"), 
                 index = structure(c(1333324800, 1333411200, 1333497600, 1333584000, 
                         1333929600, 1334016000), tzone = "UTC", tclass = "Date"), 
                 .Dim = c(6L, 2L), .Dimnames = list(NULL, c("lVIXCLS", "ldVIXCLS")))
    
    library(forecast)
    
    fit <- auto.arima(as.ts(train.dat[,1]))
    fit
    # Series: as.ts(train.dat[, 1]) 
    # ARIMA(0,1,0) 
    # 
    # sigma^2 = 0.004656:  log likelihood = 6.33
    # AIC=-10.66   AICc=-9.33   BIC=-11.05
    

    Then, you can use the model to forecast values:

    forecast(fit)
    # Point Forecast    Lo 80    Hi 80    Lo 95    Hi 95
    # 7       3.015045 2.927599 3.102491 2.881307 3.148782
    # 8       3.015045 2.891377 3.138712 2.825912 3.204177
    # 9       3.015045 2.863584 3.166506 2.783405 3.246684
    # 10       3.015045 2.840153 3.189937 2.747570 3.282519
    # 11       3.015045 2.819509 3.210580 2.715999 3.314090
    # 12       3.015045 2.800846 3.229243 2.687457 3.342632
    # 13       3.015045 2.783684 3.246405 2.661209 3.368880
    # 14       3.015045 2.767710 3.262379 2.636779 3.393310
    # 15       3.015045 2.752707 3.277383 2.613833 3.416256
    # 16       3.015045 2.738516 3.291573 2.592131 3.437959
    

    You can also plot the forecast data using plot:

    plot(fit)
    

    enter image description here

    You can analyze the test data with the same steps as above.