Learn R Programming

multiway (version 1.0-2)

parafac2: Parallel Factor Analysis-2

Description

Given a list of matrices X[[k]] = matrix(xk,I[k],J) for k = seq(1,K), the 3-way Parafac2 model (with Mode A nested in Mode C) can be written as r{ X[[k]] = tcrossprod(A[[k]]%*%diag(C[k,]),B) + E[[k]] subject to crossprod(A[[k]]) = Phi } where A[[k]] = matrix(ak,I[k],R) are the Mode A (first mode) weights for the k-th level of Mode C (third mode), Phi is the common crossproduct matrix shared by all K levels of Mode C, B = matrix(b,J,R) are the Mode B (second mode) weights, C = matrix(c,K,R) are the Mode C (third mode) weights, and E[[k]] = matrix(ek,I[k],J) is the residual matrix corresponding to k-th level of Mode C.

Given a list of arrays X[[l]] = array(xl,dim=c(I[l],J,K)) for l = seq(1,L), the 4-way Parafac2 model (with Mode A nested in Mode D) can be written as r{ X[[l]][,,k] = tcrossprod(A[[l]]%*%diag(D[l,]*C[k,]),B) + E[[k]] subject to crossprod(A[[l]]) = Phi } A[[l]] = matrix(al,I[l],R) are the Mode A (first mode) weights for the l-th level of Mode D (fourth mode), Phi is the common crossproduct matrix shared by all L levels of Mode D, D = matrix(d,L,R) are the Mode D (fourth mode) weights, and E[[l]] = matrix(el,I[l],J,K) is the residual array corresponding to l-th level of Mode D.

Weight matrices are estimated using an alternating least squares algorithm with optional constraints.

Usage

parafac2(X,nfac,nstart=10,const=NULL,
         Gfixed=NULL,Bfixed=NULL,Cfixed=NULL,Dfixed=NULL,
         Gstart=NULL,Bstart=NULL,Cstart=NULL,Dstart=NULL,
         Gstruc=NULL,Bstruc=NULL,Cstruc=NULL,Dstruc=NULL,
         maxit=500,ctol=10^-4,parallel=FALSE,
         cl=NULL,output=c("best","all"))

Arguments

X
For 3-way Parafac2: list of length K where k-th element is I[k]-by-J matrix or three-way data array with dim=c(I,J,K). For 4-way Parafac2: list of length L where l-
nfac
Number of factors.
nstart
Number of random starts.
const
Constraints for each mode. See Examples.
Gfixed
Fixed Mode A crossproducts (crossprod(Gfixed)=Phi). Only used to fit model with fixed Phi matrix.
Bfixed
Fixed Mode B weights. Only used to fit model with fixed Mode B weights.
Cfixed
Fixed Mode C weights. Only used to fit model with fixed Mode C weights.
Dfixed
Fixed Mode D weights. Only used to fit model with fixed Mode D weights.
Gstart
Starting Mode A crossproduct matrix for ALS algorithm (crossprod(Gstart)=Phi). Default uses random weights.
Bstart
Starting Mode B weights for ALS algorithm. Default uses random weights.
Cstart
Starting Mode C weights for ALS algorithm. Default uses random weights.
Dstart
Starting Mode D weights for ALS algorithm. Default uses random weights.
Gstruc
Structure constraints for Mode A crossproduct matrix (crossprod(Gstruc) = Phi structure). Default uses unstructured crossproducts.
Bstruc
Structure constraints for Mode B weights. Default uses unstructured weights.
Cstruc
Structure constraints for Mode C weights. Default uses unstructured weights.
Dstruc
Structure constraints for Mode D weights. Default uses unstructured weights.
maxit
Maximum number of iterations.
ctol
Convergence tolerance.
parallel
Logical indicating if parLapply should be used. See Examples.
cl
Cluster created by makeCluster. Only used when parallel=TRUE.
output
Output the best solution (default) or output all nstart solutions.

Value

  • If output="best", returns an object of class "parafac2" with the following elements:
  • AList with 2 elements (see Note).
  • BMode B weight matrix.
  • CMode C weight matrix.
  • DMode D weight matrix.
  • RsqR-squared value.
  • GCVGeneralized Cross-Validation.
  • edfEffective degrees of freedom.
  • iterNumber of iterations.
  • cflagConvergence flag.
  • constSame as input const.
  • Otherwise returns a list of length nstart where each element is an object of class "parafac2".

Warnings

The ALS algorithm can perform poorly if the number of factors nfac is set too large.

Non-negativity constraints can be sensitive to local optima.

Non-negativity constraints can result in slower performance.

References

Bro, R., & De Jong, S. (1997). A fast non-negativity-constrained least squares algorithm. Journal of Chemometrics, 11, 393-401.

Harshman, R. A. (1972). PARAFAC2: Mathematical and technical notes. UCLA Working Papers in Phonetics, 22, 30-44.

Helwig, N. E. (2013). The special sign indeterminacy of the direct-fitting Parafac2 model: Some implications, cautions, and recommendations, for Simultaneous Component Analysis. Psychometrika, 78, 725-739.

Kiers, H. A. L., ten Berge, J. M. F., & Bro, R. (1999). PARAFAC2-part I: A direct-fitting algorithm for the PARAFAC2 model. Journal of Chemometrics, 13, 275-294.

Examples

Run this code
##########   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),nf,nf)
Bmat <- matrix(runif(mydim[2]*nf),mydim[2],nf)
Cmat <- matrix(runif(mydim[3]*nf),mydim[3],nf)
Xmat <- Emat <- Hmat <- vector("list",mydim[3])
for(k in 1:mydim[3]){
  Hmat[[k]] <- svd(matrix(rnorm(nk[k]*nf),nk[k],nf),nv=0)$u
  Xmat[[k]] <- tcrossprod(Hmat[[k]]%*%Gmat%*%diag(Cmat[k,]),Bmat)
  Emat[[k]] <- matrix(rnorm(nk[k]*mydim[2]),nk[k],mydim[2])
}
Emat <- nscale(Emat,0,sumsq(Xmat))   # SNR=1
X <- mapply("+",Xmat,Emat)

# fit Parafac2 model (unconstrained)
pfac <- parafac2(X,nfac=nf,nstart=1)
pfac$Rsq

# check solution
Xhat <- fitted(pfac)
sse <- sumsq(mapply("-",Xmat,Xhat))
sse/(sum(nk)*mydim[2])
crossprod(pfac$A$H[[1]])
crossprod(pfac$A$G)

# reorder and resign factors
pfac$B[1:4,]
pfac <- reorder(pfac, 2:1)
pfac$B[1:4,]
pfac <- resign(pfac, mode="B")
pfac$B[1:4,]
Xhat <- fitted(pfac)
sse <- sumsq(mapply("-",Xmat,Xhat))
sse/(sum(nk)*mydim[2])

# rescale factors
colSums(pfac$B^2)
colSums(pfac$C^2)
pfac <- rescale(pfac, mode="C", absorb="B")
colSums(pfac$B^2)
colSums(pfac$C^2)
Xhat <- fitted(pfac)
sse <- sumsq(mapply("-",Xmat,Xhat))
sse/(sum(nk)*mydim[2])


##########   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),nf,nf)
Bmat <- matrix(runif(mydim[2]*nf),mydim[2],nf)
Cmat <- matrix(runif(mydim[3]*nf),mydim[3],nf)
Dmat <- matrix(runif(mydim[4]*nf),mydim[4],nf)
Xmat <- Emat <- Hmat <- vector("list",mydim[4])
for(k in 1:mydim[4]){
  Hmat[[k]] <- svd(matrix(rnorm(nk[k]*nf),nk[k],nf),nv=0)$u
  Xmat[[k]] <- array(tcrossprod(Hmat[[k]]%*%Gmat%*%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,sumsq(Xmat))   # SNR=1
X <- mapply("+",Xmat,Emat)

# fit Parafac2 model (unconstrained)
pfac <- parafac2(X,nfac=nf,nstart=1)
pfac$Rsq

# check solution
Xhat <- fitted(pfac)
sse <- sumsq(mapply("-",Xmat,Xhat))
sse/(sum(nk)*mydim[2]*mydim[3])
crossprod(pfac$A$H[[1]])
crossprod(pfac$A$G)


##########   parallel computation   ##########

# 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),nf,nf)
Bmat <- matrix(runif(mydim[2]*nf),mydim[2],nf)
Cmat <- matrix(runif(mydim[3]*nf),mydim[3],nf)
Xmat <- Emat <- Hmat <- vector("list",mydim[3])
for(k in 1:mydim[3]){
  Hmat[[k]] <- svd(matrix(rnorm(nk[k]*nf),nk[k],nf),nv=0)$u
  Xmat[[k]] <- tcrossprod(Hmat[[k]]%*%Gmat%*%diag(Cmat[k,]),Bmat)
  Emat[[k]] <- matrix(rnorm(nk[k]*mydim[2]),nk[k],mydim[2])
}
Emat <- nscale(Emat,0,sumsq(Xmat))   # SNR=1
X <- mapply("+",Xmat,Emat)

# fit Parafac2 model (10 random starts -- sequential computation)
set.seed(1)
system.time({pfac <- parafac2(X,nfac=nf)})
pfac$Rsq

# fit Parafac2 model (10 random starts -- parallel computation)
set.seed(1)
cl <- makeCluster(detectCores())
ce <- clusterEvalQ(cl,library(multiway))
system.time({pfac <- parafac2(X,nfac=nf,parallel=TRUE,cl=cl)})
pfac$Rsq
stopCluster(cl)

Run the code above in your browser using DataLab