在 R 中实现更精细的时间序列预测
Achieving More Granular Timeseries Predictions in R
我有一个 R 脚本可以生成如下图:
如何实现更精细的预测,例如这个例子 (1):
我的可重现代码如下:
d <- structure(list(Date = structure(c(17349, 17350, 17351, 17352,
17353, 17354, 17355, 17356, 17357, 17358, 17359, 17360, 17361,
17362, 17363, 17364, 17365, 17366, 17367, 17368, 17369, 17370,
17371, 17372, 17373, 17374, 17375, 17376, 17377, 17378, 17379,
17380, 17381, 17382, 17383), class = "Date"), Ratio = c(67, 50,
67, 50, 100, 50, 33, 67, 0, 0, 0, 0, 100, 75, 0, 0, 75, 100,
67, 33, 33, 33, 50, 50, 67, 100, 67, 50, 25, 25, 33, 33, 100,
33, 0)), .Names = c("Date", "Ratio"), row.names = 183:217, class = "data.frame")
library(xts)
dates = as.Date(d$Date,"%Y-%m-%d")
xs = xts(d$Ratio,dates)
library("forecast")
train.ts <- window(xs, start = as.Date("2017-07-01"), end = as.Date("2017-08-01"))
val.ts <- window(xs, start = as.Date("2017-08-02"), end = as.Date("2017-08-04"))
d.lm <- tslm(as.ts(train.ts) ~ trend + I(trend^2))
d.lm.pred <- forecast(d.lm, h = 2, level = 0)
plot(d.lm.pred, ylab = "Ratio", xlab = "Days", bty = "l", xaxt = "n", main = "", flty = 2)
lines(d.lm$fitted.values, lwd = 2)
lines(val.ts)
我曾尝试更改预测 window 以缩短季节,但预测过于平滑,没有遵循数据的 "spiking" 模式。
我的相关会话信息是:
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
other attached packages:
[1] forecast_8.1 xts_0.10-0 zoo_1.8-0
参考。 1: https://robjhyndman.com/hyndsight/forecasting-weekly-data/
编辑: 当我展开 window 并使用黄土时,我注意到一个非常波浪形的图案:
但是,当我尝试预测波浪趋势时,我没有看到高点和低点,而是得到了下降的预测:
y <- as.ts(train.ts)
x <- 1:length(y)
fit <- loess(y~x, span=0.15)
yhat <- predict(fit)
plot(x, y, ylab = "Ratio", xlab = "Days", type = "l", xaxt = "n", main = "")
lines(x, yhat, lwd = 2)
d.lm.pred <- forecast(yhat, h = 20, level = 0)
输出如下:
Point Forecast Lo 0 Hi 0
159 27.1699724 27.1699724 27.1699724
160 22.9336754 22.9336754 22.9336754
161 19.2979054 19.2979054 19.2979054
162 16.1775332 16.1775332 16.1775332
163 13.4994973 13.4994973 13.4994973
164 11.2010931 11.2010931 11.2010931
165 9.2285050 9.2285050 9.2285050
166 7.5355461 7.5355461 7.5355461
167 6.0825770 6.0825770 6.0825770
168 4.8355771 4.8355771 4.8355771
169 3.7653488 3.7653488 3.7653488
170 2.8468335 2.8468335 2.8468335
171 2.0585246 2.0585246 2.0585246
172 1.3819645 1.3819645 1.3819645
173 0.8013118 0.8013118 0.8013118
174 0.3029711 0.3029711 0.3029711
175 -0.1247262 -0.1247262 -0.1247262
176 -0.4917941 -0.4917941 -0.4917941
177 -0.8068273 -0.8068273 -0.8068273
178 -1.0772023 -1.0772023 -1.0772023
为什么预测不符合该模式?
多种方式...您只使用二阶多项式,所以您会得到一条看起来是二次的曲线。例如,如果您使用三阶多项式:
d.lm <- tslm(as.ts(train.ts) ~ trend + I(trend^2) + I(trend^3))
您得到一条如下所示的曲线:
您可以做的一件简单的事情就是尝试越来越大的多项式,直到它看起来像您想要的那样。但我怀疑你想要局部拟合,比如 LOESS:https://en.wikipedia.org/wiki/Local_regression
示例:
y <- as.ts(train.ts)
x <- 1:length(y)
fit <- loess(y~x, span=0.35)
yhat <- predict(fit)
plot(x, y, ylab = "Ratio", xlab = "Days", type = "l", xaxt = "n", main = "")
lines(x, yhat, lwd = 2)
我有一个 R 脚本可以生成如下图:
如何实现更精细的预测,例如这个例子 (1):
我的可重现代码如下:
d <- structure(list(Date = structure(c(17349, 17350, 17351, 17352,
17353, 17354, 17355, 17356, 17357, 17358, 17359, 17360, 17361,
17362, 17363, 17364, 17365, 17366, 17367, 17368, 17369, 17370,
17371, 17372, 17373, 17374, 17375, 17376, 17377, 17378, 17379,
17380, 17381, 17382, 17383), class = "Date"), Ratio = c(67, 50,
67, 50, 100, 50, 33, 67, 0, 0, 0, 0, 100, 75, 0, 0, 75, 100,
67, 33, 33, 33, 50, 50, 67, 100, 67, 50, 25, 25, 33, 33, 100,
33, 0)), .Names = c("Date", "Ratio"), row.names = 183:217, class = "data.frame")
library(xts)
dates = as.Date(d$Date,"%Y-%m-%d")
xs = xts(d$Ratio,dates)
library("forecast")
train.ts <- window(xs, start = as.Date("2017-07-01"), end = as.Date("2017-08-01"))
val.ts <- window(xs, start = as.Date("2017-08-02"), end = as.Date("2017-08-04"))
d.lm <- tslm(as.ts(train.ts) ~ trend + I(trend^2))
d.lm.pred <- forecast(d.lm, h = 2, level = 0)
plot(d.lm.pred, ylab = "Ratio", xlab = "Days", bty = "l", xaxt = "n", main = "", flty = 2)
lines(d.lm$fitted.values, lwd = 2)
lines(val.ts)
我曾尝试更改预测 window 以缩短季节,但预测过于平滑,没有遵循数据的 "spiking" 模式。
我的相关会话信息是:
R version 3.4.1 (2017-06-30)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
other attached packages:
[1] forecast_8.1 xts_0.10-0 zoo_1.8-0
参考。 1: https://robjhyndman.com/hyndsight/forecasting-weekly-data/
编辑: 当我展开 window 并使用黄土时,我注意到一个非常波浪形的图案:
但是,当我尝试预测波浪趋势时,我没有看到高点和低点,而是得到了下降的预测:
y <- as.ts(train.ts)
x <- 1:length(y)
fit <- loess(y~x, span=0.15)
yhat <- predict(fit)
plot(x, y, ylab = "Ratio", xlab = "Days", type = "l", xaxt = "n", main = "")
lines(x, yhat, lwd = 2)
d.lm.pred <- forecast(yhat, h = 20, level = 0)
输出如下:
Point Forecast Lo 0 Hi 0
159 27.1699724 27.1699724 27.1699724
160 22.9336754 22.9336754 22.9336754
161 19.2979054 19.2979054 19.2979054
162 16.1775332 16.1775332 16.1775332
163 13.4994973 13.4994973 13.4994973
164 11.2010931 11.2010931 11.2010931
165 9.2285050 9.2285050 9.2285050
166 7.5355461 7.5355461 7.5355461
167 6.0825770 6.0825770 6.0825770
168 4.8355771 4.8355771 4.8355771
169 3.7653488 3.7653488 3.7653488
170 2.8468335 2.8468335 2.8468335
171 2.0585246 2.0585246 2.0585246
172 1.3819645 1.3819645 1.3819645
173 0.8013118 0.8013118 0.8013118
174 0.3029711 0.3029711 0.3029711
175 -0.1247262 -0.1247262 -0.1247262
176 -0.4917941 -0.4917941 -0.4917941
177 -0.8068273 -0.8068273 -0.8068273
178 -1.0772023 -1.0772023 -1.0772023
为什么预测不符合该模式?
多种方式...您只使用二阶多项式,所以您会得到一条看起来是二次的曲线。例如,如果您使用三阶多项式:
d.lm <- tslm(as.ts(train.ts) ~ trend + I(trend^2) + I(trend^3))
您得到一条如下所示的曲线:
您可以做的一件简单的事情就是尝试越来越大的多项式,直到它看起来像您想要的那样。但我怀疑你想要局部拟合,比如 LOESS:https://en.wikipedia.org/wiki/Local_regression
示例:
y <- as.ts(train.ts)
x <- 1:length(y)
fit <- loess(y~x, span=0.35)
yhat <- predict(fit)
plot(x, y, ylab = "Ratio", xlab = "Days", type = "l", xaxt = "n", main = "")
lines(x, yhat, lwd = 2)