# STA 437 (Fall 2009), Assignment 3, Part 2. # Read data, separate class and other variables (as a matrix). d0 <- read.table("sat.dat",head=TRUE) cls <- d0[,ncol(d0)] # class variable cols <- c("red","green","gray")[cls] # vector of colours according to class du <- as.matrix(d0[,-ncol(d0)]) # other variables, unscaled ds <- scale(du,center=T,scale=T) # other variables, scaled # Find principal components, with and without scaling. pcu <- prcomp(du) pcs <- prcomp(ds) # Produce screeplots. pdf("a3-sat-scree.pdf",pointsize=9) par(mfrow=c(1,2)) plot(pcu$sdev,type="b",pch=20,ylim=c(0,max(pcu$sdev)*1.1),yaxs="i") plot(pcs$sdev,type="b",pch=20,ylim=c(0,max(pcs$sdev)*1.1),yaxs="i") dev.off() # Find projections of observations on first two principal component directions. pcu2 <- pcu$rotation[,1:2] pcs2 <- pcs$rotation[,1:2] pcu.prj <- du %*% pcu2 pcm.prj <- dm %*% pcm2 # Look at PC directions (eigenvectors) pdf("a3-sat-rotations.pdf",pointsize=9) par(mfrow=c(2,2)) plot(pcu$rotation[,1]) plot(pcu$rotation[,2]) plot(pcs$rotation[,1]) plot(pcs$rotation[,2]) dev.off() # Factor analysis with two common factors. fa <- factanal(ds,2) L <- fa$loadings psi <- fa$uniquenesses # Look at factor loadings and uniquenesses. pdf("a3-sat-fa.pdf",pointsize=9) par(mfrow=c(3,1)) plot(L[,1]) plot(L[,2]) plot(psi) dev.off() # Find linear combinations of scaled variables that produce factor scores. fa.comb <- t(L) %*% solve(L%*%t(L)+diag(psi)) fa.scores <- ds %*% t(fa.comb) # Plot linear combinations of unscaled inputs that give PC projections or # factor scores. pdf("a3-sat-comb.pdf",pointsize=9) par(mfrow=c(1,2)) sdevs <- sd(du) plot(pcu2[,1],pcu2[,2],pch=19,xlab="",ylab="") title("Unscaled PCA") plot(fa.comb[1,]/sdevs,fa.comb[2,]/sdevs,pch=19,xlab="",ylab="") title("Factor analysis") dev.off() # Scatterplots of PC projections and factor scores, with class identified # by colour. pdf("a3-sat-classes.pdf",pointsize=9) par(mfrow=c(2,1)) plot(pcu.prj[,1],pcu.prj[,2],col=cols) plot(fa.scores[,1],fa.scores[,2],col=cols) dev.off()