I am trying to forecast for future values of a periodic position dependent on time (x ~ time), univariate forecasting using support vector regression. The model fits well on train data but then trails into a straight line when evaluated on test data. In the code below, I used 50 observations for train (the first half of the red periodic curve, where SVR fits perfectly) and 50 observations for test (the second half of the red curve, where SVR fails to predict).
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
I looked at this post: Time Series Forecasting using Support Vector Machine (SVM) in R and tried out the suggestion of combining both train and test data together and evaluating the model on it.
Would like to know what is going on here. My hunch is that the choice of kernel is unable to generalize to periodic patterns in the future. How would I construct a kernel such that the SVR model is able to predict for periodic data in the future?
You can use
caretForecast
package. You can use any ML model which supported bycaret
including SVM.to install the package:
install.packages("caretForecast", dependencies = TRUE)
or dev versiondevtools::install_github("Akai01/caretForecast")
Example code
ref : https://github.com/Akai01/caretForecast