## source("pca.R") ## bank data ################################################ 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") ## PCA ###################################################### cov(x[,1:6]) ei <- eigen(cov(x[,1:6])) ei plot(1:6,ei$values,main="Scree plot of eigenvalues") plot(1:6,ei$values/sum(ei$values),main="Scree plot of eigenvalues (in %)") bank.pc <- princomp(x[,1:6],cor=FALSE) summary(bank.pc) plot(bank.pc$scores[,1],bank.pc$scores[,2],xlab="PC1",ylab="PC2",main="First 2 PCs") ## automarxx data ########################################### x <- read.csv("CarMarks.csv",sep=";",dec=",") dimnames(x)[[1]] <- x[,1] x <- x[,2:ncol(x)] ## PCA ###################################################### cov(x[,1:6]) ei <- eigen(cov(x[,1:6])) ei plot(1:6,ei$values,main="Scree plot of eigenvalues") plot(1:6,ei$values/sum(ei$values),main="Scree plot of eigenvalues (in %)") auto.pc <- princomp(x[,1:6],cor=FALSE) summary(auto.pc) plot(auto.pc$scores[,1],auto.pc$scores[,2],xlab="PC1",ylab="PC2",main="First 2 PCs") plot(auto.pc$scores[,1],auto.pc$scores[,2],xlab="PC1",ylab="PC2",main="First 2 PCs",col="white") text(auto.pc$scores[,1],auto.pc$scores[,2],labels=rownames(x)) auto.pc <- princomp(x[,1:6],cor=TRUE) summary(auto.pc) plot(auto.pc$scores[,1],auto.pc$scores[,2],xlab="PC1",ylab="PC2",main="First 2 PCs") plot(auto.pc$scores[,1],auto.pc$scores[,2],xlab="PC1",ylab="PC2",main="First 2 PCs",col="white") text(auto.pc$scores[,1],auto.pc$scores[,2],labels=rownames(x))