qed - 1 year ago 132

R Question

Here is the code:

`library(e1071)`

set.seed(1)

x = matrix(rnorm(200 * 2), ncol = 2)

x[1:100, ] = x[1:100, ] + 2

x[101:150, ] = x[101:150, ] - 2

y = c(rep(1, 150), rep(2, 50))

dat = data.frame(x = x, y = as.factor(y))

head(dat)

plot(x, col = y)

train = sample(200, 100)

svmfit = svm(y ~ ., data = dat[train, ], kernel = "radial", gammma = 1, cost = 1)

plot(svmfit, dat[train, ])

summary(svmfit)

The gamma parameter in the summary is different from what I set:

`Call:`

svm(formula = y ~ ., data = dat[train, ], kernel = "radial", gammma = 1, cost = 1)

Parameters:

SVM-Type: C-classification

SVM-Kernel: radial

cost: 1

gamma: 0.5

Number of Support Vectors: 36

( 18 18 )

Number of Classes: 2

Levels:

1 2

What went wrong?

Answer Source

You simply misspelled parameter, look closely `gammma`

, how many `m`

s do you see? It took me a while to see that there are more than 2.

Try

```
library(e1071)
set.seed(1)
x = matrix(rnorm(200 * 2), ncol = 2)
x[1:100, ] = x[1:100, ] + 2
x[101:150, ] = x[101:150, ] - 2
y = c(rep(1, 150), rep(2, 50))
dat = data.frame(x = x, y = as.factor(y))
head(dat)
plot(x, col = y)
train = sample(200, 100)
svmfit = svm(y ~ ., data = dat[train, ], kernel = "radial", gamma = 1, cost = 1)
plot(svmfit, dat[train, ])
summary(svmfit)
```

Shame on e1071 authors for not rising exception for specifying incorrect parameters though...