For the classes given in the vector y, we compute the eigenvalue (spectral) decomposition of the class sample covariance matrices (MLEs) using the data matrix x.

cov_eigen(x, y, pool = FALSE, fast = FALSE, tol = 1e-06)

Arguments

x

data matrix with n observations and p feature vectors

y

class labels for observations (rows) in x

pool

logical. Should the sample covariance matrices be pooled?

fast

logical. Should the Fast SVD be used? See details.

tol

tolerance value below which the singular values of x are considered zero.

Value

a list containing the eigendecomposition for each class. If pool = TRUE, then a single list is returned.

Details

If the fast argument is selected, we utilize the so-called Fast Singular Value Decomposition (SVD) to quickly compute the eigenvalue decomposition. To compute the Fast SVD, we use the corpcor::fast.svd() function, which employs a well-known trick for tall data (large n, small p) and wide data (large p, small n) to compute the SVD corresponding to the nonzero singular values. For more information about the Fast SVD, see corpcor::fast.svd().

Examples

cov_eigen(x = iris[, -5], y = iris[, 5])
#> $setosa #> eigen() decomposition #> $values #> [1] 0.231726576 0.036180358 0.026260471 0.008852595 #> #> $vectors #> [,1] [,2] [,3] [,4] #> [1,] -0.66907840 -0.5978840 0.4399628 -0.03607712 #> [2,] -0.73414783 0.6206734 -0.2746075 -0.01955027 #> [3,] -0.09654390 -0.4900556 -0.8324495 -0.23990129 #> [4,] -0.06356359 -0.1309379 -0.1950675 0.96992969 #> #> #> $versicolor #> eigen() decomposition #> $values #> [1] 0.478116465 0.070936414 0.053680563 0.009594557 #> #> $vectors #> [,1] [,2] [,3] [,4] #> [1,] -0.6867238 0.6690891 -0.26508336 0.1022796 #> [2,] -0.3053470 -0.5674653 -0.72961786 -0.2289194 #> [3,] -0.6236631 -0.3433270 0.62716496 -0.3159668 #> [4,] -0.2149837 -0.3353051 0.06366081 0.9150409 #> #> #> $virginica #> eigen() decomposition #> $values #> [1] 0.68134974 0.10442020 0.05124952 0.03358054 #> #> $vectors #> [,1] [,2] [,3] [,4] #> [1,] 0.7410168 -0.1652590 0.5344502 0.3714117 #> [2,] 0.2032877 0.7486428 0.3253749 -0.5406841 #> [3,] 0.6278918 -0.1694278 -0.6515236 -0.3905934 #> [4,] 0.1237745 0.6192880 -0.4289653 0.6458723 #> #>
cov_eigen(x = iris[, -5], y = iris[, 5], pool = TRUE)
#> eigen() decomposition #> $values #> [1] 0.43469460 0.08445964 0.05424531 0.02191645 #> #> $vectors #> [,1] [,2] [,3] [,4] #> [1,] -0.7377526 0.05608598 0.6323782 0.2295067 #> [2,] -0.3205660 -0.87323191 -0.1805701 -0.3195276 #> [3,] -0.5728512 0.45883202 -0.5818222 -0.3504249 #> [4,] -0.1574803 -0.15425166 -0.4785136 0.8499595 #>
cov_eigen(x = iris[, -5], y = iris[, 5], pool = TRUE, fast = TRUE)
#> $vectors #> [,1] [,2] [,3] [,4] #> [1,] -0.7377526 0.05608598 0.6323782 0.2295067 #> [2,] -0.3205660 -0.87323191 -0.1805701 -0.3195276 #> [3,] -0.5728512 0.45883202 -0.5818222 -0.3504249 #> [4,] -0.1574803 -0.15425166 -0.4785136 0.8499595 #> #> $values #> [1] 0.43469460 0.08445964 0.05424531 0.02191645 #>
# Generates a data set having fewer observations than features. # We apply the Fast SVD to compute the eigendecomposition corresponding to the # nonzero eigenvalues of the covariance matrices. set.seed(42) n <- 5 p <- 20 num_classes <- 3 x <- lapply(seq_len(num_classes), function(k) { replicate(p, rnorm(n, mean = k)) }) x <- do.call(rbind, x) colnames(x) <- paste0("x", 1:ncol(x)) y <- gl(num_classes, n) cov_eigen(x = x, y = y, fast = TRUE)
#> $`1` #> $`1`$vectors #> [,1] [,2] [,3] [,4] #> x1 0.06822739 -0.047327043 0.04508787 -0.408039456 #> x2 0.16141748 0.022005386 -0.36585116 0.294470422 #> x3 -0.33250044 0.107910346 -0.49288794 -0.049903718 #> x4 -0.52748931 -0.368955136 -0.10592303 -0.328033545 #> x5 0.21595117 -0.511731457 0.18471133 -0.125893365 #> x6 0.06652260 -0.118780253 -0.33342060 -0.031322001 #> x7 -0.14763914 0.109115391 0.17103777 0.054320506 #> x8 -0.23564690 -0.106764192 0.22560470 0.247665644 #> x9 0.02709602 0.307204062 0.13163122 -0.137531351 #> x10 0.01103063 0.008527884 0.39319829 -0.155966708 #> x11 0.20026005 0.067350810 0.27656791 -0.121333264 #> x12 -0.49699809 0.142715275 0.23312130 0.007526242 #> x13 0.26024197 0.117252669 -0.09329074 0.147825963 #> x14 0.04763435 0.016101152 0.05616179 -0.196083389 #> x15 -0.02473719 0.143602891 0.19616521 0.254477170 #> x16 -0.13098654 0.335154785 -0.01503575 -0.060382151 #> x17 -0.03269820 0.280287502 -0.12871667 -0.397108714 #> x18 0.06831451 -0.182827220 -0.05757876 -0.159504067 #> x19 0.26205796 0.202840821 -0.09104824 -0.430941034 #> x20 0.04426259 -0.361636160 -0.05494141 -0.001897225 #> #> $`1`$values #> [1] 6.093435 4.570241 3.832211 2.028293 #> #> #> $`2` #> $`2`$vectors #> [,1] [,2] [,3] [,4] #> x1 -0.39520889 0.27282358 0.16295294 -0.11481037 #> x2 -0.01680655 -0.07514555 0.10714012 -0.03449432 #> x3 -0.04816103 0.34662853 0.02613723 0.10377105 #> x4 0.56168976 0.03872310 -0.03180470 0.04005698 #> x5 0.21561970 -0.23229479 -0.02353895 -0.21684704 #> x6 -0.16639287 -0.24799888 -0.10346626 0.42213112 #> x7 -0.07688968 -0.34093548 0.19362126 -0.13057603 #> x8 0.26126597 0.01315439 -0.16091841 -0.29143533 #> x9 -0.02407679 -0.03855309 0.17856700 -0.05025917 #> x10 -0.14200545 0.20773787 0.38853798 0.05342185 #> x11 0.27193660 -0.01084018 0.33207258 -0.15364162 #> x12 -0.03184207 -0.38789400 -0.33531368 -0.03995662 #> x13 0.11419144 -0.25185686 0.12039026 0.01935835 #> x14 0.07090007 -0.21825345 0.37989697 0.33782219 #> x15 -0.43185403 -0.19501853 -0.14367622 -0.32959225 #> x16 0.13135962 -0.13125056 0.32132253 -0.33894961 #> x17 0.05319742 0.02423565 0.19101822 -0.13320947 #> x18 0.15115144 0.39595330 -0.30829880 -0.22851152 #> x19 0.14820083 0.18878023 0.05284989 0.36183548 #> x20 -0.11042459 0.10883427 0.25107409 -0.27670912 #> #> $`2`$values #> [1] 6.004244 3.011224 2.630152 1.023753 #> #> #> $`3` #> $`3`$vectors #> [,1] [,2] [,3] [,4] #> x1 0.485783625 -0.27618348 -0.16364823 0.07356609 #> x2 -0.007126224 0.07165659 -0.14546994 0.08522926 #> x3 -0.045804750 -0.27500883 -0.43877596 0.10721075 #> x4 0.104728995 -0.27266685 0.30086816 0.07691423 #> x5 -0.184279078 0.09469671 0.05002101 0.04678904 #> x6 -0.170863031 0.04692255 -0.18664537 0.19870840 #> x7 -0.111720989 -0.07897317 -0.24376586 -0.25185686 #> x8 0.048562183 -0.27606996 -0.15403780 -0.23665302 #> x9 0.281672076 -0.35514491 -0.02378928 -0.09239347 #> x10 -0.032067155 -0.09152817 0.20368893 -0.62711674 #> x11 -0.028389336 0.20608184 -0.40535611 0.14526251 #> x12 -0.005648856 -0.06437072 0.02981666 0.26291524 #> x13 -0.306014482 -0.23181972 0.13110138 0.00525481 #> x14 -0.444028074 -0.18764701 0.14086248 -0.01469320 #> x15 0.043343672 -0.01053781 0.10433006 -0.16673174 #> x16 -0.046984219 0.33441276 -0.17597575 -0.39042277 #> x17 0.073441577 0.34469097 0.22100908 -0.12675963 #> x18 -0.433297311 -0.31318596 -0.27190872 -0.17974719 #> x19 0.271759234 0.10403492 -0.32734095 -0.29492442 #> x20 0.172666775 -0.25545341 0.18014582 -0.03058313 #> #> $`3`$values #> [1] 7.862899 4.707142 3.641771 2.108191 #> #>
cov_eigen(x = x, y = y, pool = TRUE, fast = TRUE)
#> $vectors #> [,1] [,2] [,3] [,4] [,5] #> [1,] -0.520633204 0.19355287 0.25337184 0.1489894950 0.17300532 #> [2,] -0.006709333 -0.16348489 0.16166468 0.0836517287 -0.19208772 #> [3,] -0.005387588 0.24837798 0.27735499 0.1421855393 -0.53971092 #> [4,] 0.285663063 0.66247008 0.02857967 -0.3140182044 0.05986995 #> [5,] 0.311466323 -0.09246409 0.10233170 -0.0716768997 0.37647872 #> [6,] 0.107233218 -0.14918472 0.11899433 0.2722274810 -0.22910245 #> [7,] 0.035835852 -0.04912230 -0.24691447 0.0000133816 -0.06185875 #> [8,] 0.119137928 0.30797205 -0.06868620 -0.0970577190 0.05775038 #> [9,] -0.271074846 0.12181653 -0.02325866 -0.0799189865 0.07347017 #> [10,] -0.099046166 -0.04317731 -0.11828426 -0.1046450386 0.40745163 #> [11,] 0.038028959 -0.21033596 0.10717585 -0.4049152568 0.01562800 #> [12,] 0.062999725 0.28080542 -0.50231505 0.0949340102 -0.20697072 #> [13,] 0.249498504 -0.22630127 0.02652012 0.0960206048 0.06678320 #> [14,] 0.331060453 -0.17195931 -0.05674191 0.0203426678 0.11071839 #> [15,] -0.196985176 -0.08699297 -0.34303348 0.3224992008 0.08241733 #> [16,] -0.030126522 -0.16120053 -0.22469503 -0.4047950587 -0.24366227 #> [17,] -0.127035606 -0.10028307 -0.11640260 -0.3345588330 -0.10745505 #> [18,] 0.365137703 0.02405592 0.36490037 0.1217262970 -0.01969686 #> [19,] -0.255552428 -0.10438348 0.33514455 -0.3829687931 -0.09580779 #> [20,] -0.090755418 0.17754685 0.17934040 0.1486531623 0.34412416 #> [,6] [,7] [,8] [,9] [,10] #> [1,] 0.1200167973 0.01916348 -0.16465282 -0.32359182 0.152231561 #> [2,] -0.1167671379 -0.03910181 -0.16245812 0.07677784 -0.464079183 #> [3,] 0.3089011617 -0.12442847 0.05695591 -0.20742026 -0.152121143 #> [4,] -0.1443427377 -0.26760497 -0.09468430 -0.04722869 -0.036163005 #> [5,] -0.2261029780 0.23498400 -0.08511210 -0.31491537 -0.009516163 #> [6,] -0.1313750503 -0.08204362 -0.17447474 -0.34347451 0.120278613 #> [7,] 0.3399469782 0.13117279 -0.20308297 -0.32248667 -0.177440840 #> [8,] 0.2282928267 0.39449595 -0.02895784 0.21549663 -0.266749874 #> [9,] 0.3053753356 -0.06467331 -0.43568500 0.31296333 0.071124664 #> [10,] 0.4154469124 -0.20699267 0.34019015 -0.14011009 -0.009612538 #> [11,] 0.0810676263 0.37993712 -0.28595311 -0.24157217 0.012549600 #> [12,] -0.0009201592 0.16223615 -0.17259611 -0.11790918 0.356451709 #> [13,] 0.1774978308 -0.21949436 -0.36082072 0.39827362 -0.076170260 #> [14,] 0.3107322630 -0.41969494 -0.25627444 -0.20218334 0.198585508 #> [15,] 0.0961854639 0.11231117 0.13206837 0.03574387 -0.079800667 #> [16,] 0.1727212101 -0.03013935 0.13689310 -0.18061789 -0.355806218 #> [17,] -0.1213217521 -0.38440277 0.21561291 -0.04197374 0.090214828 #> [18,] 0.4006007751 0.21022839 0.38489793 0.08439275 0.220985946 #> [19,] 0.0645099380 0.07032068 -0.07521496 0.11764702 0.278236702 #> [20,] -0.0267269141 -0.15419244 -0.07119693 -0.17491470 -0.418149839 #> [,11] [,12] #> [1,] -0.087014495 -0.26006131 #> [2,] 0.063351931 -0.01184857 #> [3,] -0.166631969 0.06338909 #> [4,] 0.133787010 -0.03648910 #> [5,] 0.203367339 -0.29320414 #> [6,] 0.422094393 0.08177505 #> [7,] 0.292107179 -0.02950714 #> [8,] 0.181526503 0.10002175 #> [9,] 0.015535995 -0.17501458 #> [10,] 0.099748680 0.37259810 #> [11,] -0.500589969 0.06404705 #> [12,] -0.030408750 -0.12703399 #> [13,] 0.022250558 -0.23751102 #> [14,] -0.102386278 0.12455113 #> [15,] 0.133091816 -0.26147292 #> [16,] 0.105756183 -0.25447839 #> [17,] -0.052948676 -0.47469711 #> [18,] -0.005402319 -0.41228084 #> [19,] 0.547104229 0.09097828 #> [20,] -0.034808155 -0.15813427 #> #> $values #> [1] 3.2652328 2.7134904 1.9065015 1.8289753 1.7401791 1.4284267 0.9237792 #> [8] 0.6743806 0.5000194 0.4497832 0.2278324 0.1792512 #>