R version 2.9.0 (2009-04-17) Copyright (C) 2009 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # svd2.r September 2007, revised 2008, 2009 > # > # some examples of use of svd to do Singular Value Decomposition > # > # second example -- another singular design matrix > # > a <- 3 # number of levels of factor A (alpha's) > b <- 4 # number of levels of factor B (beta's) > N <- a*b # number of observations > One <- matrix(1,N,1) # intercept column > XA <- matrix(c(rep(1,b),rep(0,N)),N,a) # factor A cols > XB <- matrix(rep(diag(1,b),a),N,b,byrow=TRUE) # factor B cols > X <- cbind(One,XA,XB) > # write out design matrix > X [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 1 1 0 0 1 0 0 0 [2,] 1 1 0 0 0 1 0 0 [3,] 1 1 0 0 0 0 1 0 [4,] 1 1 0 0 0 0 0 1 [5,] 1 0 1 0 1 0 0 0 [6,] 1 0 1 0 0 1 0 0 [7,] 1 0 1 0 0 0 1 0 [8,] 1 0 1 0 0 0 0 1 [9,] 1 0 0 1 1 0 0 0 [10,] 1 0 0 1 0 1 0 0 [11,] 1 0 0 1 0 0 1 0 [12,] 1 0 0 1 0 0 0 1 > # next svd > svd(X) $d [1] 4.358899e+00 2.000000e+00 2.000000e+00 1.732051e+00 1.732051e+00 [6] 1.732051e+00 3.594146e-16 1.585726e-16 $u [,1] [,2] [,3] [,4] [,5] [,6] [1,] -0.2886751 -0.1737896 0.36941014 -0.08762684 0.49178314 0.02169989 [2,] -0.2886751 -0.1737896 0.36941014 0.47927171 -0.07881682 -0.11868672 [3,] -0.2886751 -0.1737896 0.36941014 -0.09768823 -0.20648316 0.44477153 [4,] -0.2886751 -0.1737896 0.36941014 -0.29395665 -0.20648316 -0.34778470 [5,] -0.2886751 0.4068134 -0.03419890 -0.08762684 0.49178314 0.02169989 [6,] -0.2886751 0.4068134 -0.03419890 0.47927171 -0.07881682 -0.11868672 [7,] -0.2886751 0.4068134 -0.03419890 -0.09768823 -0.20648316 0.44477153 [8,] -0.2886751 0.4068134 -0.03419890 -0.29395665 -0.20648316 -0.34778470 [9,] -0.2886751 -0.2330238 -0.33521125 -0.08762684 0.49178314 0.02169989 [10,] -0.2886751 -0.2330238 -0.33521125 0.47927171 -0.07881682 -0.11868672 [11,] -0.2886751 -0.2330238 -0.33521125 -0.09768823 -0.20648316 0.44477153 [12,] -0.2886751 -0.2330238 -0.33521125 -0.29395665 -0.20648316 -0.34778470 [,7] [,8] [1,] 0.66711920 0.11303389 [2,] -0.43595595 0.33881034 [3,] -0.12384592 -0.30701654 [4,] -0.10731733 -0.14482769 [5,] -0.37716245 -0.17533823 [6,] 0.22827612 0.24068375 [7,] 0.10057311 0.22373050 [8,] 0.04831323 -0.28907602 [9,] -0.28995675 0.06230434 [10,] 0.20767983 -0.57949409 [11,] 0.02327282 0.08328604 [12,] 0.05900410 0.43390372 $v [,1] [,2] [,3] [,4] [,5] [1,] -0.7947194 8.425252e-19 1.967092e-16 6.287630e-15 -6.237373e-16 [2,] -0.2649065 -3.475791e-01 7.388203e-01 -3.170122e-15 6.045645e-17 [3,] -0.2649065 8.136267e-01 -6.839779e-02 -1.217078e-15 9.326314e-16 [4,] -0.2649065 -4.660476e-01 -6.704225e-01 -3.655073e-15 -1.775916e-16 [5,] -0.1986799 -9.020562e-16 8.326673e-17 -1.517741e-01 8.517934e-01 [6,] -0.1986799 -1.665335e-15 -4.163336e-16 8.301230e-01 -1.365147e-01 [7,] -0.1986799 1.026956e-15 -1.110223e-16 -1.692010e-01 -3.576393e-01 [8,] -0.1986799 1.249001e-15 -1.942890e-16 -5.091478e-01 -3.576393e-01 [,6] [,7] [,8] [1,] -2.060769e-19 -0.5914052 0.1366053 [2,] -1.735750e-16 0.2528817 -0.4463280 [3,] -1.440273e-17 0.2528817 -0.4463280 [4,] -2.921687e-17 0.2528817 -0.4463280 [5,] 3.758531e-02 0.3385234 0.3097228 [6,] -2.055714e-01 0.3385234 0.3097228 [7,] 7.703669e-01 0.3385234 0.3097228 [8,] -6.023808e-01 0.3385234 0.3097228 > # inner product matrix > xpx <- t(X) %*% X > print("X'X") [1] "X'X" > xpx [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [1,] 12 4 4 4 3 3 3 3 [2,] 4 4 0 0 1 1 1 1 [3,] 4 0 4 0 1 1 1 1 [4,] 4 0 0 4 1 1 1 1 [5,] 3 1 1 1 3 0 0 0 [6,] 3 1 1 1 0 3 0 0 [7,] 3 1 1 1 0 0 3 0 [8,] 3 1 1 1 0 0 0 3 > expx <- eigen(xpx,symmetric=TRUE) # eigenvectors and values of X'X > sqrt(expx$values) [1] 4.358899e+00 2.000000e+00 2.000000e+00 1.732051e+00 1.732051e+00 [6] 1.732051e+00 1.192093e-07 4.214685e-08 > # done > rm(list=ls()) > q()