# How to “Compare unfold-PCA and PARAFAC”

Centering
To center the data do the following
(or use mncn in the PLS_Toolbox after matricizing the data)

`Cent  = [1 0 0]; Scal  = [0 0 0]; Xmean = nprocess(X,Cent,Scal);`

Before you can analyze the data with PCA, they must be rearranged into a matrix
`Xmean = reshape(Xmean,10,88);`

PCA
Use truncated SVD – singular value decomposition – (or the PLS_Toolbox) to estimate the PCA model. If you don’t know what SVD is, never mind; just fit a two-component PCA model which can be done as follows.

[`U,S,V] = svd(Xmean,0); T = U(:,1:2)*S(1:2,1:2);  % Score matrix P = V(:,1:2); % loading matrix`

and use the plotting functions of MATLAB to see the results

`plot(T(:,1),T(:,2),'.') for i = 1:size(T,1)   text(T(i,1),T(i,2),num2str(i)) end`

PARAFAC
Fit a two-component PARAFAC model and convert the output to scores and loadings. For plotting the PARAFAC attributes use the matrix attrib which holds the names of the attributes

`plot(B(:,1),B(:,2),'.')  for i = 1:size(B,1)    text(B(i,1),B(i,2),attrib(i,:))  end`

For plotting the scores from both PCA and PARAFAC do

` subplot(1,2,1)  plot(T(:,1),T(:,2),'.')  for i = 1:size(T,1)    text(T(i,1),T(i,2),num2str(i))  end `
` subplot(1,2,2)  plot(A(:,1),A(:,2),'.')  for i = 1:size(A,1)    text(A(i,1),A(i,2),num2str(i))  end`

Note that the order and sign of the scores may differ.
This is of no concern as it is the relative positions that are important. Try e.g.

` subplot(1,2,2)  plot(-A(:,2),-A(:,1),'.')  for i = 1:size(A,1)    text(-A(i,2),-A(i,1),num2str(i))  end`

Note also that centering makes a difference.
Remember to center the data both before doing PCA and PARAFAC.