library(MASS) data(iris) head(iris) Species <- iris$Species levels(Species) g <- length(levels(Species)) p <- 4 n <- dim(X)[1] X <- iris[,1:p] lda.obj <- lda(X,Species) lda.obj q <- min(2,p) m <- cbind(1:q,lda.obj$svd,100*lda.obj$svd/sum(lda.obj$svd),100*cumsum(lda.obj$svd)/sum(lda.obj$svd)) colnames(m) <- c("e-pair","eigenvalue","%acc't by","% cumulative acc't by") options(digits = 3) m LD.f <- lda.obj$scaling X.m <- matrix(as.numeric(unlist(X)),n,p) scores <- X.m%*%LD.f plot(scores[,1],scores[,2],type="n") for (i in 1:g){ I <- Species == levels(Species)[i] points(scores[I,1],scores[I,2],col=i)} ########################################################## head(fgl) lda.obj <- lda(type~.,data=fgl) n <- dim(fgl)[1] p <- dim(fgl)[2]-1 q <- min(5,p) m <- cbind(1:q,lda.obj$svd,100*lda.obj$svd/sum(lda.obj$svd),100*cumsum(lda.obj$svd)/sum(lda.obj$svd)) colnames(m) <- c("e-pair","eigenvalue","%acc't by","% cumulative acc't by") options(digits = 3) m LD.f <- lda.obj$scaling X.m <- matrix(as.numeric(unlist(fgl[,1:9])),n,p) scores <- X.m%*%LD.f plot(scores[,1],scores[,2],type="n") g <- length(levels(fgl$type)) for (i in 1:g){ I <- fgl$type == levels(fgl$type)[i] points(scores[I,1],scores[I,2],col=i)} method <- 1 # random forest libary(randomForest) method <- 2 #lda yX <- data.frame(fgl$type,X.m) r <- 50 nt <- floor(0.9*n) for (i in 1:r){ s <- sample(1:n,size=nt) u <- rep(0,nt) ct <- 1 for (j in 1:n){ if (sum(s==j)==0) { u[ct] <- j ct <- ct + 1} } if (method ==1){ rf.obj <- randomForest(x=X.m,y=fgl$type, xtest=X.m[u,],ytest=fgl$type[u]) predictions <- rf.obj$predicted} if (method==2){ lda.obj <- lda(fgl$type~X.m,CV=F,subset=s) #,priors=rep(1/6,6) predictions <- predict(lda.obj,newdata=yX)$class } if (i==1) save.m <- cbind(fgl$type[u],predictions[u]) if (i!=1) save.m <- rbind(save.m,cbind(fgl$type[u],predictions[u])) print(c("Loop ",i)) } y <- save.m[,1] predictions <- save.m[,2] tabl <- table(y,predictions) options(digits=3) 100*diag(tabl/rowSums(tabl)) rowSums(tabl) 100*sum(diag(tabl))/sum(tabl) tabl2 <- rbind(cbind(tabl,rowSums(tabl)),c(colSums(tabl),sum(sum(tabl)))) rownames(tabl2) <- c(rownames(tabl),"Total") colnames(tabl2) <- c(colnames(tabl),"Total") "Rows = actual columns = predicted" tabl2