我有一个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)
我尝试改变预测窗口以缩短季节,但预测过于平滑且不遵循数据的“尖峰”模式。
我的相关会话信息是:
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/
编辑:当我展开窗户并使用黄土时,我注意到一种非常波浪状的图案:
然而,当我试图预测波浪趋势时,我没有看到高点和低点,而是下降预测:
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
为什么预测不遵循这种模式?
答案 0 :(得分:1)
多种方式......您只使用二阶多项式,因此您将得到一条看似二次的曲线。例如,如果使用三阶多项式:
import java.util.Scanner;
public class JavaProgram {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the base you want to convert and base to be converted: ");
int firstBase = in.nextInt();
int secondBase = in.nextInt();
switch(firstBase) {
case 2:
System.out.print("Enter value: ");
// I get confused here already
case 3:
case 4:
case 5:
case 8:
case 10:
case 12:
case 16:
case 20:
case 24:
case 26:
case 27:
case 30:
case 32:
case 36:
}
in.close();
}
}
你得到一条如下曲线:
你可以做的一件简单的事情就是尝试越来越大的多项式,直到看起来如你所愿。但我怀疑你想要本地拟合,比如LOESS:https://en.wikipedia.org/wiki/Local_regression
示例:
d.lm <- tslm(as.ts(train.ts) ~ trend + I(trend^2) + I(trend^3))