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.
Un texte encapsul? et encod? dans un jeu de caract?res inconnu a ?t? nettoy?... Nom : non disponible URL : <https://stat.ethz.ch/pipermail/r-help/attachments/20101123/bb734687/attachment.pl>
@Francial Giscard LIBENGUE please post your query again so that with different subject -- View this message in context: http://r.789695.n4.nabble.com/cross-validation-using-e1071-SVM-tp3055335p3055831.html Sent from the R help mailing list archive at Nabble.com.
could anyone help me with my last problem. if the question is not clear please let me know thank you 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-tp3055335p3055836.html Sent from the R help mailing list archive at Nabble.com.
Neeti, I'm pretty sure that the error is related to the confusionMAtrix call, which is in the caret package, not e1071. The error message is pretty clear: you need to pas in two factor objects that have the same levels. You can check by running the commands: str(pred_true1) str(species_test) Also, caret can do the resampling for you instead of you writing the loop yourself. Max
thank you so much for your help. if i am not wrong then createDataPartition can be used to create stratified random splits of a data set. is there other way to do that? Thank you -- View this message in context: http://r.789695.n4.nabble.com/cross-validation-using-e1071-SVM-tp3055335p3057684.html Sent from the R help mailing list archive at Nabble.com.