library(class) library(e1071) source("data") x <- subset(data, select = -Class) y <- c(rep(1,sum(data$Class == 1)),rep(2,sum(data$Class == 2))) pred <- rep(0, length(y)) ng <- length(unique(y)) np <- dim(x) ncb <-ng * round((0.4*ng*(ng-1 + np[2]/2))/ng,0) #modified version of original suggestion from Kohonen, but here with equal CBVs for all groups for(iter in 1:length(y)) { set.seed(0) # note that LVQ in R is only reproducible given a fixed seed mod0 <- lvqinit(x[-iter,], y[-iter], ncb, prior=c(0.5,0.5)) mod1 <- olvq1(x[-iter,], y[-iter], mod0) mod1 <- lvq3(x[-iter,], y[-iter], mod1) pred[iter] <- lvqtest(mod1, x[iter,]) } cM <- table(true = data$Class, predicted = pred) cMstats <- classAgreement(cM) # cat("\n Multipass LVQ \n") # cat(" ============================== ") # cat("\n Accuracy: ") ; print(cMstats$diag) # cat("\n Kappa: ") ; print(cMstats$kappa) # cat("\n Confusion matrix: \n") ; print(cM) # cat("\n") write(cM, 'confusionMatrix') write(cMstats$diag, 'accuracy') write(cMstats$kappa, 'kappa') write(pred, file="pred_lvq", ncolumns = 1)