1

我正在尝试预测依赖于时间(x ~ time)的周期性位置的未来值,使用支持向量回归进行单变量预测。该模型非常适合训练数据,但在对测试数据进行评估时会变成一条直线。在下面的代码中,我使用了 50 个观察值进行训练(红色周期曲线的前半部分,SVR 非常适合)和 50 个观察值进行测试(红色曲线的后半部分,SVR 无法预测)。

library(lubridate)
library(purrr)
library(ggplot2)
library(Metrics)
library(caret)
library(dplyr)
library(e1071)

# train_data has 50 observations
# eval_data has 100 observations (the first half is train_data)
 
func <- x ~ abs_time # position x dependent on time
svr_model <- svm(func, train_data, type = "eps-regression", 
                 kernel="radial", gamma=13, cost=10, epsilon = 0.01)

k_hat <- predict(svr_model, eval_data)
  
plot(x = eval_data$abs_time, y = eval_data$x, type = "l", col="red") # true position
points(x = eval_data$abs_time, y = k_hat, col = "blue") # SVR predicted position

在此处输入图像描述

我查看了这篇文章: Time Series Forecasting using Support Vector Machine (SVM) in R并尝试了将训练数据和测试数据结合在一起并在其上评估模型的建议。

想知道这里发生了什么。我的预感是内核的选择在未来无法推广到周期性模式。我将如何构建一个内核,以便 SVR 模型能够预测未来的周期性数据?

4

1 回答 1

4

你可以使用caretForecast包。您可以使用caret包含 SVM 支持的任何 ML 模型。

安装包:install.packages("caretForecast", dependencies = TRUE)或开发版本 devtools::install_github("Akai01/caretForecast")

示例代码

library(caretForecast)
library(forecast)

training_data <- window(AirPassengers, end = c(1960, 1))

testing_data <- window(AirPassengers, start = c(1960, 2))


fit <- ARml(training_data, maxlag = 12, caret_method = "svmLinear",
            lambda = "auto")

forecast(fit, h = length(testing_data), level = NULL)-> fc

accuracy(fc, testing_data)


fc_plot(fc) + 
  autolayer(testing_data, series = "testing_data")

get_var_imp(fc)

get_var_imp(fc, plot = F)

在此处输入图像描述

参考:https ://github.com/Akai01/caretForecast

于 2020-10-07T08:00:38.200 回答