%% Principal Component Analysis

load oral_micro.mat

otus
hold on
for i = 1:size(otus,1)
    if strcmpi(meta.Location{i},'oral')
        plot(otus(i,:),'r')
    else
        plot(otus(i,:),'b')
    end
end
xlabel('OTUs')
ylabel('Abundance in Samples')
legend('Oral','Stool')
hold off
[coeff,score,latent,tsquared,explained] = pca(otus);
gscatter(score(:,1), score(:,2), meta.Location)
xlabel(sprintf('Principal Component 1 (%0.2f%% of variance)',explained(1)));
ylabel(sprintf('Principal Component 2 (%0.2f%% of variance)',explained(2)));
plot(coeff(:,1))
xlabel('OTUs');
ylabel('Principal Component 1 loadings');