Running examples in ‘multiway-Ex.R’ failed
The error most likely occurred in:

> ### Name: parafac2
> ### Title: Parallel Factor Analysis-2
> ### Aliases: parafac2
> ### Keywords: models multivariate
> 
> ### ** Examples
> 
> ##########   3-way example   ##########
> 
> # create random data list with Parafac2 structure
> set.seed(3)
> mydim <- c(NA, 10, 20)
> nf <- 2
> nk <- sample(c(50, 100, 200), mydim[3], replace = TRUE)
> Gmat <- matrix(rnorm(nf^2), nrow = nf, ncol = nf)
> Bmat <- matrix(runif(mydim[2]*nf), nrow = mydim[2], ncol = nf)
> Cmat <- matrix(runif(mydim[3]*nf), nrow = mydim[3], ncol = nf)
> Xmat <- Emat <- Amat <- vector("list", mydim[3])
> for(k in 1:mydim[3]){
+   Amat[[k]] <- matrix(rnorm(nk[k]*nf), nrow = nk[k], ncol = nf)
+   Amat[[k]] <- svd(Amat[[k]], nv = 0)$u %*% Gmat
+   Xmat[[k]] <- tcrossprod(Amat[[k]] %*% diag(Cmat[k,]), Bmat)
+   Emat[[k]] <- matrix(rnorm(nk[k]*mydim[2]), nrow = nk[k], ncol = mydim[2])
+ }
> Emat <- nscale(Emat, 0, ssnew = sumsq(Xmat))   # SNR = 1
> X <- mapply("+", Xmat, Emat)
> 
> # fit Parafac2 model (unconstrained)
> pfac <- parafac2(X, nfac = nf, nstart = 1)

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |======================================================================| 100%
> pfac

3-way Parafac2 with 2 factors 

Constraints:
      A      B      C
 uncons uncons uncons

Fit Information:
  SSE = 34.62162 
  R^2 = 0.5939997 
  GCV = 0.002773813 
  EDF = 3899 
 
Converged: TRUE (6 iterations)
 > 
> # check solution
> Xhat <- fitted(pfac)
> sse <- sumsq(mapply("-", Xmat, Xhat))
> sse / (sum(nk) * mydim[2])
[1] 0.0004736223
> crossprod(pfac$A[[1]])
           [,1]       [,2]
[1,] 1.00000000 0.04824405
[2,] 0.04824405 1.00000000
> crossprod(pfac$A[[2]])
           [,1]       [,2]
[1,] 1.00000000 0.04824405
[2,] 0.04824405 1.00000000
> pfac$Phi
           [,1]       [,2]
[1,] 1.00000000 0.04824405
[2,] 0.04824405 1.00000000
> 
> # reorder and resign factors
> pfac$B[1:4,]
            [,1]       [,2]
[1,]  0.47222383 -1.5045452
[2,]  0.06353471 -1.7277388
[3,] -0.18210813 -0.9735257
[4,]  0.13544967 -0.3934817
> pfac <- reorder(pfac, 2:1)
> pfac$B[1:4,]
           [,1]        [,2]
[1,] -1.5045452  0.47222383
[2,] -1.7277388  0.06353471
[3,] -0.9735257 -0.18210813
[4,] -0.3934817  0.13544967
> pfac <- resign(pfac, mode="B")
> pfac$B[1:4,]
          [,1]        [,2]
[1,] 1.5045452  0.47222383
[2,] 1.7277388  0.06353471
[3,] 0.9735257 -0.18210813
[4,] 0.3934817  0.13544967
> Xhat <- fitted(pfac)
> sse <- sumsq(mapply("-", Xmat, Xhat))
> sse / (sum(nk) * mydim[2])
[1] 0.0004736223
> 
> # rescale factors
> colSums(pfac$B^2)
[1] 10 10
> colSums(pfac$C^2)
[1] 2.379171 2.684502
> pfac <- rescale(pfac, mode = "C", absorb = "B")
> colSums(pfac$B^2)
[1] 1.189586 1.342251
> colSums(pfac$C^2)
[1] 20 20
> Xhat <- fitted(pfac)
> sse <- sumsq(mapply("-", Xmat, Xhat))
> sse / (sum(nk) * mydim[2])
[1] 0.0004736223
> 
> 
> ##########   4-way example   ##########
> 
> # create random data list with Parafac2 structure
> set.seed(4)
> mydim <- c(NA, 10, 20, 5)
> nf <- 3
> nk <- sample(c(50,100,200), mydim[4], replace=TRUE)
> Gmat <- matrix(rnorm(nf^2), nrow = nf, ncol = nf)
> Bmat <- scale(matrix(rnorm(mydim[2]*nf), nrow = mydim[2], ncol = nf), center = FALSE)
> cseq <- seq(-3, 3, length=mydim[3])
> Cmat <- cbind(pnorm(cseq), pgamma(cseq+3.1, shape=1, rate=1)*(3/4), pt(cseq-2, df=4)*2)
> Dmat <- scale(matrix(runif(mydim[4]*nf)*2, nrow = mydim[4], ncol = nf), center = FALSE)
> Xmat <- Emat <- Amat <- vector("list",mydim[4])
> for(k in 1:mydim[4]){
+   aseq <- seq(-3, 3, length.out = nk[k])
+   Amat[[k]] <- cbind(sin(aseq), sin(abs(aseq)), exp(-aseq^2))
+   Amat[[k]] <- svd(Amat[[k]], nv = 0)$u %*% Gmat
+   Xmat[[k]] <- array(tcrossprod(Amat[[k]] %*% diag(Dmat[k,]),
+                                 krprod(Cmat, Bmat)), dim = c(nk[k], mydim[2], mydim[3]))
+   Emat[[k]] <- array(rnorm(nk[k] * mydim[2] * mydim[3]), dim = c(nk[k], mydim[2], mydim[3]))
+ }
> Emat <- nscale(Emat, 0, ssnew = sumsq(Xmat))   # SNR = 1
> X <- mapply("+", Xmat, Emat)
> 
> # fit Parafac model (smooth A, unconstrained B, monotonic C, non-negative D)
> pfac <- parafac2(X, nfac = nf, nstart = 1, 
+                  const = c("smooth", "uncons", "moninc", "nonneg"))
Error in parafac2(X, nfac = nf, nstart = 1, const = c("smooth", "uncons",  : 
  Input 'X' must be 3-way or 4-way array
Execution halted