library(class) library(e1071) source("data") x <- subset(data, select = -Class) y <- as.factor(data$Class) 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$ numclasses = length(unique(y)) ncp = rep(1/numclasses, numclasses); rand <- sample(10, length(t(y)), replace = T) pred <- rep(0,length(t(y))) for(iter in sort(unique(rand))) { set.seed(0) # note that LVQ in R is only reproducible given a fixed seed mod0 <- lvqinit(x[rand != iter,], y[rand != iter], ncb, prior=ncp) mod1 <- olvq1(x[rand != iter,], y[rand != iter], mod0) mod1 <- lvq3(x[rand != iter,], y[rand != iter], mod1) pred[rand == iter] <- lvqtest(mod1, x[rand == iter,]) } cM <- table(true = data$Class, predicted = pred) cMstats <- classAgreement(cM) write(cM, 'confusionMatrix', ncolumns=1) write(cMstats$diag, 'accuracy') write(cMstats$kappa, 'kappa') write(pred, 'Pred', ncolumns=1)