Update page 'Solutions to the questions concerning R language'
@@ -8,3 +8,211 @@ train = sample(dim(OJ)[1], 800)
|
||||
OJ.train = OJ[train, ]
|
||||
OJ.test = OJ[-train, ]
|
||||
```
|
||||
|
||||
**ii.**
|
||||
```
|
||||
library(e1071)
|
||||
```
|
||||
```
|
||||
## Warning: package 'e1071' was built under R version 3.0.2
|
||||
```
|
||||
```
|
||||
## Loading required package: class
|
||||
```
|
||||
```
|
||||
svm.linear = svm(Purchase ~ ., kernel = "linear", data = OJ.train, cost = 0.01)
|
||||
summary(svm.linear)
|
||||
```
|
||||
|
||||
```
|
||||
##
|
||||
## Call:
|
||||
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "linear",
|
||||
## cost = 0.01)
|
||||
##
|
||||
##
|
||||
## Parameters:
|
||||
## SVM-Type: C-classification
|
||||
## SVM-Kernel: linear
|
||||
## cost: 0.01
|
||||
## gamma: 0.05556
|
||||
##
|
||||
## Number of Support Vectors: 432
|
||||
##
|
||||
## ( 217 215 )
|
||||
##
|
||||
##
|
||||
## Number of Classes: 2
|
||||
##
|
||||
## Levels:
|
||||
## CH MM
|
||||
```
|
||||
Support vector classifier creates 432 support vectors out of 800 training points. Out of these, 217 belong to level 𝙲𝙷 and remaining 215 belong to level 𝙼𝙼.
|
||||
|
||||
**iii.**
|
||||
```
|
||||
train.pred = predict(svm.linear, OJ.train)
|
||||
table(OJ.train$Purchase, train.pred)
|
||||
```
|
||||
```
|
||||
## train.pred
|
||||
## CH MM
|
||||
## CH 439 53
|
||||
## MM 82 226
|
||||
```
|
||||
```
|
||||
(82 + 53)/(439 + 53 + 82 + 226)
|
||||
```
|
||||
```
|
||||
## [1] 0.1688
|
||||
```
|
||||
```
|
||||
test.pred = predict(svm.linear, OJ.test)
|
||||
table(OJ.test$Purchase, test.pred)
|
||||
```
|
||||
```
|
||||
## test.pred
|
||||
## CH MM
|
||||
## CH 142 19
|
||||
## MM 29 80
|
||||
```
|
||||
```
|
||||
(19 + 29)/(142 + 19 + 29 + 80)
|
||||
```
|
||||
```
|
||||
## [1] 0.1778
|
||||
```
|
||||
The training error rate is 16.9% and test error rate is about 17.8%.
|
||||
|
||||
**iv.**
|
||||
```
|
||||
set.seed(1554)
|
||||
tune.out = tune(svm, Purchase ~ ., data = OJ.train, kernel = "linear", ranges = list(cost = 10^seq(-2, 1, by = 0.25)))
|
||||
summary(tune.out)
|
||||
```
|
||||
```
|
||||
##
|
||||
## Parameter tuning of 'svm':
|
||||
##
|
||||
## - sampling method: 10-fold cross validation
|
||||
##
|
||||
## - best parameters:
|
||||
## cost
|
||||
## 0.3162
|
||||
##
|
||||
## - best performance: 0.1687
|
||||
##
|
||||
## - Detailed performance results:
|
||||
## cost error dispersion
|
||||
## 1 0.01000 0.1688 0.03692
|
||||
## 2 0.01778 0.1688 0.03398
|
||||
## 3 0.03162 0.1713 0.03230
|
||||
## 4 0.05623 0.1725 0.03162
|
||||
## 5 0.10000 0.1700 0.03291
|
||||
## 6 0.17783 0.1712 0.03336
|
||||
## 7 0.31623 0.1687 0.03499
|
||||
## 8 0.56234 0.1700 0.03129
|
||||
## 9 1.00000 0.1687 0.03398
|
||||
## 10 1.77828 0.1688 0.03241
|
||||
## 11 3.16228 0.1688 0.03294
|
||||
## 12 5.62341 0.1713 0.03121
|
||||
## 13 10.00000 0.1713 0.03283
|
||||
```
|
||||
Tuning shows that optimal cost is 0.3162.
|
||||
|
||||
**v.**
|
||||
```
|
||||
svm.linear = svm(Purchase ~ ., kernel = "linear", data = OJ.train, cost = tune.out$best.parameters$cost)
|
||||
train.pred = predict(svm.linear, OJ.train)
|
||||
table(OJ.train$Purchase, train.pred)
|
||||
```
|
||||
```
|
||||
## train.pred
|
||||
## CH MM
|
||||
## CH 435 57
|
||||
## MM 71 237
|
||||
```
|
||||
```
|
||||
(57 + 71)/(435 + 57 + 71 + 237)
|
||||
```
|
||||
```
|
||||
## [1] 0.16
|
||||
```
|
||||
```
|
||||
test.pred = predict(svm.linear, OJ.test)
|
||||
table(OJ.test$Purchase, test.pred)
|
||||
```
|
||||
```
|
||||
## test.pred
|
||||
## CH MM
|
||||
## CH 141 20
|
||||
## MM 29 80
|
||||
```
|
||||
```
|
||||
(29 + 20)/(141 + 20 + 29 + 80)
|
||||
```
|
||||
```
|
||||
## [1] 0.1815
|
||||
```
|
||||
The training error decreases to 16% but test error slightly increases to 18.1% by using best cost.
|
||||
|
||||
**vi.**
|
||||
```
|
||||
set.seed(410)
|
||||
svm.radial = svm(Purchase ~ ., data = OJ.train, kernel = "radial")
|
||||
summary(svm.radial)
|
||||
```
|
||||
```
|
||||
##
|
||||
## Call:
|
||||
## svm(formula = Purchase ~ ., data = OJ.train, kernel = "radial")
|
||||
##
|
||||
##
|
||||
## Parameters:
|
||||
## SVM-Type: C-classification
|
||||
## SVM-Kernel: radial
|
||||
## cost: 1
|
||||
## gamma: 0.05556
|
||||
##
|
||||
## Number of Support Vectors: 367
|
||||
##
|
||||
## ( 184 183 )
|
||||
##
|
||||
##
|
||||
## Number of Classes: 2
|
||||
##
|
||||
## Levels:
|
||||
## CH MM
|
||||
```
|
||||
```
|
||||
train.pred = predict(svm.radial, OJ.train)
|
||||
table(OJ.train$Purchase, train.pred)
|
||||
```
|
||||
```
|
||||
## train.pred
|
||||
## CH MM
|
||||
## CH 452 40
|
||||
## MM 78 230
|
||||
```
|
||||
```
|
||||
(40 + 78)/(452 + 40 + 78 + 230)
|
||||
```
|
||||
```
|
||||
## [1] 0.1475
|
||||
```
|
||||
```
|
||||
test.pred = predict(svm.radial, OJ.test)
|
||||
table(OJ.test$Purchase, test.pred)
|
||||
```
|
||||
```
|
||||
## test.pred
|
||||
## CH MM
|
||||
## CH 146 15
|
||||
## MM 27 82
|
||||
```
|
||||
```
|
||||
(27 + 15)/(146 + 15 + 27 + 82)
|
||||
```
|
||||
```
|
||||
## [1] 0.1556
|
||||
```
|
||||
Reference in New Issue
Block a user