[R] SVM cross validation in e1071
Tao Shi
shitao at hotmail.com
Wed Jul 8 02:33:40 CEST 2009
Hi list,
Could someone help me to explain why the leave-one-out cross validation results I got from svm using the internal option "cross" are different from those I got manually? It seems using "cross" to do cross validation, the results are always better. Please see the code below. I also include lda as a comparison.
I'm using WinXP, R-2.9.0, and e1071_1.5-19.
Many thanks!
...Tao
##==================================================
## manual
##
##
> set.seed(1234)
> dat <- data.frame( rbind(matrix(rnorm(1000),ncol = 10), matrix(rnorm(1000, mean=0.6),ncol = 10)))
> cl <- as.factor(rep(1:2, each=100))
> y.lda <- rep(NA, nrow(dat))
> y.svm <- rep(NA, nrow(dat))
> for (i in 1:nrow(dat)){
+ testset <- dat[i, ]
+ trainset <- dat[-i, ]
+ model.lda <- lda(cl[-i]~., data=trainset)
+ model.svm <- svm(cl[-i]~., data=trainset)
+ y.lda[i] <- as.character(predict(model.lda, testset)$class)
+ y.svm[i] <- as.character(predict(model.svm, testset))
+ }
>
> table(y.lda, cl)
cl
y.lda 1 2
1 84 10
2 16 90
> table(y.svm, cl)
cl
y.svm 1 2
1 83 8
2 17 92
##==========================================
## using internal CV options
##
> z2 <- lda(cl~., data=dat, CV=T)
> table(z2$class, cl)
cl
1 2
1 84 10
2 16 90
> z <- svm(cl~., data=dat, cross=200)
> table(z$fitted, cl)
cl
1 2
1 93 4
2 7 96
More information about the R-help
mailing list