R Under development (unstable) (2024-02-05 r85863 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 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. 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. > ## this will render the output independent from the version of the package > suppressPackageStartupMessages(library(rrcov3way)) > > set.seed(123456) > > ## Example with the UNIDO Manufacturing value added data > data(va3way) > dim(va3way) [1] 49 5 14 > > ## Treat quickly and dirty the zeros in the data set (if any) > va3way[va3way==0] <- 0.001 > > ## IGNORE_RDIFF_BEGIN > res <- Parafac(va3way, trace=TRUE) # tracing Candecomp/Parafac function value at Start is 3.39457362294427e+24 f= 1.97957920221784e+23 after 50 iters; diff.= 8945743210706108416 f= 1.97722009973095e+23 after 100 iters; diff.= 2576101314060615680 f= 1.97634185241095e+23 after 150 iters; diff.= 1216969433998688256 f= 1.97588048976323e+23 after 200 iters; diff.= 707595978248552448 f= 1.97559562967186e+23 after 250 iters; diff.= 4.6278293851e+17 f= 1.9754019735921e+23 after 300 iters; diff.= 326500214426828800 f= 1.97526160802808e+23 after 350 iters; diff.= 242856346825785344 Candecomp/Parafac function value is 1.97517430216866e+23 after 390 iterations Fit percentage is 94.18 % > ## IGNORE_RDIFF_END > > ## Using robustness with clr transformation > try(res <- Parafac(va3way, robust=TRUE, coda.transform="clr")) Error in Parafac(va3way, robust = TRUE, coda.transform = "clr") : The robust option is not possible with 'clr' transform compositional data. Please use 'ilr'. > > ## Rejected values of parameter 'crit' > try(res <- Parafac(va3way, crit=c(1:10))) # length different than 1 Error in Parafac(va3way, crit = c(1:10)) : 'crit' has to be a single positive number less than 1! > try(res <- Parafac(va3way, crit=-1)) # crit non-positive Error in Parafac(va3way, crit = -1) : 'crit' has to be a single positive number less than 1! > try(res <- Parafac(va3way, crit=2)) # crit >= 1 Error in Parafac(va3way, crit = 2) : 'crit' has to be a single positive number less than 1! > > res <- Parafac(va3way, crit=0.2) # crit < 0.5 --> crit=1-crit > > ## Test cp_als(): the input array > try(rrcov3way:::cp_als(va3way)) # missing ncomp Error in rrcov3way:::cp_als(va3way) : Number of factors to extract 'ncomp' must be provided! > > set.seed(98765) > rrcov3way:::cp_als(va3way, ncomp=2) # OK, 3-way array $fit [1] 1.975173e+23 $fp [1] 94.18138 $ss [1] 3.394574e+24 $A [,1] [,2] [1,] 75325306 184384923 [2,] -8222132158 -706325609 [3,] -10028635776 -2304869333 [4,] -9235645113 49236012457 [5,] 144204886 1162123010 [6,] -9209273813 22182444692 [7,] -15970577022 -5563332563 [8,] 2433687877 9337493282 [9,] 438478268 1340749311 [10,] 184203532 536581878 [11,] -5397073344 -910008700 [12,] -6232934360 -2616449170 [13,] 1270743122 3238798585 [14,] 113709848 611006478 [15,] -3816120588 930010937 [16,] -39994050458 -8031850169 [17,] 84247398 219875097 [18,] 175740842 415243568 [19,] -164239813087 -118774189733 [20,] -4584857904 -2257019456 [21,] -8658993135 13953903478 [22,] 1274473021 17017890520 [23,] -5952142672 1026172289 [24,] -2129113144 589425432 [25,] -31133743039 7226820838 [26,] 206665027 1079920182 [27,] -58368100072 -24155348523 [28,] 215145457 668876766 [29,] 200411814 877350610 [30,] 300673966 653811419 [31,] -181139313 853856931 [32,] -11024715690 -1554598944 [33,] -2531525584 1306423795 [34,] 2774399686 7518193077 [35,] -3475833077 7297305407 [36,] 410954020 4829122549 [37,] -1295493028 207181313 [38,] -290912976 2503807639 [39,] 6477968044 43767955751 [40,] 50996412 193932515 [41,] -10497983292 -9500681476 [42,] -1077176286 316975234 [43,] -1416666704 -465133849 [44,] -11782349711 11394183501 [45,] -12425813012 -5924237992 [46,] -20709472397 -14016083131 [47,] -599892851 11440591554 [48,] -35925845820 -6985682064 [49,] 543484639 1141458655 $B [,1] [,2] [1,] -2.0128950 1.5744456 [2,] -0.5515332 0.2866199 [3,] -2.5352626 1.7014215 [4,] -3.2963694 1.2415543 [5,] -0.9844209 0.4270625 $C [,1] [,2] [1,] 0.4513023 0.4748265 [2,] 0.4354471 0.4486442 [3,] 0.4495636 0.4582773 [4,] 0.5377859 0.5428363 [5,] 0.6248667 0.6467386 [6,] 0.6700282 0.7527217 [7,] 0.7423040 0.8659029 [8,] 0.8542403 1.0232329 [9,] 0.8970301 1.1423872 [10,] 0.7238015 0.9159853 [11,] 0.8763976 1.1605787 [12,] 0.9842029 1.3208483 [13,] 0.9215009 1.2585477 [14,] 0.9485225 1.2769873 $iter [1] 391 $tripcos Minimal triple cosine -0.7333024 $mintripcos [1] -0.7330044 $ftiter [,1] [,2] [1,] 2.044477e+23 -0.02491081 [2,] 1.987935e+23 -0.15798293 [3,] 1.983906e+23 -0.25618180 [4,] 1.981542e+23 -0.32907730 [5,] 1.980056e+23 -0.38454241 [6,] 1.979078e+23 -0.42773241 [7,] 1.978404e+23 -0.46214450 [8,] 1.977914e+23 -0.49019938 [9,] 1.977540e+23 -0.51358224 [10,] 1.977244e+23 -0.53347032 [11,] 1.977002e+23 -0.55069138 [12,] 1.976800e+23 -0.56583357 [13,] 1.976627e+23 -0.57932069 [14,] 1.976478e+23 -0.59146337 [15,] 1.976348e+23 -0.60249357 [16,] 1.976233e+23 -0.61258794 [17,] 1.976132e+23 -0.62188363 [18,] 1.976040e+23 -0.63048922 [19,] 1.975958e+23 -0.63849211 [20,] 1.975884e+23 -0.64596385 [21,] 1.975816e+23 -0.65296382 [22,] 1.975755e+23 -0.65954195 [23,] 1.975698e+23 -0.66574067 [24,] 1.975646e+23 -0.67159639 [25,] 1.975598e+23 -0.67714061 [26,] 1.975553e+23 -0.68240080 [27,] 1.975512e+23 -0.68740104 [28,] 1.975473e+23 -0.69216260 [29,] 1.975437e+23 -0.69670431 [30,] 1.975404e+23 -0.70104296 [31,] 1.975372e+23 -0.70519358 [32,] 1.975342e+23 -0.70916965 [33,] 1.975314e+23 -0.71298331 [34,] 1.975288e+23 -0.71664555 [35,] 1.975263e+23 -0.72016633 [36,] 1.975239e+23 -0.72355471 [37,] 1.975217e+23 -0.72681892 [38,] 1.975195e+23 -0.72996653 [39,] 1.975175e+23 -0.73300444 $const [1] "none" "none" "none" > rrcov3way:::cp_als(unfold(va3way), ncomp=2, + n=49, m=5, p=14) # OK, unfolded 3-way array $fit [1] 1.975174e+23 $fp [1] 94.18138 $ss [1] 3.394574e+24 $A [,1] [,2] [1,] -156564243 -11456778 [2,] 599513414 1250657041 [3,] 1956821842 1525434201 [4,] -41807974113 1405061113 [5,] -986787462 -21929308 [6,] -18836037766 1400918173 [7,] 4723534010 2429245748 [8,] -7928658980 -370141270 [9,] -1138455151 -66690074 [10,] -455621198 -28016430 [11,] 772551768 820939162 [12,] 2221514693 948073091 [13,] -2750117993 -193276005 [14,] -518819361 -17293346 [15,] -789811887 580471006 [16,] 6818854768 6083415859 [17,] -186699695 -12813739 [18,] -352589852 -26729780 [19,] 100849540296 24981781676 [20,] 1916361413 697387429 [21,] -11848915345 1317177994 [22,] -14450331668 -193776767 [23,] -871533105 905379297 [24,] -500558962 323860508 [25,] -6137440276 4735757215 [26,] -916984791 -31430449 [27,] 20509285255 8878204475 [28,] -567955637 -32722343 [29,] -744977018 -30480210 [30,] -555160662 -45732074 [31,] -725038606 27557124 [32,] 1319722255 1676951229 [33,] -1109394850 385074466 [34,] -6383828493 -421975245 [35,] -6196452463 528740681 [36,] -4100531837 -62486577 [37,] -175959890 197057446 [38,] -2126061422 44262650 [39,] -37164427796 -985148404 [40,] -164672091 -7756087 [41,] 8066991941 1596793500 [42,] -269184257 163849842 [43,] 394915428 215485519 [44,] -9675485438 1792255555 [45,] 5030059762 1890049308 [46,] 11900839685 3150031372 [47,] -9714550026 91303871 [48,] 5930656686 5464611411 [49,] -969228878 -82663471 $B [,1] [,2] [1,] -1.2824706 1.9007237 [2,] -0.2334633 0.5208042 [3,] -1.3858919 2.3939934 [4,] -1.0112777 3.1127308 [5,] -0.3478557 0.9295767 $C [,1] [,2] [1,] 0.6864742 3.141965 [2,] 0.6486208 3.031583 [3,] 0.6625473 3.129863 [4,] 0.7847967 3.744067 [5,] 0.9350131 4.350321 [6,] 1.0882414 4.664728 [7,] 1.2518745 5.167906 [8,] 1.4793353 5.947201 [9,] 1.6516066 6.245093 [10,] 1.3242857 5.039082 [11,] 1.6779094 6.101444 [12,] 1.9096206 6.851978 [13,] 1.8195509 6.415446 [14,] 1.8462089 6.603572 $iter [1] 373 $tripcos Minimal triple cosine -0.73327 $mintripcos [1] -0.7323726 $ftiter [,1] [,2] [1,] 2.003601e+23 -0.2336666 [2,] 1.982150e+23 -0.3105052 [3,] 1.980420e+23 -0.3706370 [4,] 1.979306e+23 -0.4172073 [5,] 1.978553e+23 -0.4540189 [6,] 1.978017e+23 -0.4837758 [7,] 1.977617e+23 -0.5083737 [8,] 1.977304e+23 -0.5291408 [9,] 1.977051e+23 -0.5470095 [10,] 1.976840e+23 -0.5626396 [11,] 1.976662e+23 -0.5765030 [12,] 1.976508e+23 -0.5889431 [13,] 1.976374e+23 -0.6002138 [14,] 1.976257e+23 -0.6105068 [15,] 1.976152e+23 -0.6199697 [16,] 1.976059e+23 -0.6287183 [17,] 1.975975e+23 -0.6368453 [18,] 1.975899e+23 -0.6444258 [19,] 1.975830e+23 -0.6515221 [20,] 1.975767e+23 -0.6581863 [21,] 1.975710e+23 -0.6644623 [22,] 1.975657e+23 -0.6703879 [23,] 1.975608e+23 -0.6759956 [24,] 1.975562e+23 -0.6813137 [25,] 1.975520e+23 -0.6863670 [26,] 1.975481e+23 -0.6911772 [27,] 1.975445e+23 -0.6957639 [28,] 1.975411e+23 -0.7001441 [29,] 1.975378e+23 -0.7043332 [30,] 1.975348e+23 -0.7083450 [31,] 1.975320e+23 -0.7121920 [32,] 1.975293e+23 -0.7158853 [33,] 1.975268e+23 -0.7194351 [34,] 1.975244e+23 -0.7228507 [35,] 1.975221e+23 -0.7261405 [36,] 1.975200e+23 -0.7293121 [37,] 1.975179e+23 -0.7323726 $const [1] "none" "none" "none" > > try(rrcov3way:::cp_als("abc", ncomp=2)) # error, not an array or matrix Error in rrcov3way:::cp_als("abc", ncomp = 2) : 'X' must be three dimensional array or a matrix! > > try(rrcov3way:::cp_als(unfold(va3way), ncomp=2))# missing dimensions Error in rrcov3way:::cp_als(unfold(va3way), ncomp = 2) : The three dimensions of the matricisized array must be provided! > try(rrcov3way:::cp_als(unfold(va3way), ncomp=2, + n=50, m=5, p=14)) # n != dim(Xa)[1] Error in rrcov3way:::cp_als(unfold(va3way), ncomp = 2, n = 50, m = 5, : 'n' must be equal to the first dimension of the matrix 'X'! > try(rrcov3way:::cp_als(unfold(va3way), ncomp=2, + n=49, m=1, p=14)) # m*p != dim(Xa)[2] Error in rrcov3way:::cp_als(unfold(va3way), ncomp = 2, n = 49, m = 1, : 'm*p' must be equal to the second dimension of the matrix 'X'! > > ## Test cp_als(): the constraints > try(Parafac(va3way, const="abc")) # wrong constraint Error in cp_als(X, ncomp = ncomp, const = const, conv = conv, start = start, : All elements of 'const' must be one of 'none', 'orth', 'nonneg' or 'zerocor' > res <- Parafac(va3way, const=c("none", "none")) # length of const < 3 > res$const [1] "none" "none" "none" > > ## Test cp_als(): the initial values > try(Parafac(va3way, start=c(1:2))) # wrong start Error in cp_als(X, ncomp = ncomp, const = const, conv = conv, start = start, : 'start' must be either a list with elements A, B and C or a single character - one of 'random' or 'svd'! > try(Parafac(va3way, start="abc")) # wrong start Error in cp_als(X, ncomp = ncomp, const = const, conv = conv, start = start, : 'start' must be either a list with elements A, B and C or one of 'random' or 'svd'! > > Parafac(va3way, start="svd") Call: Parafac(X = va3way, start = "svd") PARAFAC analysis with 2 components. Fit value: 1.975174e+23 Fit percentage: 94.18 % > Parafac(va3way, const="nonneg", start="svd") Call: Parafac(X = va3way, const = "nonneg", start = "svd") PARAFAC analysis with 2 components. Fit value: 2.026874e+23 Fit percentage: 94.03 % > Parafac(va3way, const="orth", start="svd") Call: Parafac(X = va3way, const = "orth", start = "svd") PARAFAC analysis with 2 components. Fit value: 3.394574e+24 Fit percentage: 0 % > Parafac(va3way, const="zerocor", start="svd") Call: Parafac(X = va3way, const = "zerocor", start = "svd") PARAFAC analysis with 2 components. Fit value: 2.514353e+24 Fit percentage: 25.93 % > > set.seed(12345) > n <- 49 > m <- 5 > p <- 14 > r <- 2 > > A <- matrix(runif(max(n,r) * r), max(n,r))[1:n, , drop=FALSE] > B <- matrix(runif(max(m,r) * r), max(m,r))[1:m, , drop=FALSE] > C <- matrix(runif(max(p,r) * r), max(p,r))[1:p, , drop=FALSE] > Parafac(va3way, const="nonneg", start=list(A=A, B=B, C=C)) Call: Parafac(X = va3way, const = "nonneg", start = list(A = A, B = B, C = C)) PARAFAC analysis with 2 components. Fit value: 2.026874e+23 Fit percentage: 94.03 % > > A <- pracma::orth(matrix(rnorm(max(n,r) * r), max(n,r)))[1:n, , drop=FALSE] > B <- pracma::orth(matrix(rnorm(max(m,r) * r), max(m,r)))[1:m, , drop=FALSE] > C <- pracma::orth(matrix(rnorm(max(p,r) * r), max(p,r)))[1:p, , drop=FALSE] > Parafac(va3way, start=list(A=A, B=B, C=C)) Call: Parafac(X = va3way, start = list(A = A, B = B, C = C)) PARAFAC analysis with 2 components. Fit value: 1.975173e+23 Fit percentage: 94.18 % > try(Parafac(va3way, const="nonneg", start=list(A=A, B=B, C=C))) Error in cp_als(X, ncomp = ncomp, const = const, conv = conv, start = start, : Error in nonnegative LS for mode A at iter= 0 > > proc.time() user system elapsed 2.10 0.28 2.37