-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSupportVectorMachinesV2(1).R
More file actions
67 lines (60 loc) · 2.53 KB
/
SupportVectorMachinesV2(1).R
File metadata and controls
67 lines (60 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Support Vector Machine using Caret
library(caret)
data(iris)
#Create traning test split
intrain <- createDataPartition(iris$Species,p=0.75,list = FALSE)
train1 <- iris[intrain,]
test1 <- iris[-intrain,]
#Create cross validation
trctrl <- trainControl(method = "cv", number = 10)
#Fit the SVM model to training data
#svmRadial uses the Radial Kernel.
#You can explicitly specify the cost parameter by using the C = option
#and the gamma (sigma) parameter using the sigma = option
modSVMFit <- train(Species ~ .,method="svmRadial",sigma =.2,trControl=trctrl,data=train1)
#See model fit details
modSVMFit$finalModel
#See the tuning parametrs used (cost C, and sigma of the radial kernel function)
modSVMFit$bestTune
#See the results details by each optimization run
modSVMFit$results
#Predict test dataset
SVMpredict <- predict(modSVMFit,test1)
confusionMatrix(SVMpredict,test1$Species)
#Use a polynomial Kernel
modSVMFit <- train(Species ~ .,method="svmPoly",degree = 3, trControl=trctrl,data=train1)
#See model fit details
modSVMFit$finalModel
#The polynomial kernel is defined as (scale * crossprod(x, y) + offset)^degree
#See the tuning parametrs used (cost C, Scale of the kernel function)
modSVMFit$bestTune
#See the results details by each optimization run
modSVMFit$results
#Predict test dataset
SVMpredict <- predict(modSVMFit,test1)
confusionMatrix(SVMpredict,test1$Species)
#Use a tuning grid to tune parameters. Need one column for each parameter that can be tuned
grid <- expand.grid(C=c(.1,1,5,10), degree=c(2,3,4), scale=c(1,2))
modSVMFit <- train(Species ~ .,method="svmPoly",tuneGrid=grid, trControl=trctrl,data=train1)
#See model fit details
modSVMFit$finalModel
#The polynomial kernel is defined as (scale * crossprod(x, y) + offset)^degree
#See the tuning parametrs used (cost C, Scale of the kernel function)
modSVMFit$bestTune
#See the results details by each grid search run
modSVMFit$results
#Predict test dataset
SVMpredict <- predict(modSVMFit,test1)
confusionMatrix(SVMpredict,test1$Species)
# Application to Gene Expression Data
library(ISLR)
train1 = data.frame(x=Khan$xtrain, y=as.factor(Khan$ytrain))
test1 = data.frame(x=Khan$xtest, y=as.factor(Khan$ytest))
#Create cross validation
trctrl <- trainControl(method = "cv", number = 10)
#Fit the SVM model to training data
#svmLinear uses the linear Kernel.
modSVMFit <- train(y ~ .,method="svmLinear2",trControl=trctrl,data=train1)
SVMpredict <- predict(modSVMFit,test1)
confusionMatrix(SVMpredict,test1$y)
modSVMFit$finalModel