How do you forecast future values using support vector regression in R

1.9k views Asked by At

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

enter image description here

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?

1

There are 1 answers

3
Econ_matrix On BEST ANSWER

You can use caretForecast package. You can use any ML model which supported by caret including SVM.

to install the package: install.packages("caretForecast", dependencies = TRUE) or dev version devtools::install_github("Akai01/caretForecast")

Example code

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)

enter image description here

ref : https://github.com/Akai01/caretForecast