## source("bank.R") library(Rfwdmv) data(bank.dat) x <- bank.dat x <- data.frame(x,c(rep("genuine",100),rep("forged",100))) names(x) <- c("length","left.height","right.height", "lower.frame","upper.frame","diagonal","group") ## Boxplots ################################################# boxplot(x[,1]) boxplot(x[,1] ~ x[,7]) boxplot(x$length ~ x$group) boxplot(x$diagonal ~ x$group) ## Scatterplots ############################################ plot(x[,c(1,6)]) plot(x$length,x$diagonal) col <- c(rep("blue",100),rep("red",100)) plot(x$upper.frame,x$diagonal,col=col) plot(1:20,1:20,pch=1:20) pch <- c(rep(1,100),rep(8,100)) plot(x$upper.frame,x$diagonal,col=col,pch=pch) plot(x$lower.frame,x$upper.frame,col=col,pch=pch) ## Scatterplot Matrix ####################################### pairs(x[,1:6],col=col) plot(x[,1:6],col=col) ## Histograms ############################################### hist(x$diagonal) Breaks <- function(x,x0,h){ minx <- x0 + floor((min(xd)-x0)/h)*h maxx <- x0 + ceiling((max(xd)-x0)/h)*h B <- seq(minx,maxx,by=h) B[1] <- min(x); B[length(B)] <- max(x) return(B) } xd <- x$diagonal par(mfrow=c(2,2)) hist(xd,breaks=Breaks(xd,0,0.1), main="h=0.1") hist(xd,breaks=Breaks(xd,0,0.2), main="h=0.2") hist(xd,breaks=Breaks(xd,0,0.3), main="h=0.3") hist(xd,breaks=Breaks(xd,0,0.4), main="h=0.4") par(mfrow=c(1,1)) par(mfrow=c(2,2)) hist(xd,breaks=Breaks(xd,0.0,0.4), main="x0=0.1") hist(xd,breaks=Breaks(xd,0.1,0.4), main="x0=0.2") hist(xd,breaks=Breaks(xd,0.2,0.4), main="x0=0.3") hist(xd,breaks=Breaks(xd,0.3,0.4), main="x0=0.4") par(mfrow=c(1,1)) ## Kernel Density Estimates ################################# library(KernSmooth) plot(bkde(xd),type="l") fh <- bkde2D(x[,4:5], bandwidth=n^(-1/6)*sd(x[,4:5])) contour(fh$x1, fh$x1, fh$fhat) persp(fh$x1, fh$x1, fh$fhat, theta = 30, phi = 30, expand = 0.5, col = "lightblue") fh <- bkde2D(x[,1:2], bandwidth=0.25*n^(-1/6)*sd(x[,4:5])) contour(fh$x1, fh$x1, fh$fhat) persp(fh$x1, fh$x1, fh$fhat, theta = 30, phi = 30, expand = 0.5, col = "lightblue") par(mfrow=c(2,2)) plot(bkde(xd,bandwidth=0.1), type="l",main="h=0.1") plot(bkde(xd,bandwidth=0.2), type="l",main="h=0.2") plot(bkde(xd,bandwidth=0.4), type="l",main="h=0.4") plot(bkde(xd,bandwidth=0.8), type="l",main="h=0.8") par(mfrow=c(1,1)) plot(bkde(xd[x$group=="genuine"]),type="l",col="blue",xlim=range(xd)) lines(bkde(xd[x$group=="forged"]),col="red") ## Tchernoff Faces ########################################## library(aplpack) faces(x[91:110,1:6]) i <- c( sample(1:100,10), sample(101:200,10) ) faces(x[i,1:6]) ## Parallel Coordinates ##################################### parcoord(x[,1:6]) parcoord(x[,6:1]) parcoord(x[,1:6],col=col) parcoord(x[,6:1],col=col) ## Andrews Curves ########################################### andrews <- function(x,col="black",xlab="t",ylab="data",main="Andrews Curves",stand=TRUE,...){ x <- as.matrix(x) t <- seq(-pi,pi,length=100) a <- matrix(NA,length(t),ncol(x)) if (stand){ minx <- apply(x,2,min) maxx <- apply(x,2,max) xs <- t( (t(x) - minx)/(maxx-minx) ) ##xs <- t( (t(x) - colMeans(x))/sd(x) ) }else{ xs <- x } if (length(col)==1){ col <- rep(col,nrow(x)) } for (j in 1:ncol(xs)){ print(j) if (j==1){ a[,j] <- 1/sqrt(2) }else{ if (j %% 2 == 0){ a[,j] <- sin(t*j/2) } if (j %% 2 == 1){ a[,j] <- cos(t*(j-1)/2) } } } ylim <- range(a %*% xs[1,]) if (nrow(xs)>1){ for (i in 2:nrow(xs)){ ylim <- range( c(ylim, a %*% xs[i,]) ) } } for (i in 1:nrow(xs)){ ac <- a %*% xs[i,] if (i==1){ plot(t,ac,type="l",col=col[i],ylim=ylim, xlab=xlab,ylab=ylab,main=main,...) }else{ lines(t,ac,col=col[i]) } } } andrews(x[,1:6]) andrews(x[,1:6],stand=FALSE) andrews(x[,1:6],col=col) andrews(x[,6:1]) andrews(x[,6:1],col=col) andrews(x[96:105,1:6],col=col[96:105]) andrews(x[96:105,6:1],col=col[96:105]) ## Star Plots ########################################### stars(x[,1:6]) stars(x[,1:6], full=FALSE) stars(x[,1:6], full=FALSE, col.stars=col) stars(x[,1:6], full=FALSE, draw.segments=TRUE,col.segments=rainbow(6)) stars(x[,1:6], locations = c(0,0), radius = FALSE) ## spider stars(x[,1:6], locations = c(0,0), radius = FALSE, draw.segments=TRUE,col.segments=rainbow(6))