Draw first principal component line and regression line on the same scatterplot
3 views (last 30 days)
Show older comments
Given the data below
WireLength DieHeight
1 125
4 110
5 287
6 200
8 350
10 280
12 400
14 370
13 480
18 420
19 540
22 518
where WireLength is the x axis and Die Height is the y axis
Draw a regression line and the first principal component line on the scatter plot.
Below I have standardized the data and then drew the scatterplot.

After this I drew the regression line using the code below
%regression best fit line
model = fitlm(WireLength_Z, DieHeight_Z);
p = polyfit(WireLength_Z, DieHeight_Z, 1);
f = polyval(p, WireLength_Z);
h = plot(WireLength_Z, DieHeight_Z,'ok', WireLength_Z, f, '-', 'LineWidth', 2, 'MarkerSize', 5)
%filled markers
set(h, {'MarkerFaceColor'}, get(h,'Color'));
% Set axis limits
xlim([-3.5 3.5]); % Set x-axis limits
ylim([-3.5 3.5]); % Set y-axis limits
% Add x and y axes
ax = gca; % Get current axes
ax.XAxisLocation = 'origin'; % Set x-axis to the origin
ax.YAxisLocation = 'origin'; % Set y-axis to the origin
ax.XColor = 'k'; % Set x-axis color
ax.YColor = 'k'; % Set y-axis color
% Add a grid for better visualization
grid on;
using this code my scatterplot now looks like

So on the same axis I also want to draw the first PC line only such that it looks something like

0 Comments
Answers (1)
Image Analyst
on 28 Aug 2024
It looks like you're doing a simple linear fit. I'm not seeing how you're using pca. It sounds like homework and they want you to compute the principal components, not simply do a fit with polyfit.
help pca
This is how I'd start it:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 16;
markerSize = 30;
% Define experimental/sample data.
% [WireLength, DieHeight]
data = [...
1 125
4 110
5 287
6 200
8 350
10 280
12 400
14 370
13 480
18 420
19 540
22 518]
% Plot the original data, which has 12 "observations".
figure('Name', 'PCA Demo by Image Analyst', 'NumberTitle', 'off')
subplot(2, 1, 1);
plot(data(:, 1), data(:, 2), 'b.', 'MarkerSize', markerSize)
title('Data in Original Coordinate Space', 'FontSize', fontSize)
xlabel('Wire Length', 'FontSize', fontSize)
ylabel('Die Height', 'FontSize', fontSize)
grid
% You can tell by looking at the plot that PC1 would go along
% the data from lower left to upper right. PC2 would then be
% the distance of the data perpendicularly from that line.
%--------------------------------------------------------
% Do a regression to minimize the squared vertical distance from the fitted line.
x = data(:, 1);
y = data(:, 2);
coefficients = polyfit(x, y, 1);
fittedx = [min(x), max(x)];
fittedy = polyval(coefficients, fittedx);
% Label points
for k = 1 : numel(x)
str = sprintf(' %d', k);
xt = x(k);
yt = y(k);
text(xt, yt, str, 'Color', 'b', 'FontSize', fontSize);
end
% Plot the regression over the original data
hold on;
plot(fittedx, fittedy, 'r-', 'LineWidth', 2);
legend('Original Data', 'Fitted Regression Line', 'Location', 'northwest')
drawnow;
%--------------------------------------------------------
% Do principal components analysis.
[coeff,score,latent,tsquared,explained,mu] = pca(data)
%--------------------------------------------------------
For a demo beyond that, see attached demo m-file, where I draw the axes on the original data.
0 Comments
See Also
Categories
Find more on Dimensionality Reduction and Feature Extraction in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!