# Programs used for graphics and tables of the book: # "Data anlysis and data mining" by A.Azzalini and B.Scarpa, # © Oxford University Press, 2012 (ISBN 978-0-19-976710-6). # # Code regarding the first part of section 5.6 # (© 2003, 2004, 2012 A.Azzalini and B.Scarpa) #------------------------------------------------------------------------ source("base-www.R") # dataset <- read.table("classes.dat", head=TRUE, nrows=200) x1 <- dataset[,1] x2 <- dataset[,2] gr <- (2-dataset[,3]) # groups are indicated by 0 and 1 K <- 2 n <- nrow(dataset) plot(dataset[,1:2], type="n", xlab=expression(italic(z)[1]), ylab=expression(italic(z)[2])) for(k in 1:K) { g <- (dataset[,3]==k) points(dataset[g,1:2], pch=Pch[k], col=Col[k], cex=cex0) } # # #---k-nearest-neighbors library(class) x <- seq(min(x1), max(x1), length=100) y <- seq(min(x2), max(x2), length=100) nx <- length(x) ny <- length(y) X <- cbind(rep(x, ny), as.vector(matrix(y, nx, ny, byrow = TRUE))) # knn1<-knn(cbind(x1,x2),data.frame(x1=X[,1], x2=X[,2]),gr, k=1) pred <- matrix(knn1, nx, ny) # name <-"figure 5.14a" plot(dataset[,1:2], type="n", xlab=expression(italic(z)[1]), ylab=expression(italic(z)[2])) for(k in 1:K) { g <- (dataset[,3]==k) points(dataset[g,1:2], pch=Pch[k], col=Col[k], cex=0.75) } contour(x,y,pred, levels=0.5, add=TRUE, drawlabels=FALSE, lty=1) pause(name) # knn1<-knn(cbind(x1,x2),data.frame(x1=X[,1], x2=X[,2]),gr, k=50) pred <- matrix(knn1, nx, ny) # name<- "figure 5.14b" plot(dataset[,1:2], type="n", xlab=expression(italic(z)[1]), ylab=expression(italic(z)[2])) for(k in 1:K) { g <- (dataset[,3]==k) points(dataset[g,1:2], pch=Pch[k], col=Col[k], cex=0.75) } contour(x,y,pred, levels=0.5, add=TRUE, drawlabels=FALSE, lty=1) pause(name) #-------------------------- #---loess # require(splines) library(gam) rl3<-gam(gr~ lo(x1,x2),family=binomial) x <- seq(min(x1), max(x1), length=50) y <- seq(min(x2), max(x2), length=50) nx <- length(x) ny <- length(y) X <- cbind(rep(x, ny), as.vector(matrix(y, nx, ny, byrow = TRUE))) pred <- predict(rl3, newdata=data.frame(x1=X[,1], x2=X[,2])) pred <- matrix(pred, nx, ny) # name<- "figure 5.15b" plot(dataset[,1:2], type="n", xlab=expression(italic(z)[1]), ylab=expression(italic(z)[2])) for(k in 1:K) { g <- (dataset[,3]==k) points(dataset[g,1:2], pch=Pch[k], col=Col[k], cex=0.75) } contour(x,y,pred, levels=0.5, add=TRUE, drawlabels=FALSE, lty=1) pause(name) #------------------------------- #---splines # detach(package:gam) # library(mgcv) rl3<-gam(gr~ s(x1,x2),family=binomial) # x <- seq(min(x1), max(x1), length=50) y <- seq(min(x2), max(x2), length=50) nx <- length(x) ny <- length(y) X <- cbind(rep(x, ny), as.vector(matrix(y, nx, ny, byrow = TRUE))) pred <- predict(rl3, newdata=data.frame(x1=X[,1], x2=X[,2])) pred <- matrix(pred, nx, ny) # name<- "figure 5.15b" plot(dataset[,1:2], type="n", xlab=expression(italic(z)[1]), ylab=expression(italic(z)[2])) for(k in 1:K) { g <- (dataset[,3]==k) points(dataset[g,1:2], pch=Pch[k], col=Col[k], cex=0.75) } contour(x,y,pred, levels=0.5, add=TRUE, drawlabels=FALSE, lty=1) pause(name) #---------------------------- detach(package:class) detach(package:mgcv) detach(package:splines) detach.all()