Plot hotelling's confidence ellipse in PCA

18 visualizaciones (últimos 30 días)
karthik
karthik el 25 de Mzo. de 2014
Respondida: Aditya el 3 de Mzo. de 2025
Can anyone tell me how to draw an ellipse error on a pca plot. The princomp function gives us the distances from each point to the center of the group, but how can we use this information to draw the corresponding ellipse's?

Respuestas (1)

Aditya
Aditya el 3 de Mzo. de 2025
Hi Karthik,
To draw an error ellipse on a PCA plot, you can use the covariance matrix of the principal component scores to determine the shape and orientation of the ellipse.
Here is the sample code for the same:
% Sample data
data = randn(100, 3); % 100 observations with 3 variables
% Perform PCA
[coeff, score, latent] = pca(data);
% Calculate the covariance matrix of the scores
covariance = cov(score(:, 1:2)); % Use the first two principal components
% Calculate the mean of the scores
mean_score = mean(score(:, 1:2));
% Eigen decomposition of the covariance matrix
[eigenvec, eigenval] = eig(covariance);
% Calculate the ellipse parameters
chi_square_val = 2.4477; % For 95% confidence interval
theta = linspace(0, 2*pi, 100);
ellipse_x = chi_square_val * sqrt(eigenval(1,1)) * cos(theta);
ellipse_y = chi_square_val * sqrt(eigenval(2,2)) * sin(theta);
% Rotation matrix
R = [cos(atan2(eigenvec(2,1), eigenvec(1,1))), -sin(atan2(eigenvec(2,1), eigenvec(1,1)));
sin(atan2(eigenvec(2,1), eigenvec(1,1))), cos(atan2(eigenvec(2,1), eigenvec(1,1)))];
% Rotate the ellipse
ellipse = [ellipse_x; ellipse_y]' * R;
% Plot the PCA scores
figure;
scatter(score(:, 1), score(:, 2), 'b.');
hold on;
% Plot the error ellipse
plot(mean_score(1) + ellipse(:, 1), mean_score(2) + ellipse(:, 2), 'r-', 'LineWidth', 2);
% Add labels and title
xlabel('Principal Component 1');
ylabel('Principal Component 2');
title('PCA with Error Ellipse');
axis equal;
grid on;
hold off;

Categorías

Más información sobre Dimensionality Reduction and Feature Extraction en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by