Learn R Programming

multiway (version 1.0-2)

tucker: Tucker Factor Analysis

Description

Given a 3-way array X = array(x,dim=c(I,J,K)), the 3-way Tucker model can be written as c{ X[i,j,k] = sum sum sum A[i,p]*B[j,q]*C[k,r]*G[p,q,r] + E[i,j,k] } where A = matrix(a,I,P) are the Mode A (first mode) weights, B = matrix(b,J,Q) are the Mode B (second mode) weights, C = matrix(c,K,R) are the Mode C (third mode) weights, G = array(g,dim=c(P,Q,R)) is the 3-way core array, and E = array(e,dim=c(I,J,K)) is the 3-way residual array. The summations are for p = seq(1,P), q = seq(1,Q), and r = seq(1,R).

Given a 4-way array X = array(x,dim=c(I,J,K,L)), the 4-way Tucker model can be written as c{ X[i,j,k,l] = sum sum sum sum A[i,p]*B[j,q]*C[k,r]*D[l,s]*G[p,q,r,s] + E[i,j,k,l] } where D = matrix(d,L,S) are the Mode D (fourth mode) weights, G = array(g,dim=c(P,Q,R,S)) is the 4-way residual array, E = array(e,dim=c(I,J,K,L)) is the 4-way residual array, and the other terms can be interprered as previously described.

Weight matrices are estimated using an alternating least squares algorithm.

Usage

tucker(X,nfac,nstart=10,Afixed=NULL,
       Bfixed=NULL,Cfixed=NULL,Dfixed=NULL,
       Bstart=NULL,Cstart=NULL,Dstart=NULL,
       maxit=500,ctol=10^-4,parallel=FALSE,
       cl=NULL,output=c("best","all"))

Arguments

X
Three-way data array with dim=c(I,J,K) or four-way data array with dim=c(I,J,K,L).
nfac
Number of factors in each mode.
nstart
Number of random starts.
Afixed
Fixed Mode A weights. Only used to fit model with fixed weights in Mode A.
Bfixed
Fixed Mode B weights. Only used to fit model with fixed weights in Mode B.
Cfixed
Fixed Mode C weights. Only used to fit model with fixed weights in Mode C.
Dfixed
Fixed Mode D weights. Only used to fit model with fixed weights in Mode D.
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.
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 "tucker" with the following elements:
  • AMode A weight matrix.
  • BMode B weight matrix.
  • CMode C weight matrix.
  • DMode D weight matrix.
  • GCore array.
  • RsqR-squared value.
  • GCVGeneralized Cross-Validation.
  • edfEffective degrees of freedom.
  • iterNumber of iterations.
  • cflagConvergence flag.
  • Otherwise returns a list of length nstart where each element is an object of class "tucker".

Warnings

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

Input matrices in Afixed, Bfixed, Cfixed, Dfixed, Bstart, Cstart, and Dstart must be columnwise orthonormal.

References

Kroonenberg, P. M., & de Leeuw, J. (1980). Principal component analysis of three-mode data by means of alternating least squares algorithms. Psychometrika, 45, 69-97. Tucker, L. R. (1966). Some mathematical notes on three-mode factor analysis. Psychometrika, 31, 279-311.

Examples

Run this code
##########   3-way example   ##########

# create random data array with Tucker structure
set.seed(3)
mydim <- c(50,20,5)
nf <- c(3,2,3)
Amat <- svd(matrix(rnorm(mydim[1]*nf[1]),mydim[1],nf[1]),nu=nf[1])$u
Bmat <- svd(matrix(rnorm(mydim[2]*nf[2]),mydim[2],nf[2]),nu=nf[2])$u
Cmat <- svd(matrix(rnorm(mydim[3]*nf[3]),mydim[3],nf[3]),nu=nf[3])$u
Gmat <- array(rnorm(prod(nf)),dim=nf)
Xmat <- array(tcrossprod(Amat%*%matrix(Gmat,nf[1],nf[2]*nf[3]),kronecker(Cmat,Bmat)),dim=mydim)
Emat <- array(rnorm(prod(mydim)),dim=mydim)
Emat <- nscale(Emat,0,sumsq(Xmat))   # SNR=1
X <- Xmat + Emat

# fit Tucker model
tuck <- tucker(X,nfac=nf,nstart=1)
tuck$Rsq

# check solution
Xhat <- fitted(tuck)
sum((Xmat-Xhat)^2)/prod(mydim)

# reorder mode="A"
tuck$A[1:4,]
tuck$G
tuck <- reorder(tuck, neworder=c(3,1,2), mode="A")
tuck$A[1:4,]
tuck$G
Xhat <- fitted(tuck)
sum((Xmat-Xhat)^2)/prod(mydim)

# reorder mode="B"
tuck$B[1:4,]
tuck$G
tuck <- reorder(tuck, neworder=2:1, mode="B")
tuck$B[1:4,]
tuck$G
Xhat <- fitted(tuck)
sum((Xmat-Xhat)^2)/prod(mydim)

# resign mode="C"
tuck$C[1:4,]
tuck <- resign(tuck, mode="C")
tuck$C[1:4,]
Xhat <- fitted(tuck)
sum((Xmat-Xhat)^2)/prod(mydim)


##########   4-way example   ##########

# create random data array with Tucker structure
set.seed(4)
mydim <- c(30,10,8,10)
nf <- c(2,3,4,3)
Amat <- svd(matrix(rnorm(mydim[1]*nf[1]),mydim[1],nf[1]),nu=nf[1])$u
Bmat <- svd(matrix(rnorm(mydim[2]*nf[2]),mydim[2],nf[2]),nu=nf[2])$u
Cmat <- svd(matrix(rnorm(mydim[3]*nf[3]),mydim[3],nf[3]),nu=nf[3])$u
Dmat <- svd(matrix(rnorm(mydim[4]*nf[4]),mydim[4],nf[4]),nu=nf[4])$u
Gmat <- array(rnorm(prod(nf)),dim=nf)
Xmat <- array(tcrossprod(Amat%*%matrix(Gmat,nf[1],prod(nf[2:4])),
                      kronecker(Dmat,kronecker(Cmat,Bmat))),dim=mydim)
Emat <- array(rnorm(prod(mydim)),dim=mydim)
Emat <- nscale(Emat,0,sumsq(Xmat))   # SNR=1
X <- Xmat + Emat

# fit Tucker model
tuck <- tucker(X,nfac=nf,nstart=1)
tuck$Rsq

# check solution
Xhat <- fitted(tuck)
sum((Xmat-Xhat)^2)/prod(mydim)


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

# create random data array with Tucker structure
set.seed(3)
mydim <- c(50,20,5)
nf <- c(3,2,3)
Amat <- svd(matrix(rnorm(mydim[1]*nf[1]),mydim[1],nf[1]),nu=nf[1])$u
Bmat <- svd(matrix(rnorm(mydim[2]*nf[2]),mydim[2],nf[2]),nu=nf[2])$u
Cmat <- svd(matrix(rnorm(mydim[3]*nf[3]),mydim[3],nf[3]),nu=nf[3])$u
Gmat <- array(rnorm(prod(nf)),dim=nf)
Xmat <- array(tcrossprod(Amat%*%matrix(Gmat,nf[1],nf[2]*nf[3]),kronecker(Cmat,Bmat)),dim=mydim)
Emat <- array(rnorm(prod(mydim)),dim=mydim)
Emat <- nscale(Emat,0,sumsq(Xmat))   # SNR=1
X <- Xmat + Emat

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

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

Run the code above in your browser using DataLab