使用 xts 对象进行 ARIMA 预测

ARIMA forecasting with xts object

我有两个xts对象(一个火车和一个test/validation集合),我想使用基于火车数据集的ARIMA模型对测试数据集进行一步预测(即一步出样本预测)。然而,每当我使用“预测”功能时,结果似乎很奇怪。这可能是因为 forecast() 假定了一个时间序列对象,而我有 xts 对象。 我想知道是否有人知道我想要完成的 R 命令?我也想生成一个像上传的图表一样的图表,但由于我相信使用 xts 对象,结果再次变得非常奇怪。 非常感谢您的帮助,因为我真的坚持分析的这一步!:) 我的训练集:

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")))

我的验证集:

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 代码:

    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]

为了能够将 {forecast} 包中的函数应用到您的数据中,您只需要使用 as.ts 将数据转换为时间序列。例如:

# 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

然后,您可以使用该模型预测值:

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

您还可以使用 plot 绘制预测数据:

plot(fit)

您可以按照上述相同的步骤分析测试数据。