Thursday, February 10, 2011

R code to show UU' != I

U matrix as in SVD X=UDV' is only column orthogonal but not necessarily row orthogonal. U matrix, a N by p matrix, does not contain all the eigenvectors of XX', but only the first p vectors. X is also a N by p matrix, and XX' may have as many as N eigenvectors.
   

x1 <- c(1,1,0,0,0,0)
x2 <- c(0,0,1,1,0,0)
x3 <- c(0,0,0,0,1,1)
y <- c(rnorm(n=2, mean=0), rnorm(n=2,mean=2), rnorm(n=2, mean=4))
x <- cbind(rep(1, times=6), x1,x2,x3))
xxt <- x %*% t(x)
xtx <- t(x) %*% x
e.xtx <- eigen(xtx)
e.xxt <- eigen(xxt)
V <- e.xtx$'vectors'; round(V, digits=2)
U <- e.xxt$'vectors'; round(U, digits=2)
s.v <-e.xtx$'values' # single values

# all these matrix have rank = 3;

d <- sqrt(diag(s.v[c(1,2,3)]))
U3 <- U[,c(1:3)] 
V3 <- V[,c(1:3)]

round(U3 %*% t(U3), digits=2)

# but i cannot get x matrix back
x.svd <- U3 %*% d %*% t(V3)

No comments: