%% Principal Components of a Data Set
% Load the sample data set.

load hald

% The ingredients data has 13 observations for 4 variables.
% Find the principal components for the ingredients data.

[coeff,score,latent,tsquared,explained] = pca(ingredients);

%
% Principal Component Coefficients (Loadings)

coeff

% The rows of coeff contain the coefficients for the four ingredient variables, and its columns correspond to four principal components.
%
% Principal Component Scores

score

%
% Principal Component Variances

latent

% The values in latent tell you how much of the total variance in the data is explained by each principal component. Larger values indicate that the component explains a larger portion of the variance in the data.
%
% Hotelling’s T-squared Statistic

tsquared

% High values in tsquared suggest that the corresponding observation is an outlier or is far from the center of the principal component space.
%
% Percentage of Variance Explained

explained

% The values in explained sum to 100% and indicate how much of the total variance in the data is captured by each principal component. The first few components usually capture the majority of the variance, which is why PCA is useful for dimensionality reduction.
%
% Visualize both the orthonormal principal component coefficients for each variable and the principal component scores for each observation in a single plot.

biplot(coeff(:,1:2),'scores',score(:,1:2),'varlabels',{'v_1','v_2','v_3','v_4'});

% All four variables are represented in this biplot by a vector, and the direction and length of the vector indicate how each variable contributes to the two principal components in the plot.
% A longer vector indicates that the variable has a strong influence on the corresponding principal component.
% The angle between vectors represents the correlation between variables. Smaller angles indicate higher correlations, while larger angles indicate weaker or negative correlations.
% For example, the first principal component, which is on the horizontal axis, has positive coefficients for the third and fourth variables. Therefore, vectors v3 and v4 are directed into the right half of the plot. The largest coefficient in the first principal component is the fourth, corresponding to the variable v4.
% The second principal component, which is on the vertical axis, has negative coefficients for the variables v1, v2, and v4, and a positive coefficient for the variable v3.
% This 2-D biplot also includes a point for each of the 13 observations, with coordinates indicating the score of each observation for the two principal components in the plot. For example, points near the left edge of the plot have the lowest scores for the first principal component. The points are scaled with respect to the maximum score value and maximum coefficient length, so only their relative locations can be determined from the plot.