# # file: svd_example.r # # Purpose: Simple R program showing Singular # Value Decomposition # # rm(list=ls(all=TRUE)) # # Read the vote data from fwf # T <- matrix(scan("C:/uga_course_2015/h106_data_2015.txt",0),ncol=11,byrow=TRUE) dim(T) # nrow <- length(T[,1]) ncol <- length(T[1,]) # xsvd <- svd(T) # # The Two Lines Below Put the Singular Values in a # Diagonal Matrix -- The first one creates an # identity matrix and the second command puts # the singular values on the diagonal # Lambda <- diag(ncol) diag(Lambda) <- xsvd$d # # Compute U*LAMBDA*V' for check below # XX <- xsvd$u %*% Lambda %*% t(xsvd$v) # xerror <- sum(abs(T-XX)) # Eigenvalues <- NULL Eigenvalues <- xsvd$d DUMMY <- NULL #i <- 0 #while (i < nrow){ #i <- i + 1 #DUMMY[i] <- i #} DUMMY <- seq(1:ncol) plot(DUMMY,Eigenvalues, xlab="",ylab="", main="", type="n",axes=FALSE) # # Main title mtext("Singular Values \n for the Evil Bush",side=3,line=1.25,cex=1.2,font=2) # x-axis title mtext("Dimension",side=1,line=2.75,cex=1.2) # y-axis title mtext("Singular Value",side=2,line=2.5,cex=1.2) # axis(1,1:ncol, # labels=c(' 1',' 2',' 3',' 4',' 5',' 6',' 7',' 8',' 9','10','11'), font=2,col.axis='black',cex.axis=1) # axis(2,at=NULL,col.axis='black',cex.axis=1.0,font=2) # box() points(DUMMY,Eigenvalues,pch=16,col="red",cex=1.2,font=2) lines(DUMMY[1:ncol],Eigenvalues[1:ncol],lty=1,lwd=3,col="blue")