This function generates K multivariate normal data sets, where each class is generated with a constant mean vector and a covariance matrix consisting of block-diagonal autocorrelation matrices. The data are returned as a single matrix x along with a vector of class labels y that indicates class membership.

generate_blockdiag(n, mu, num_blocks, block_size, rho, sigma2 = rep(1, K))

Arguments

n

vector of the sample sizes of each class. The length of n determines the number of classes K.

mu

matrix containing the mean vectors for each class. Expected to have p rows and K columns.

num_blocks

the number of block matrices. See details.

block_size

the dimensions of the square block matrix. See details.

rho

vector of the values of the autocorrelation parameter for each class covariance matrix. Must equal the length of n (i.e., equal to K).

sigma2

vector of the variance coefficients for each class covariance matrix. Must equal the length of n (i.e., equal to K).

Value

named list with elements:

  • x: matrix of observations with n rows and p columns

  • y: vector of class labels that indicates class membership for each observation (row) in x.

Details

For simplicity, we assume that a class mean vector is constant for each feature. That is, we assume that the mean vector of the \(k\)th class is \(c_k * j_p\), where \(j_p\) is a \(p \times 1\) vector of ones and \(c_k\) is a real scalar.

The \(k\)th class covariance matrix is defined as $$\Sigma_k = \Sigma^{(\rho)} \oplus \Sigma^{(-\rho)} \oplus \ldots \oplus \Sigma^{(\rho)},$$ where \(\oplus\) denotes the direct sum and the \((i,j)\)th entry of \(\Sigma^{(\rho)}\) is $$\Sigma_{ij}^{(\rho)} = \{ \rho^{|i - j|} \}.$$

The matrix \(\Sigma^{(\rho)}\) is referred to as a block. Its dimensions are provided in the block_size argument, and the number of blocks are specified in the num_blocks argument.

Each matrix \(\Sigma_k\) is generated by the cov_block_autocorrelation() function.

The number of classes K is determined with lazy evaluation as the length of n.

The number of features p is computed as block_size * num_blocks.

Examples

# Generates data from K = 3 classes. means <- matrix(rep(1:3, each=9), ncol=3) data <- generate_blockdiag(n = c(15, 15, 15), block_size = 3, num_blocks = 3, rho = seq(.1, .9, length = 3), mu = means) data$x
#> [,1] [,2] [,3] [,4] [,5] [,6] #> [1,] 0.4837275 0.38971795 -0.686043277 0.60265943 0.6046112 3.67142428 #> [2,] -0.4660931 1.00103611 0.005328557 0.97446250 0.5420383 0.36568044 #> [3,] 1.5422629 0.53674065 0.977550113 2.18935088 2.4375404 -0.01952462 #> [4,] 0.9399582 0.86710067 0.108935161 0.55284913 0.9277010 0.58350982 #> [5,] 1.6430146 -0.02091586 0.909150651 -0.49226202 2.0729881 0.77923156 #> [6,] 0.1788846 0.49221281 2.256376014 0.77171842 -0.0697155 1.10875326 #> [7,] 0.0316075 1.40812238 1.103796579 1.88609142 0.8574065 1.82744038 #> [8,] 0.7793774 -0.42076834 1.569183188 1.48283697 1.0253929 1.15276186 #> [9,] 0.6569110 -0.31181732 1.632006801 1.50569684 0.1137671 -0.63231702 #> [10,] -0.3362567 0.03281909 2.031591831 1.43940715 1.6959700 2.84378569 #> [11,] 2.2676593 3.04410705 -0.267754438 -0.18865682 0.1856995 -0.09234226 #> [12,] 3.0394829 1.20514848 0.929034699 1.49637104 1.0554982 0.87180979 #> [13,] 1.3656479 0.64256058 0.462732621 -0.04602268 1.3651860 0.84360849 #> [14,] 2.2386351 0.83882633 1.937865670 -0.22438824 0.4615037 0.70316993 #> [15,] 1.1940619 0.03573754 0.225498233 2.06442690 2.3676785 2.31396455 #> [16,] 2.0561598 2.40786546 1.238053481 0.83998631 1.4709132 0.75135760 #> [17,] 1.2014991 1.60354502 0.959743424 1.94372192 2.9630203 3.11530618 #> [18,] 3.2108122 2.97941041 2.123439621 -0.52956352 1.5242695 2.30552783 #> [19,] 1.9919861 3.24348031 2.742780920 1.26627385 3.2823550 3.38554991 #> [20,] 2.6569098 2.28732009 2.407926161 3.95867892 1.3215151 -0.00147656 #> [21,] 1.7597395 3.83397260 2.356152751 1.58239458 1.9410505 2.74278106 #> [22,] 2.8134802 2.41113107 2.791682289 1.89129056 2.0300320 2.43588379 #> [23,] 1.6409675 1.01337087 0.806641036 1.49904475 2.2863787 1.68472865 #> [24,] 2.9043549 1.85629418 2.162391955 2.22491351 2.4659191 3.74771154 #> [25,] 1.2508665 1.91353991 2.620899208 1.41152733 1.2096736 1.91233497 #> [26,] 0.4976387 0.82198537 1.172764183 2.10512505 2.5127239 1.74729204 #> [27,] 2.7016872 0.73855711 1.775015759 3.40411140 2.8076798 1.51105372 #> [28,] 3.1516671 2.23383608 2.840774281 1.24201700 0.2402703 0.75913163 #> [29,] 1.9752283 2.20822600 3.746735138 0.59497838 1.3522796 2.09760162 #> [30,] 0.7112391 2.76741383 1.537423300 1.46876978 0.9268473 1.82410035 #> [31,] 3.9697129 4.17522638 3.672611238 2.90138974 2.9500023 3.25707917 #> [32,] 2.7894530 2.97358119 2.681497456 3.45236670 3.8968922 3.76676083 #> [33,] 3.6848267 3.00165783 3.127138031 2.40130943 2.1217621 1.94458157 #> [34,] 1.0227487 1.43279340 1.719179816 1.02664123 1.2936484 1.41275480 #> [35,] 3.4979760 4.21476546 4.120373992 1.66062182 2.1133580 2.03538190 #> [36,] 3.3632290 3.24261564 3.100381005 4.20111346 3.3304433 3.11545227 #> [37,] 0.7850060 1.28705101 1.172833573 4.91790570 4.8493804 4.43518623 #> [38,] 2.5707094 2.82535557 2.695206293 2.55355285 2.6664750 2.10664450 #> [39,] 3.5288912 3.56953688 4.188690946 4.67271937 5.1815008 4.01484548 #> [40,] 1.6450799 1.72231937 2.101744553 3.61803270 3.1725941 2.11530547 #> [41,] 2.4559857 1.55108692 2.215705781 2.35122044 2.3159044 2.39811115 #> [42,] 0.2390692 0.33076314 0.437526645 3.59669581 3.3015337 3.38215654 #> [43,] 2.7763010 2.79639767 2.822620660 3.93249742 3.4249583 3.69939077 #> [44,] 3.8096861 4.19394146 4.213318628 2.79731712 2.7628440 2.48448013 #> [45,] 2.8212038 3.57611376 3.839348815 2.10649507 1.9202212 2.24816517 #> [,7] [,8] [,9] #> [1,] -0.3591482 0.9941251 -0.4899978 #> [2,] -1.0826947 -0.3139406 1.1104510 #> [3,] 0.9411394 2.1691383 0.5904959 #> [4,] 2.0863094 0.5542221 0.5475066 #> [5,] 0.4708012 -0.2589332 0.9285783 #> [6,] 0.6725970 1.6420210 2.4587589 #> [7,] -0.6551406 2.6412159 1.9416112 #> [8,] 0.4361725 1.3534108 1.3105309 #> [9,] 1.1883948 0.6786544 1.2358091 #> [10,] 1.0298659 -0.9727906 1.2337943 #> [11,] 0.3412894 0.7227375 -0.2114046 #> [12,] 2.4592587 0.7931723 -0.2872943 #> [13,] 1.5008283 0.7590796 0.3325428 #> [14,] 0.6768765 2.3246444 1.0432315 #> [15,] -0.2725840 3.0266411 2.1129082 #> [16,] 2.4077085 3.0020632 1.3477863 #> [17,] 1.9005463 0.3512341 1.5213628 #> [18,] 2.1255017 2.2668094 2.4595812 #> [19,] 2.3218354 0.5090887 1.1151326 #> [20,] 2.1346881 1.5373583 2.2839439 #> [21,] 1.6859912 0.5030966 1.5976901 #> [22,] 2.6408584 1.0748832 1.7519429 #> [23,] 1.1647448 1.6462745 0.7734516 #> [24,] 1.4951166 2.8722590 2.2372148 #> [25,] 2.7990228 1.8314765 0.7416726 #> [26,] 1.1059882 2.3576352 2.3091892 #> [27,] 2.4430431 1.2545896 1.4160010 #> [28,] 0.7974974 1.1901239 1.4489292 #> [29,] 3.7111384 2.8012901 3.3740021 #> [30,] 4.2219093 4.4425230 1.7431991 #> [31,] 4.5625114 4.3202036 4.0929528 #> [32,] 3.4156462 3.3584642 3.7164174 #> [33,] 3.8479380 4.1821011 4.4927390 #> [34,] 1.4713737 1.1232186 1.8793616 #> [35,] 3.5337524 3.1950885 2.9037587 #> [36,] 2.0663453 2.6621765 3.1283009 #> [37,] 4.0903680 3.4672814 3.8817135 #> [38,] 2.7304916 2.8210357 2.6639953 #> [39,] 4.7486971 5.0931624 4.5249174 #> [40,] 1.8976859 1.9807971 1.5673776 #> [41,] 3.3558694 3.3384170 3.1203151 #> [42,] 1.7032703 2.1318134 2.6279582 #> [43,] 3.2084309 2.5883581 2.8695959 #> [44,] 2.0029741 1.6833399 1.6678482 #> [45,] 5.1264518 4.7724621 3.3995850
data$y
#> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 #> [39] 3 3 3 3 3 3 3 #> Levels: 1 2 3
# Generates data from K = 4 classes. Notice that we use specify a variance. means <- matrix(rep(1:4, each=9), ncol=4) data <- generate_blockdiag(n = c(15, 15, 15, 20), block_size = 3, num_blocks = 3, rho = seq(.1, .9, length = 4), mu = means) data$x
#> [,1] [,2] [,3] [,4] [,5] [,6] #> [1,] 0.72006295 2.50968486 1.0371282 2.5809962 0.77736992 1.5935435 #> [2,] -0.23382545 0.35581511 1.3350452 -0.9930424 -0.95103488 -0.2177700 #> [3,] 1.15629815 0.90841191 0.2930293 2.3048003 -0.03708009 0.7221426 #> [4,] 1.58515231 2.44275865 1.8331853 0.3854451 1.11891453 2.3760803 #> [5,] 1.91531856 1.80723085 1.3414790 2.4180779 -0.40149845 1.8389022 #> [6,] 1.96409560 1.04854307 -0.5591542 -0.9785244 1.43555907 0.9462257 #> [7,] 0.78884642 -0.03599171 -1.0276713 0.8399892 2.12554614 2.9264076 #> [8,] 1.02523559 0.48200363 -0.7566558 -0.2472712 1.26912356 -0.4422473 #> [9,] 0.99759601 0.99468297 1.1511059 1.5905145 0.89160232 0.7477137 #> [10,] -1.11709933 1.90349702 0.7950314 1.4743093 2.04807568 1.8854857 #> [11,] 1.83003035 0.43700425 -0.9372898 -0.8428592 1.26245142 3.2259908 #> [12,] 0.39523156 1.93648983 1.3977478 1.2592017 2.04631001 1.7973434 #> [13,] 1.61008726 0.62252596 1.4323031 1.3557646 0.77196504 1.4527894 #> [14,] 2.35493243 0.37043258 0.1957381 1.4655394 0.09217967 0.5074039 #> [15,] 1.57834173 -0.63303941 -0.4305290 -1.9716318 1.69081490 1.2810118 #> [16,] 1.08500017 2.58075637 3.3261639 0.4008984 1.08497468 1.5247416 #> [17,] 3.28108435 2.41581338 2.7891009 3.6365149 2.74042491 3.1858784 #> [18,] 2.78584765 2.57253830 1.9559505 0.8761290 2.23276765 1.1563916 #> [19,] 2.17221296 1.80385171 0.7091649 3.1109502 1.86402996 3.1298195 #> [20,] 1.53802692 1.42892183 1.0546089 0.8804292 2.22189581 1.2587978 #> [21,] -0.19742163 1.34743647 1.9981726 2.3099628 2.24975492 1.1046577 #> [22,] 0.63983961 0.81537129 2.3514292 1.7737748 2.01730654 3.6364144 #> [23,] 1.75412872 3.16723533 1.8560818 1.1977149 0.81912669 2.0767053 #> [24,] 2.62219980 3.55643208 2.9297257 0.7798584 0.99312894 1.0845718 #> [25,] 1.32371382 1.65195119 2.6250005 1.8320472 1.52799426 2.8282417 #> [26,] 0.41089952 -0.34766386 -0.5985621 2.5273115 1.55430619 1.6651676 #> [27,] 0.03238952 1.34534013 2.0089318 -0.1610937 1.94020132 2.3509359 #> [28,] 2.58592903 2.07780268 1.9492722 2.2282344 2.66085312 1.7190757 #> [29,] 2.55493525 3.37661086 1.3495153 1.6726798 1.13727924 2.6928362 #> [30,] 0.93408918 1.66355194 1.7025011 2.3751553 0.71274306 0.6304495 #> [31,] 3.30267500 3.34484516 4.6595699 3.4432112 3.52367713 3.9629046 #> [32,] 0.49145965 1.22794531 2.3542493 2.3704711 3.51664238 3.7312447 #> [33,] 2.67337380 2.70862658 3.4257134 4.2013194 3.50473677 2.3154394 #> [34,] 0.62348118 1.42279245 2.2779663 3.8499711 3.21282074 5.7458922 #> [35,] 2.39763817 2.74043583 3.4855281 1.5344578 0.89198756 2.2836197 #> [36,] 2.91267794 3.04771009 1.3668021 2.0006486 2.83932557 2.6006232 #> [37,] 1.96548434 1.68401063 2.2177903 2.5327458 1.67646217 1.9029744 #> [38,] 2.17207504 3.08698894 2.7733371 2.0423393 2.91821059 2.6540454 #> [39,] 2.65311666 3.15153189 3.3341759 3.3230195 3.26507915 3.0969656 #> [40,] 2.89851729 4.09831928 3.4825534 2.4304656 2.89731106 2.8045737 #> [41,] 4.43795625 4.01090464 2.6993258 3.0144331 4.85991877 5.1883635 #> [42,] 3.28624352 3.26415536 3.1674590 4.4852763 3.74172305 4.5274775 #> [43,] 3.51572410 4.07404480 3.2573418 5.2182756 6.14340689 4.9547298 #> [44,] 4.10527745 1.07894985 2.4818496 2.9211736 2.72914651 1.6303762 #> [45,] 3.69107107 3.85740973 2.5449406 2.9885306 3.21826639 3.2954688 #> [46,] 3.13628972 3.85571810 4.1296241 4.2232714 3.61826511 3.7035095 #> [47,] 4.79607669 4.59881726 4.1329528 3.4338962 2.89205449 3.5748143 #> [48,] 2.71059530 3.03482492 3.9153128 3.6263187 3.33600367 3.8670246 #> [49,] 4.99728613 5.65872274 5.9287816 2.1856762 2.16102443 2.2961219 #> [50,] 5.89345346 5.85531893 5.7366763 3.4479320 3.07140914 3.0601924 #> [51,] 2.58299350 3.13807424 4.0411756 2.6087947 2.57473764 3.3340809 #> [52,] 4.83408572 4.74820515 4.5125068 1.5050001 2.80425918 2.8041752 #> [53,] 6.36901376 6.35925509 7.3497929 5.5077244 5.57234765 5.4415707 #> [54,] 4.75852675 4.46840436 3.8685800 4.5528293 3.56411920 3.6296859 #> [55,] 3.61619155 3.15128607 3.1914359 2.4477121 1.48833842 2.0060835 #> [56,] 4.67400558 4.29542271 4.5690799 1.6669728 1.62905076 1.5572283 #> [57,] 5.31587192 5.90585158 6.1819884 3.4775637 3.66411622 3.3232783 #> [58,] 4.37100588 4.32659532 4.3595986 3.7599093 3.57585139 3.6729331 #> [59,] 2.47427410 2.35716283 2.4500691 2.0012257 1.75732008 2.8425585 #> [60,] 3.61791108 4.02370358 4.0706896 4.5682705 4.49172048 4.6542891 #> [61,] 5.39392516 4.69745587 5.3269894 3.7308722 3.83264530 3.9118930 #> [62,] 3.42015394 2.97696515 3.6418286 4.2966201 4.92747348 4.6724190 #> [63,] 3.91212054 3.35665558 2.6282404 2.7106100 2.94883168 3.4629260 #> [64,] 3.57793328 3.24829624 3.3374894 2.9447068 2.96826321 3.7707887 #> [65,] 3.40855238 3.87260824 4.0325659 5.1174411 4.77161222 4.5058291 #> [,7] [,8] [,9] #> [1,] 1.3888692 4.2782215 2.0815907 #> [2,] 0.7134238 2.2046945 0.7867863 #> [3,] 0.6852507 1.5937671 2.6305134 #> [4,] -0.5436521 1.2380546 0.8537834 #> [5,] -0.5478227 1.5117504 1.1129848 #> [6,] 0.4116254 0.6275883 0.3117320 #> [7,] 1.0021851 0.9678116 1.9577473 #> [8,] 1.9790900 -0.3003157 0.7432878 #> [9,] 1.1442161 0.6523308 2.5143984 #> [10,] 0.4124189 2.4459791 -0.5452108 #> [11,] 1.3327206 2.1835567 1.8951803 #> [12,] 0.3906247 1.3182970 0.1263629 #> [13,] 1.5665002 0.8572077 2.1613895 #> [14,] 1.7660092 0.3716988 -0.9513590 #> [15,] 1.5037941 1.8636937 1.0020568 #> [16,] 0.8305026 2.3238389 1.4729123 #> [17,] 1.9306815 0.5828442 1.7555002 #> [18,] 0.8169474 2.1187659 0.8346360 #> [19,] 1.4265707 1.3517611 1.8394682 #> [20,] 0.5144688 1.1101089 1.0499065 #> [21,] 1.4626405 0.8498557 1.1745063 #> [22,] 1.8825186 1.6076982 2.6320838 #> [23,] 3.3877716 1.9731678 2.2666570 #> [24,] 2.1571449 2.0896549 2.3677438 #> [25,] 1.9328849 1.4309546 3.5109414 #> [26,] 2.1161977 1.3076280 0.3506799 #> [27,] 3.3117665 2.4341848 2.2978838 #> [28,] 1.3017411 0.8971281 0.8754272 #> [29,] 1.7391336 2.1784971 2.3581994 #> [30,] 2.6100620 1.4514038 1.2832284 #> [31,] 2.7875317 2.6824662 2.0036474 #> [32,] 2.9198625 2.4540105 1.6947576 #> [33,] 4.9188832 4.5057074 3.7987116 #> [34,] 0.4974200 1.8927351 2.2729016 #> [35,] 3.1012112 3.2006138 3.6195461 #> [36,] 3.6784441 2.6349480 3.5075160 #> [37,] 2.2758852 3.7410885 3.8002682 #> [38,] 3.9506209 3.4852200 4.1888790 #> [39,] 3.4957524 4.4789842 4.2688542 #> [40,] 2.6526348 1.9019706 1.9400741 #> [41,] 2.2795305 2.4259318 4.9112120 #> [42,] 2.7891726 3.0512140 2.1601315 #> [43,] 2.7289281 2.8685891 2.5855152 #> [44,] 3.4635728 3.6375651 4.2702726 #> [45,] 2.7251918 3.9043002 2.7886866 #> [46,] 3.4569644 4.1721506 4.1875739 #> [47,] 3.5829017 2.7972278 2.2294968 #> [48,] 2.5394152 3.3131303 3.9761110 #> [49,] 5.2604548 5.4400485 5.5570382 #> [50,] 5.7436137 6.2511987 6.3095657 #> [51,] 6.4949478 6.5103978 5.9718171 #> [52,] 3.9166910 3.2378176 2.7639895 #> [53,] 3.7308866 3.9368867 4.4558645 #> [54,] 6.4698142 5.9380870 5.8794776 #> [55,] 4.3972499 4.0182020 3.9255317 #> [56,] 4.5006888 4.5616382 5.0923976 #> [57,] 3.7092706 3.0566732 2.9401584 #> [58,] 4.8086171 4.1281048 3.5276900 #> [59,] 4.1467460 3.7825643 3.5435446 #> [60,] 4.7903120 4.8526495 4.8185695 #> [61,] 2.3272187 1.7319021 2.3861869 #> [62,] 3.5397475 3.8763627 3.9958478 #> [63,] 3.1727404 3.0103588 2.9286183 #> [64,] 2.7673808 3.1701822 3.0939973 #> [65,] 5.4167780 4.2635091 3.8451107
data$y
#> [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 #> [39] 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 #> Levels: 1 2 3 4