#Rcode9output.txt & #This is the unix command to run this code in the background and save the output in the file code9output.txt set.seed(9999, kind = NULL) repeat.times<-1 q<-.2 iterations<-1000 n<-200 misclass.store<-matrix(0,repeat.times,iterations) bayeserror.store<-rep(0,repeat.times) nnerror.store<-rep(0,repeat.times) rferror.store<-rep(0,repeat.times) library(class) library(rpart) #You can also try using Random Forests for comparison by uncommenting the code provided later #I commented it out because Random Forests is not part of the basic R package and must be installed separately #I put mine in the directory "temp" using the following commands (from within R) #options(CRAN = "http://cran.us.r-project.org/") #install.packages("randomForest",lib="temp") #library(randomForest, lib.loc="temp") for (iui in 1:repeat.times){ #Use a Latin Hypercube Design for the x (the Training Data) for Plottting Purposes x<-matrix(0,n,2) for (dd in 1:2){ x[,dd]<-seq(1,n)/n-.5/n u<-runif(n) o<-order(runif(n)) x[,dd]<-x[o,dd] } y<-rep(0,n) for (i in 1:n){ y[i]<-1*(runif(1)0)+6),cex=.23) points(x[,1],x[,2],pch=20,col=(6*(y>0)+1)) dev.off() #One Nearest Neighbor Plot postscript("code9plotb.ps") par(mfrow=c(1,1),cex=1.5,cex.axis=1.5,cex.lab=1.5,pty="s") plot(x[,1],x[,2],xlab=expression(x^{(1)}),ylab=expression(x^{(2)}),type="n",xlim=c(0,1),ylim=c(0,1)) points(hold.out.x[,1],hold.out.x[,2],pch=15,col=(-ppp+6),cex=.23) points(x[,1],x[,2],pch=20,col=(6*(y>0)+1)) dev.off() print("###############################################################") print("Nearest Neighbor Mean and Standard Deviation") print(mean(nnerror.store)) print(sqrt(var(nnerror.store))) #print("Random Forests Mean and Standard Deviation") #print(mean(rferror.store)) #print(sqrt(var(rferror.store))) print("Bayes Error Mean and Standard Deviation") print(mean(bayeserror.store)) print(sqrt(var(bayeserror.store))) print("AdaBoost Final Mean and Standard Deviation") print(mean(misclass.store[,iterations])) print(sqrt(var(misclass.store[,iterations]))) print("AdaBoost First Iteration Mean and Standard Deviation") print(mean(misclass.store[,1])) print(sqrt(var(misclass.store[,1]))) print("Mean and Standard Deviation of Difference") print(mean(misclass.store[,1]-misclass.store[,iterations])) print(sqrt(var(misclass.store[,1]-misclass.store[,iterations]))) print("Percent of Postive Differences") print(sum(1*(misclass.store[,1]-misclass.store[,iterations])>0)/repeat.times)