[R] cross validation using e1071:SVM

Neeti nikkihathi at gmail.com
Tue Nov 23 13:37:51 CET 2010


Hi everyone

I am trying to do cross validation (10 fold CV) by using e1071:svm method. I
know that there is an option (“cross”) for cross validation but still I
wanted to make a function to Generate cross-validation indices  using pls:
cvsegments method.

#####################################################################

Code (at the end) Is working fine but sometime caret:confusionMatrix gives
following error:

stat_result<- confusionMatrix(pred_true1,species_test)

Error in confusionMatrix.default(pred_true1, species_test) : 
 The data and reference factors must have the same number of levels

My data: total number=260
	Class = 6

#####################################
Sorry if I missed some previous discussion about this problem.

It would be nice if anyone explain or point out the mistake I am doing in
this following code.

Is there another way to do this? As I wanted to check my result based on
Accuracy and Kappa value generated by caret:confusionMatrix.

##########################################
Code
#########################################
x<-NULL
index<-cvsegments(nrow(data),10)
for(i in 1:length(index))
{
	x<-matrix(index[i])
	testset<-data[x[[1]],]
	trainset<-data[-x[[1]],]
	
	species<-as.factor(trainset[,ncol(trainset)])
	train1<-trainset[,-ncol(trainset)]
	train1<-train1[,-(1)]

	test_t<-testset[,-ncol(testset)]
	species_test<-as.factor(testset[,ncol(testset)])
	test_t<-test_t[,-(1)]
	model_true1 <- svm(train1,species)
	pred_true1<-predict(model_true1,test_t)
	stat_result<- confusionMatrix(pred_true1,species_test)
	stat_true[[i]]<-as.matrix(stat_result,what="overall")
	kappa_true[i]<-stat_true[[i]][2,1]
	accuracy_true[i]<-stat_true[[i]][1,1]
}

-- 
View this message in context: http://r.789695.n4.nabble.com/cross-validation-using-e1071-SVM-tp3055335p3055335.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list