R : Copyright 2003, The R Development Core Team Version 1.6.2 (2003-01-10) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type `license()' or `licence()' for distribution details. R is a collaborative project with many contributors. Type `contributors()' for more information. Type `demo()' for some demos, `help()' for on-line help, or `help.start()' for a HTML browser interface to help. Type `q()' to quit R. > invisible(options(echo = TRUE)) > library(class) > > train <- read.table("loans.dat",header=F,col.names=c("x1","x2","y")) > > X <- cbind(train$x1,train$x2) > ty <- train$y > y <- as.factor(train$y) > > valid <- read.table("eval.dat",header=F,col.names=c("x1","x2","y")) > > vX <- cbind(valid$x1,valid$x2) > vy <- valid$y > > idx <- seq(3,15,2) > n <- length(idx) > > te <- mat.or.vec(n,1) > ve <- mat.or.vec(n,1) > K <- mat.or.vec(n,1) > > j = 0 > for (i in idx) { + + j <- j + 1 + K[j] = -i + print(i) + + dflt <- knn(train=X,test=X,cl=y,k=i) + + dflt <- as.numeric((dflt==1)) + + err <- abs(ty-dflt) + te[j] <- mean(err) + print(mean(err)) + + dflt <- knn(train=X,test=vX,cl=y,k=i) + + dflt <- as.numeric((dflt==1)) + + err <- abs(vy-dflt) + ve[j] <- mean(err) + print(mean(err)) + + } [1] 3 [1] 0.044 [1] 0.08465 [1] 5 [1] 0.068 [1] 0.07943 [1] 7 [1] 0.064 [1] 0.07572 [1] 9 [1] 0.072 [1] 0.07521 [1] 11 [1] 0.076 [1] 0.07557 [1] 13 [1] 0.06 [1] 0.07635 [1] 15 [1] 0.056 [1] 0.07699 > > source("psopts.r") > postscript(file="nearloss.eps") > > plot(x=c(K,K),y=c(te,ve),type='n',xlab="Complexity",ylab="Loss") > lines(x=K,y=te,lty="solid",col="red") > lines(x=K,y=ve,lty="solid",col="green") > dev.off() null device 1 > > proc.time() [1] 8.51 0.29 8.81 0.00 0.00 >