# svd1.r September 2007, 2009 # # some examples of use of svd to do Singular Value Decomposition # # first example -- singular design matrix X <- matrix(1,6,4) X[,2] <- 1:6 X[,3] <- 19:24 # dependent on first two X[,4] <- (1:6)*(1:6) X # should look familiar # first qr qr(X) # what does QR do in this case? # next svd svd(X) # inner product matrix xpx <- t(X) %*% X print("X'X") xpx eigen(xpx,symmetric=TRUE) # eigenvectors and values of X'X xxp <- X %*% t(X) print("XX'") xxp exxp <- eigen(xxp,symmetric=TRUE) # vector and values of XX' exxp$values # eigenvalues are same except 0's sqrt(exxp$values) # singular values exxp$vectors # other singular vectors # done rm(list=ls()) q()