# SVM Exercise of ISLR

When I was tried to finish the exercise of 9.R SVMs in R of ISLR course(https://lagunita.stanford.edu/courses/HumanitiesSciences/StatLearning/Winter2016/). I found that my result is not in accordance with the answer.

The problem is: In this problem, you will use simulation to evaluate (by Monte Carlo) the expected misclassification error rate given a particular generating model. Let y i be equally divided between classes 0 and 1, and let x i ∈ R 10 be normally distributed.

Given yi=0 , x i ∼ N 10 ( 0 , I 10 ) . Given yi=1, xi ∼ N 10 ( μ , I 10 ) with μ= ( 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 ) .

Now, we would like to know the expected test error rate if we fit an SVM to a sample of 50 random training points from class 1 and 50 more from class 0. We can calculate this to high precision by 1) generating a random training sample to train on, 2) evaluating the number of mistakes we make on a large test set, and then 3) repeating (1-2) many times and averaging the error rate for each trial.

Use svm in the e1071 package with the default settings (the default kernel is a radial kernel). What is the expected test error rate of this method (to within 10%)?

And this is my R code:

set.seed(1001)
counts = 100
errate = rep(0, counts)
for(i in 1:counts){
x = matrix(rnorm(100 * 10), ncol = 10)
y = c(rep(0, 50), rep(1, 50))
x[y == 1, 1:5] = x[y == 1, 1:5] + 1
dat = data.frame(x = x, y = as.factor(y))
svm.fit = svm(y ~ ., data = dat, kernel = "linear", cost = 1)

xtest = matrix(rnorm(100 * 10), ncol = 10)
ytest = sample(c(0, 1), 100, rep = TRUE)
xtest[ytest == 1, 1:5] = x[ytest == 1, 1:5] + 1
testdat = data.frame(x = xtest, y = as.factor(ytest))

ypred = predict(svm.fit, testdat)
result = table(predict = ypred, truth = testdat\$y)
errate[i] = 1 - (result[1] + result[4]) / 100
}
mean(errate)

The result is 0.1196 but the right answer is 0.16350. Which part of my code is wrong?