TrainA1<-read.table("TrainA1.dat") names(TrainA1)<-c("x1","x2","y") g1 <- lm( y ~ x1 + x2, data=TrainA1) y1 <- predict( g1, TrainA1[,-3]) yhat <- rep( 0,100) for( i in 1:100 ) { if(y1[i] > 0.5) yhat[i] <- 1 else yhat[i] <- 0} CErr = 0 for( i in 1:50 ) { if( yhat[i] == 0 ) CErr = CErr + 1 } for( i in 51:100 ) { if( yhat[i] == 1 ) CErr = CErr + 1 } par(mfcol=c(2,2)) plot(TrainA1[,-3],col=TrainA1$y+1) title("Training Set A1") mtext(paste("Plug-in Error = ", 100*CErr/100, "%")) p <- as.matrix(TrainA1[, -3]) xp <- seq(min(TrainA1$x1), max(TrainA1$x1), length = 50); np <- length(xp) yp <- seq(min(TrainA1$x2), max(TrainA1$x2), length = 50); pT <- expand.grid(x1 = xp, x2 = yp) gp <- predict(g1,pT) contour(xp, yp, matrix(gp, np), add = T, levels = 0.5, labex = 0) TrainA2 <- read.table("TrainA2.dat") names(TrainA2)<-c("x1","x2","y") y2 <- predict( g1, TrainA2[,-3]) yhat2 <- rep( 0,100) for( i in 1:100 ) { if(y2[i] > 0.5) yhat2[i] <- 1 else yhat2[i] <- 0} CErr2 = 0 for( i in 1:50 ) { if( yhat2[i] == 0 ) CErr2 = CErr2 + 1 } for( i in 51:100 ) { if( yhat2[i] == 1 ) CErr2 = CErr2 + 1 } plot(TrainA2[,-3],col=TrainA2$y+1) title("Classification of Training Set A2") mtext(paste("Test Error = ", 100*CErr2/100, "%")) p <- as.matrix(TrainA1[, -3]) xp <- seq(min(TrainA2$x1), max(TrainA2$x1), length = 50); np <- length(xp) yp <- seq(min(TrainA2$x2), max(TrainA2$x2), length = 50); pT <- expand.grid(x1 = xp, x2 = yp) gp <- predict(g1,pT) contour(xp, yp, matrix(gp, np), add = T, levels = 0.5, labex = 0) TrainA12<-rbind(TrainA1,TrainA2) g12 <- lm( y ~ x1 + x2, data=TrainA12) y12 <- predict( g12, TrainA12[,-3]) yhat12<-rep(0,200) for( i in 1:200 ) { if(y12[i] > 0.5) yhat12[i] <- 1 else yhat12[i] <- 0} CErr12 = 0 for( i in 1:50 ) { if( yhat12[i] == 0 ) CErr12 = CErr12 + 1 } for( i in 51:100 ) { if( yhat12[i] == 1 ) CErr12 = CErr12 + 1 } for( i in 100:150 ) { if( yhat12[i] == 0 ) CErr12 = CErr12 + 1 } for( i in 151:200 ) { if( yhat12[i] == 1 ) CErr12 = CErr12 + 1 } plot(TrainA12[,-3],col=TrainA12$y+1) title("Training Sets A1 and A2") mtext(paste("Plug-in Error = ", 100*CErr12/200, "%")) p <- as.matrix(TrainA12[, -3]) xp <- seq(min(TrainA12$x1), max(TrainA12$x1), length = 50); np <- length(xp) yp <- seq(min(TrainA12$x2), max(TrainA12$x2), length = 50); pT <- expand.grid(x1 = xp, x2 = yp) gp <- predict(g12,pT) contour(xp, yp, matrix(gp, np), add = T, levels = 0.5, labex = 0) TestA<-read.table("TestA.dat") names(TestA)<-c("x1","x2") gTest <- predict(g12,TestA) y<-rep(0,400) for( i in 1:400 ) { if(gTest[i] > 0.5) y[i] <- 1 else y[i] <- 0} plot(TestA,col=y+1) title("Test Set A") text.x1=paste("x1=", format(g12$coefficients["x1"], digits=3)) text.x2=paste("x2=", format(g12$coefficients["x2"], digits=3)) text.i=paste("Int=", format(g12$coefficients["(Intercept)"], digits=3)) mtext(paste(text.i, text.x1, text.x2)) contour(xp, yp, matrix(gp, np), add = T, levels = 0.5, labex = 0) write.table(data.frame(TestA,y), "TestA-C-L.dat", quote=F, col.name=F, row.name=F)