forked from asadoughi/stat-learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
7.Rmd
49 lines (44 loc) · 1.41 KB
/
7.Rmd
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
Chapter 9: Exercise 7
========================================================
### a
```{r}
library(ISLR)
gas.med = median(Auto$mpg)
new.var = ifelse(Auto$mpg > gas.med, 1, 0)
Auto$mpglevel = as.factor(new.var)
```
### b
```{r}
library(e1071)
set.seed(3255)
tune.out = tune(svm, mpglevel~., data=Auto, kernel="linear", ranges=list(cost=c(0.01, 0.1, 1, 5, 10, 100)))
summary(tune.out)
```
We see that cross-validation error is minimized for $\tt{cost}=1$.
### c
```{r}
set.seed(21)
tune.out = tune(svm, mpglevel~., data=Auto, kernel="polynomial", ranges=list(cost=c(0.1, 1, 5, 10), degree=c(2, 3, 4)))
summary(tune.out)
```
The lowest cross-validation error is obtained for $\tt{cost} = 10$ and $\tt{degree} = 2$.
```{r}
set.seed(463)
tune.out = tune(svm, mpglevel~., data=Auto, kernel="radial", ranges=list(cost=c(0.1, 1, 5, 10), gamma=c(0.01, 0.1, 1, 5, 10, 100)))
summary(tune.out)
```
Finally, for radial basis kernel, $\tt{cost} = 10$ and $\tt{gamma} = 0.01$.
### d
```{r 7d}
svm.linear = svm(mpglevel~., data=Auto, kernel="linear", cost=1)
svm.poly = svm(mpglevel~., data=Auto, kernel="polynomial", cost=10, degree=2)
svm.radial = svm(mpglevel~., data=Auto, kernel="radial", cost=10, gamma=0.01)
plotpairs = function(fit){
for (name in names(Auto)[!(names(Auto) %in% c("mpg", "mpglevel","name"))]) {
plot(fit, Auto, as.formula(paste("mpg~", name, sep="")))
}
}
plotpairs(svm.linear)
plotpairs(svm.poly)
plotpairs(svm.radial)
```