title: Detecting Outliers When Doing PCA
body: <p>I am new to data analysis and trying to better understand how I can identify outliers when doing PCA analysis. I have created a data matrix with 5 columns to represent my variables of Math, English, History, Physics, and Social Science; and each row represents the final grade a student received in the class. The fifth column in my data matrix is an outlier when I plot scores for the first and second principal components. I would like to have a way to mathematically detect outliers without having to plot the scores; any suggestion or ideas to aid me in doing that is greatly appreciated. Thanks in advance for your help. I have posted my code below. </p>
  
  <pre><code>%Column names
  colNames = {'Math','English','History','Physics','Social Science'};
  
  %data matrix
  data = [75.23,74.65,77,73.04,72.11;
      88.50,89.43,86.23,88.50,50.97;
      66.12,65.12,67.45,66.02,66.54;
      89.23,90.43,88.21,88.23,71.21;
      72.35,72.43,73.56,74.32,63.51;
      50.23,52.34,51.78,52.32,59.85;
      58.79,58.79,58.79,58.79,91.08;
      86.08,86.08,86.08,86.08,71.49;
      73.67,73.67,73.67,73.67,94.38;
      56.34,57.63,58.23,58.32,69.55;
      67.05,69.42,66.32,65.32,88.45;
      81.23,80.36,80.32,79.89,69.83;
      59.68,59.58,60.32,59.02,90.42;
      87.34,86.92,85.23,86.01,87.75;
      63.21,62.14,62.03,62.32,68.86;
      95.87,94.54,95.65,96.12,60.80;
      64.34,63.45,63.45,63.45,89.52;
      89.32,87.54,88.27,88.01,97.46;
      59.65,58.23,60.32,59.43,66.37;
      63.98,64.37,65.01,64.01,83.56;
      56.34,55.35,53.98,54.25,71.93;
      79.98,78.81,78.01,77.99,91.67;
      84.16,85.021,83.99,84.87,88.44;
      78.38,77.32,76.98,77.56,58.36;
      71.28,72.98,71.99,71.56,93.09;];
  
  %Computing PCA
  covarianceMat=cov(data);
  [eigenVectors,eigenValues]=eigs(covarianceMat,5);
  
  %Sorting Eigen values
  [eigenValues I] = sort(diag(eigenValues),'descend');
  
  %Computing Variance Percentage of each Eigen value
  variancePercentage = (eigenValues ./ sum(eigenValues)) .*100;
  
  figure(2)
  plot(eigenVectors(:,1),eigenVectors(:,2),'*');
  xlabel('Principal Component 1');ylabel('Principal Component 2')
  for Loop = 1:length(colNames)
      text(eigenVectors(Loop,1),eigenVectors(Loop,2),colNames{Loop},'Color','r')
  end
  </code></pre>