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 )
#add line through data points
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.

Example Support Vector Regression Plot

As can be seen in the plot above Support Vector Regression is very effective at fitting non-linear models.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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