- Preparing data
- Fitting the model and predicting test data
- Accuracy check
- Source code listing

library(e1071) library(caret)

**Preparing data**

We'll use the Boston housing price dataset as a target regression data in this tutorial. We'll prepare data by splitting it into the train and test parts.

boston = MASS::Boston set.seed(123) indexes = createDataPartition(boston$medv, p = .9, list = F) train = boston[indexes, ] test = boston[-indexes, ]

**Fitting the model and predicting test data**

Train and test data are ready. Now, we can define the svm model with default parameters and fit it with train data. Here, we can change the kernel type into 'linear', 'polynomial', and 'sigmoid' for training and predicting. The default is a 'radial' kernel.

model_reg = svm(medv~., data=train) print(model_reg) Call: svm(formula = medv ~ ., data = train) Parameters: SVM-Type: eps-regression SVM-Kernel: radial cost: 1 gamma: 0.07692308 epsilon: 0.1 Number of Support Vectors: 306

Next, we'll predict the test data and plot the results to compare visually.

pred = predict(model_reg, test) x = 1:length(test$medv) plot(x, test$medv, pch=18, col="red") lines(x, pred, lwd="1", col="blue")

**Accuracy check**

Finally, we'll check the prediction accuracy with the MSE, MAE, RMSE, and R-squared metrics.

mse = MSE(test$medv, pred) mae = MAE(test$medv, pred) rmse = RMSE(test$medv, pred) r2 = R2(test$medv, pred, form = "traditional") cat(" MAE:", mae, "\n", "MSE:", mse, "\n", "RMSE:", rmse, "\n", "R-squared:", r2) MAE: 1.877403 MSE: 6.028015 RMSE: 2.455202 R-squared: 0.914078

In this tutorial, we have briefly learned how to use an 'e1071' package's svm function for the regression problem. Thank you for reading and the full source code is listed below.

**Source code listing**

library(e1071) library(caret) # Regression example boston = MASS::Boston set.seed(123) indexes = createDataPartition(boston$medv, p = .9, list = F) train = boston[indexes, ] test = boston[-indexes, ] model_reg = svm(medv~., data=train) print(model_reg) pred = predict(model_reg, test) x=1:length(test$medv) plot(x, test$medv, pch=18, col="red") lines(x, pred, lwd="1", col="blue") # accuracy check mse = MSE(test$medv, pred) mae = MAE(test$medv, pred) rmse = RMSE(test$medv, pred) r2 = R2(test$medv, pred, form = "traditional") cat(" MAE:", mae, "\n", "MSE:", mse, "\n", "RMSE:", rmse, "\n", "R-squared:", r2)

` `

## No comments:

## Post a Comment