| Title: | Error Correction Term Based Support Vector Regression for Time Series |
|---|---|
| Description: | The cointegration based support vector regression model is a combination of error correction model and support vector regression. This hybrid model allows the researcher to make use of the information extracted by the cointegrating vector as an input in the support vector regression mode. Implements the models with multiple kernel functions (linear, polynomial, radial, sigmoid) for time series forecasting. Includes stationarity tests, Johansen cointegration, and comprehensive model evaluation. |
| Authors: | Pankaj Das [aut, cre] (ORCID: <https://orcid.org/0000-0003-1672-2502>) |
| Maintainer: | Pankaj Das <[email protected]> |
| License: | GPL-3 |
| Version: | 0.2.0 |
| Built: | 2026-11-13 13:33:56 UTC |
| Source: | https://github.com/cran/ECTSVR |
This function calculates common performance metrics for regression models including RMSE, MAE, MAPE, and R-squared.
calculate_metrics(actual, predicted)calculate_metrics(actual, predicted)
actual |
A numeric vector of actual values |
predicted |
A numeric vector of predicted values |
A named numeric vector containing:
RMSE |
Root Mean Square Error |
MAE |
Mean Absolute Error |
MAPE |
Mean Absolute Percentage Error |
R2 |
R-squared (coefficient of determination) |
actual <- c(10, 12, 15, 18, 20) predicted <- c(11, 13, 14, 17, 21) metrics <- calculate_metrics(actual, predicted) print(metrics)actual <- c(10, 12, 15, 18, 20) predicted <- c(11, 13, 14, 17, 21) metrics <- calculate_metrics(actual, predicted) print(metrics)
This function compares standard SVR and ECT-SVR models, identifies the best performing models based on different metrics, and creates a comparison table.
evaluate_models(std_results, ect_results, format = "simple", verbose = TRUE)evaluate_models(std_results, ect_results, format = "simple", verbose = TRUE)
std_results |
A list containing results from standard SVR models |
ect_results |
A list containing results from ECT-SVR models |
format |
Output format for the table when using knitr. Options: "simple", "markdown", "latex", "html", or "pipe" (default: "simple") |
verbose |
Logical; print the summary and table (default: TRUE) |
A list containing:
combined_results |
Data frame with combined results from both model types |
best_models |
List of best models by RMSE, MAE, MAPE, and R2 |
comparison_table |
Formatted kable table for display (if knitr available) |
## Not run: # Assuming you have trained models comparison <- evaluate_models(std_results, ect_results) print(comparison$combined_results) print(comparison$best_models$best_rmse) # With markdown format comparison <- evaluate_models(std_results, ect_results, format = "markdown") ## End(Not run)## Not run: # Assuming you have trained models comparison <- evaluate_models(std_results, ect_results) print(comparison$combined_results) print(comparison$best_models$best_rmse) # With markdown format comparison <- evaluate_models(std_results, ect_results, format = "markdown") ## End(Not run)
This function creates visual comparisons between actual values, standard SVR predictions, and ECT-SVR predictions for different kernel types.
plot_comparisons( y_test, std_predictions, ect_predictions, kernels = c("linear", "polynomial", "radial", "sigmoid"), ylab = "Fruit Price" )plot_comparisons( y_test, std_predictions, ect_predictions, kernels = c("linear", "polynomial", "radial", "sigmoid"), ylab = "Fruit Price" )
y_test |
A numeric vector of actual test values |
std_predictions |
A list containing standard SVR predictions for each kernel |
ect_predictions |
A list containing ECT-SVR predictions for each kernel |
kernels |
A character vector of kernel types (default: c("linear", "polynomial", "radial", "sigmoid")) |
ylab |
Label of the y axis (default: "Fruit Price") |
No return value, creates plots in the current graphics device
## Not run: # After training models plot_comparisons( data$y_test, std_results$predictions, ect_results$predictions ) ## End(Not run)## Not run: # After training models plot_comparisons( data$y_test, std_results$predictions, ect_results$predictions ) ## End(Not run)
This function prepares time series data for ECT-SVR analysis by performing Johansen cointegration test, constructing error correction terms, creating lag features, and splitting data into training and test sets.
prepare_ectsvr_data( fruit, oil, train_ratio = 0.8, lags = 2, ect_sample = c("train", "full") )prepare_ectsvr_data( fruit, oil, train_ratio = 0.8, lags = 2, ect_sample = c("train", "full") )
fruit |
A numeric vector of fruit prices |
oil |
A numeric vector of crude oil prices |
train_ratio |
A numeric value between 0 and 1 specifying the proportion of data to use for training (default: 0.8) |
lags |
Number of lags of |
ect_sample |
Sample used to estimate the cointegrating vector:
|
A list containing:
train |
Training dataset as data frame |
test |
Test dataset as data frame |
X_train_std |
Standard features for training (lags only) |
X_test_std |
Standard features for testing (lags only) |
X_train_ect |
ECT-enhanced features for training |
X_test_ect |
ECT-enhanced features for testing |
y_train |
Training target values |
y_test |
Test target values |
ECT |
Error correction term vector |
johansen_test |
Johansen test result object |
beta |
Cointegrating vector |
## Not run: # Generate example data fruit <- rnorm(100, 100, 10) oil <- rnorm(100, 50, 5) # Prepare data data <- prepare_ectsvr_data(fruit, oil) str(data) ## End(Not run)## Not run: # Generate example data fruit <- rnorm(100, 100, 10) oil <- rnorm(100, 50, 5) # Prepare data data <- prepare_ectsvr_data(fruit, oil) str(data) ## End(Not run)
This function performs a complete ECT-SVR analysis including stationarity tests, cointegration tests, model training with multiple kernels, and comprehensive evaluation. It serves as the main entry point for the package.
run_ectsvr_analysis( fruit, oil, train_ratio = 0.8, kernels = c("linear", "polynomial", "radial", "sigmoid"), verbose = TRUE, plot_results = TRUE, lags = 2 )run_ectsvr_analysis( fruit, oil, train_ratio = 0.8, kernels = c("linear", "polynomial", "radial", "sigmoid"), verbose = TRUE, plot_results = TRUE, lags = 2 )
fruit |
A numeric vector of fruit prices |
oil |
A numeric vector of crude oil prices |
train_ratio |
A numeric value between 0 and 1 for training proportion (default: 0.8) |
kernels |
A character vector of kernel types to use. Options are "linear", "polynomial", "radial", and "sigmoid" (default: all four) |
verbose |
A logical value indicating whether to print detailed output (default: TRUE) |
plot_results |
A logical value indicating whether to generate comparison plots (default: TRUE) |
lags |
Number of lags of the fruit series used as features (default: 2) |
A list containing all analysis results:
stationarity |
Results from stationarity tests |
cointegration |
Results from cointegration tests |
data |
Prepared data for modeling |
std_svr |
Standard SVR model results |
ect_svr |
ECT-SVR model results |
evaluation |
Model evaluation and comparison results |
best_model |
Information about the best performing model |
## Not run: # Load your data fruit <- c(100, 102, 101, 105, 108, 107, 110, 112, 111, 115) oil <- c(50, 51, 50.5, 52, 51.5, 53, 52.5, 54, 53.5, 55) # Run complete analysis results <- run_ectsvr_analysis(fruit, oil) # View best model print(results$best_model) # View comparison table print(results$evaluation$comparison_table) ## End(Not run)## Not run: # Load your data fruit <- c(100, 102, 101, 105, 108, 107, 110, 112, 111, 115) oil <- c(50, 51, 50.5, 52, 51.5, 53, 52.5, 54, 53.5, 55) # Run complete analysis results <- run_ectsvr_analysis(fruit, oil) # View best model print(results$best_model) # View comparison table print(results$evaluation$comparison_table) ## End(Not run)
This function performs the Johansen cointegration test on fruit and oil price series and extracts the cointegrating vector and error correction term.
test_cointegration(fruit, oil, verbose = TRUE)test_cointegration(fruit, oil, verbose = TRUE)
fruit |
A numeric vector of fruit prices |
oil |
A numeric vector of crude oil prices |
verbose |
Logical; print the test summary and cointegrating vector (default: TRUE) |
A list containing:
johansen_test |
The Johansen test result object from urca::ca.jo |
beta |
The first cointegrating vector |
ECT |
The error correction term calculated from the cointegrating vector |
## Not run: fruit <- rnorm(100, 100, 10) oil <- rnorm(100, 50, 5) coint <- test_cointegration(fruit, oil, verbose = TRUE) print(coint$beta) plot(coint$ECT, type = "l") ## End(Not run)## Not run: fruit <- rnorm(100, 100, 10) oil <- rnorm(100, 50, 5) coint <- test_cointegration(fruit, oil, verbose = TRUE) print(coint$beta) plot(coint$ECT, type = "l") ## End(Not run)
This function performs various stationarity tests including Jarque-Bera, Augmented Dickey-Fuller (ADF), and Kwiatkowski-Phillips-Schmidt-Shin (KPSS) tests on oil and fruit price series.
test_stationarity(oil, fruit, verbose = TRUE)test_stationarity(oil, fruit, verbose = TRUE)
oil |
A numeric vector of oil prices |
fruit |
A numeric vector of fruit prices |
verbose |
Logical; print the test results (default: TRUE) |
A list containing test results:
jb_oil |
Jarque-Bera test result for oil (from tseries::jarque.bera.test) |
jb_fruit |
Jarque-Bera test result for fruit |
adf_oil |
ADF test result for oil (from tseries::adf.test) |
kpss_oil |
KPSS test result for oil (from tseries::kpss.test) |
adf_fruit |
ADF test result for fruit |
kpss_fruit |
KPSS test result for fruit |
## Not run: fruit <- rnorm(100, 100, 10) oil <- rnorm(100, 50, 5) results <- test_stationarity(oil, fruit, verbose = TRUE) print(results$adf_oil) ## End(Not run)## Not run: fruit <- rnorm(100, 100, 10) oil <- rnorm(100, 50, 5) results <- test_stationarity(oil, fruit, verbose = TRUE) print(results$adf_oil) ## End(Not run)
This function trains Support Vector Regression models using different kernel functions, with options for hyperparameter tuning. It scales the features, trains models for each specified kernel, and returns performance metrics.
train_svr_models( X_train, X_test, y_train, y_test, model_name, kernels = c("linear", "polynomial", "radial", "sigmoid"), cost = 100, gamma = 0.1, epsilon = 0.1, verbose = TRUE )train_svr_models( X_train, X_test, y_train, y_test, model_name, kernels = c("linear", "polynomial", "radial", "sigmoid"), cost = 100, gamma = 0.1, epsilon = 0.1, verbose = TRUE )
X_train |
A data frame or matrix of training features |
X_test |
A data frame or matrix of test features |
y_train |
A numeric vector of training target values |
y_test |
A numeric vector of test target values |
model_name |
A character string naming the model type (e.g., "Standard SVR") |
kernels |
A character vector of kernel types. Options: "linear", "polynomial", "radial", "sigmoid" (default: all four) |
cost |
A numeric value for the cost parameter C (default: 100) |
gamma |
A numeric value for the gamma parameter (default: 0.1) |
epsilon |
A numeric value for the epsilon parameter (default: 0.1) |
verbose |
Logical; print progress messages (default: TRUE) |
A list containing:
results |
A data frame with performance metrics for each kernel |
predictions |
A list of predictions for training and test sets for each kernel |
models |
A list of trained SVR model objects for each kernel |
## Not run: # Assuming prepared data from prepare_ectsvr_data() data <- prepare_ectsvr_data(fruit, oil) results <- train_svr_models( data$X_train_std, data$X_test_std, data$y_train, data$y_test, "Standard SVR" ) print(results$results) ## End(Not run)## Not run: # Assuming prepared data from prepare_ectsvr_data() data <- prepare_ectsvr_data(fruit, oil) results <- train_svr_models( data$X_train_std, data$X_test_std, data$y_train, data$y_test, "Standard SVR" ) print(results$results) ## End(Not run)