 # Support Vector Regression in R

As an applied scientist and systems thinker I’ve always been interested in the problems of prediction, pattern recognition and fitting non-linear models in the process. One interesting and helpful technique is Support Vector Regression – a machine learning technique and a variant of Support Vector Machines.

Today I wanted to learn how-to use Support Vector Regression as easily and simply as possible in R – and luckily I found this great tutorial by Alexandre KOWALCZYK.

Below is Support Vector Regression using the e1071 library by David Meyer in 20 lines of R code including example data 🙂

```#Load support vector library
library (e1071)

#Create toy dataset
X <- 1:10
Y <- X*X
data <- data.frame ( X , Y )

#Create SVR model
svrmodel <- svm( Y ~ X, data)
tuneResult <- tune(svm, Y ~ X,  data = data,ranges = list(epsilon = seq(0,0.2,0.01), cost= 2^(2:9)))
svrmodel <- tuneResult\$best.model
predictedY <- predict( svrmodel , newdata = data.frame( X <- data\$X+0.5))

#plot data
plot ( data )
lines ( data , col = "blue" )

# Add points for fitted svrmodel
points(data\$X+0.5 , predictedY , col = "red" , pch=4)
lines( data\$X+0.5 , predictedY , col = "red" )
```

This library is super simple to use as I am sure you can see – its the same as a linear model (lm) but you use the svm command! Unlike the lm however its important to tune your SVM model this is achieved using a grid search approach. As can be seen in the plot above Support Vector Regression is very effective at fitting non-linear models.

This site uses Akismet to reduce spam. Learn how your comment data is processed.