我正在 forecasting 使用 auto.Arima 使用单变量数据,但我的预测不正确。我已经正确使用了所有步骤,但点预测 value 并不正确。请帮我。
这是我的数据:
s <- read.csv(url('https://ondemand.websol.barchart.com/getHistory.csv?apikey=c3122f072488a29c5279680b9a2cf88e&symbol=zs*1&type=dailyNearest&backAdjust=false&startDate=20100201'))
这是我的代码:
data <- s[c(3, 7)]
summary(data)
data1.ts <- zoo(data[,2], seq(from = as.Date("2010-02-01"), to = as.Date("2022-05-13"), by = 1))
autoplot(data1.ts)
Arima 型号:
fit_arima <- auto.arima(data1.ts, stepwise = FALSE, approximation = FALSE, trace = TRUE)
print(summary(fit_arima))
checkresiduals(fit_arima)
forecast_Arima <- forecast(fit_arima, h = 1)
forecast_Arima
预测 Value:
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
19126 976.4357 949.874 1002.997 935.813 1017.058
小更新:
我试图将数据加载为 ts 对象并获得准确的点预测 value 但是,我的预测年份不正确。领先一步的 forecasting 给了我 2021 年的 value,但我的结束 date 是 2022-05-13。我只想更正年份。这是新代码:
ts_soy <- ts(data[,2], start = c(2010-02-01), frequency = 214)
autoplot(ts_soy)
fit_arima <- auto.arima(ts_soy)
print(summary(fit_arima))
checkresiduals(fit_arima)
forecast_Arima <- forecast(fit_arima, h = 1)
forecast_Arima
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2021.472 1646.5 1625.071 1667.929 1613.727 1679.273
回答1
我可以重现您的问题,原因是您的 data1.ts
包含太多数据。您正试图摆脱周末以创建一个连续的时间序列(也就是没有间隙的时间序列)。原则是正确的,但是您的记录数量超过了 1388 条记录。由于 R 倾向于回收 values,因此您会再次获得早年的收盘价,这会影响 arima
函数。
您可以执行诸如从最早的 date 开始创建时间序列之类的操作,并且此 date + 记录数 - 1
data.ts <- zoo(data[,2], seq(from = as.Date("2010-02-01"),
to = as.Date("2010-02-01") + 3096,
by = 1))
fit_ar <- forecast::auto.arima(data.ts, stepwise = FALSE, approximation = FALSE)
forecast::forecast(fit_ar, h = 1)
Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
17738 1648.129 1626.759 1669.499 1615.446 1680.812
这也是我喜欢使用fable的原因之一,我可以更好地检查数据。
library(fpp3)
library(fable.prophet)
fit <- data %>%
mutate(id = row_number()) %>% # create index to use otherwise timeseries has gaps
tsibble(index = id) %>%
model(naive = NAIVE(close),
arima = ARIMA(close, stepwise = FALSE, approximation = FALSE),
)
forecast(fit, h = 1)
# A fable: 2 x 4 [1]
# Key: .model [2]
.model id close .mean
<chr> <dbl> <dist> <dbl>
1 naive 3098 N(1646, 280) 1646.
2 arima 3098 N(1649, 278) 1649.
# prophet needs dates and can handle weekends
prophet_fit <- data %>%
mutate(tradingDay = ymd(tradingDay)) %>%
tsibble() %>%
model(prophet_model = prophet(close))
forecast(prophet_fit, h = 1)
# A fable: 1 x 4 [1D]
# Key: .model [1]
.model tradingDay close .mean
<chr> <date> <dist> <dbl>
1 prophet_model 2022-05-14 sample[5000] 1657.