title: Writing (and using) principal component analysis in matlab
body: <p>I (hope to) obtain a matrix with data on different characteristics on rat calls (in the ultrasound). Variables include starting frequency, ending frequency, duration etc etc. The observations will include all the rat calls in my audio recording.</p>
  
  <p>I want to use PCA to analyze my data, hopefully decorrelating any principal components that are not important to the structure of these calls and how they work, allowing me to group the calls up.</p>
  
  <p>My problem is that while I have a basic understanding of how PCA works, I don't have an understanding of the finer points including how to implement this in Matlab.</p>
  
  <p>I know you should standardise my data. All methods I have seen involve means adjusting by subtracting the mean. However some others also divide by the standard deviation or divide the transpose of the means adjusted data by the square root of N-1 (N being the number of variables).</p>
  
  <p>I know with the standardised data, you can then find the covariance matrix, and extract the eigen values and vectors such as with using eig(cov(...)). some others use svd(...) instead. I still don't understand what this is and why it is important</p>
  
  <p>I know there are different ways to implement PCA, but I don't like how I get different results for all of them.</p>
  
  <p>There is even a pca(...) command also.</p>
  
  <p>While reconstructing the data, some people multiply the means adjust data with the principal component data, others do the same but with the transpose of the principal component data</p>
  
  <p>I just want to be able to analyse my data by plotting graphs of the principal components, and of the data (with the most insignificant principal components removed). I want to know about the variances of these eigen vectors and how much they represent the total variance of the data. I want to be able to fully exploit all the information PCA can allow me to get out</p>
  
  <p>can anyone help?</p>
  
  <p>=========================================================</p>
  
  <p>This code seems to work based on pg 20 of <a href="http://people.maths.ox.ac.uk/richardsonm/SignalProcPCA.pdf" rel="nofollow">http://people.maths.ox.ac.uk/richardsonm/SignalProcPCA.pdf</a></p>
  
  <pre><code>X = [105 103 103 66; 245 227 242 267; 685 803 750 586;...
      147 160 122 93; 193 235 184 209; 156 175 147 139;...
      720 874 566 1033; 253 265 171 143; 488 570 418 355;...
      198 203 220 187; 360 365 337 334; 1102 1137 957 674;...
      1472 1582 1462 1494; 57 73 53 47; 1374 1256 1572 1506;...
      375 475 458 135; 54 64 62 41];
  
   [M,N] = size(X);
   mn = mean(X,2); 
   data = X - repmat(mn,1,N);
   Y = data' / sqrt(N-1); 
   [~,S,PC] = svd(Y); 
   S = diag(S); 
   V = S .* S;
   signals = PC' * data;
  
   %plotting single PC1 on its own
   figure;
   plot(signals(1,:),zeros(1,size(signals,2)),'b.','markersize',15)
   xlabel('PC1')
   title('plotting single PC1 on its own')
  
   %plotting PC1 against PC2
   figure;
   plot(signals(1,:),signals(2,:),'b.','markersize',15)
   xlabel('PC1'),ylabel('PC2')
   title('plotting PC1 against PC2')
  
  figure;
  plot(PC(:,1),PC(:,2),'m.','markersize',15)
  xlabel('effect(PC1)'),ylabel('effect(PC2)')
  </code></pre>
  
  <p>but where is the standard deviation? how is the result different to </p>
  
  <pre><code>B=zscore(X); 
  [PC, D] = eig(cov(B));
  D = diag(D);
  cumsum(flipud(D)) / sum(D)
  PC*B %(note how this says PC whereas above it says PC')
  </code></pre>
  
  <p>If the principal components are represented as columns, then I can remove the most insignificant eigen vectors by finding the smallest eigenvalue and setting its corresponding eigen vector column to a column of zeros.</p>
  
  <p>How can either of these methods above be applied by using the pca(...) command and achieve THE SAME result? can anyone help explain this to me (and ideally show me how all of these can achieve the same results)?</p>