data(Boston)
## Warning in data(Boston): data set 'Boston' not found
library(MASS)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:MASS':
##
## select
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
bostonsvm <- select(Boston,medv,crim)
names(bostonsvm)
## [1] "medv" "crim"
plot(bostonsvm,pch=16)
model <- lm(medv ~ crim, bostonsvm)
abline(model)
predictedY <- predict(model, bostonsvm)
points(bostonsvm$medv, predictedY, col = "blue", pch=4)
rmse <- function(error)
{
sqrt(mean(error^2))
}
error <- model$residuals # same as data$Y - predictedY
predictionRMSE <- rmse(error) # 5.703778
library(e1071)
class(Boston)
## [1] "data.frame"
model <- svm(medv ~ crim , bostonsvm)
predictedY <- predict(model, bostonsvm)
points(bostonsvm$medv, predictedY, col = "red", pch=4)

error <- bostonsvm$medv - predictedY
svrPredictionRMSE <- rmse(error)
svrPredictionRMSE
## [1] 8.387847
predictionRMSE
## [1] 8.467038
tuneResult <- tune(svm, medv~crim, data = bostonsvm,
ranges = list(epsilon = seq(0,1,0.1), cost = 2^(2:9))
)
print(tuneResult)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## epsilon cost
## 0.6 8
##
## - best performance: 67.21342
# best performance: MSE = 8.371412, RMSE = 2.89 epsilon 1e-04 cost 4
# Draw the tuning graph
plot(tuneResult)

tuneResult <- tune(svm, medv~crim, data = bostonsvm,
ranges = list(epsilon = seq(0,0.2,0.01), cost = 2^(2:9))
)
print(tuneResult)
##
## Parameter tuning of 'svm':
##
## - sampling method: 10-fold cross validation
##
## - best parameters:
## epsilon cost
## 0.18 8
##
## - best performance: 71.00509
plot(tuneResult)

tunedModel <- tuneResult$best.model
tunedModelY <- predict(tunedModel, bostonsvm)
error <- bostonsvm$medv - tunedModelY
# this value can be different on your computer
# because the tune method randomly shuffles the data
tunedModelRMSE <- rmse(error)
# 2.219642
tunedModelRMSE
## [1] 8.360493
plot(bostonsvm,pch=16)
