How can I plot the linear variation?

3 views (last 30 days)
In this attached file, I plot the car price vs mileage and model (year).
in = readtable('FordFocusCMAX.xlsx')
%plot the data as a red asterisks
scatter3(in{:,1}, in{:,2}, in{:,3}, 'o')
xlabel('År');
ylabel('Mil');
zlabel('Pris');
How can I also include the linear variation?
Thanks
  1 Comment
Dyuman Joshi
Dyuman Joshi on 28 Feb 2024
Edited: Dyuman Joshi on 28 Feb 2024
I am not sure what you mean/want to do, but try plot3.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 28 Feb 2024
Edited: Star Strider on 29 Feb 2024
Calculating the llinear regression was straightforward, however calculating and plotting the regression plane was something of a challenge.
Try this —
in = readtable('FordFocusCMAX.xlsx', 'VariableNamingRule','preserve');
in = rmmissing(in(:,1:3)) % Remove 'NaN' Values
in = 66x3 table
Årsm Mil Pris ____ _____ _________ 7 3100 1.099e+05 7 6615 1.059e+05 6 5600 98500 8 3300 1.3e+05 8 3373 1.46e+05 8 4100 1.399e+05 3 10145 74500 4 8500 68900 7 6885 1.09e+05 5 6900 1.099e+05 7 4900 1.299e+05 7 6770 1.099e+05 9 1700 1.31e+05 6 10300 89900 7 5526 1.07e+05 7 3552 1.09e+05
B = [in{:,[1 2]} ones(size(in,1),1)] \ in{:,3} % Calculate Linear Regression Parameters
B = 3x1
1.0e+04 * 0.9093 -0.0006 9.0395
[xmin,xmax] = bounds(in{:,[1 2]});
xvc = [xmax; xmin];
[Xx,Xn] = ndgrid(xvc(:,1), xvc(:,2));
Pc = [Xx(:), Xn(:)] * B(1:2) + B(3);
P = reshape(Pc, 2, []);
%plot the data as a red asterisks
figure
scatter3(in{:,1}, in{:,2}, in{:,3}, '*r') % Plot Data
hold on
surf(Xx, Xn, P, 'FaceColor',[1 1 1]*0.75, 'FaceAlpha',0.75) % Plot Regression Surface
hold off
xlabel('År');
ylabel('Mil');
zlabel('Pris');
% view(-27,30)
axis([xmin(1) xmax(1) xmin(2) xmax(2)])
EDIT — (29 Feb 2024 at 02:28)
Improved code efficiency.
.
  2 Comments
Sergio
Sergio on 29 Feb 2024
Thanks @Star Strider, this is quite advanced and very illustrative. The variation must be the distance between each point and the plane. Very nice!
Star Strider
Star Strider on 29 Feb 2024
As always, my pleasure!
I did not understand how ‘Variation’ was defined. (The formal definition would be ‘Residual’.)
Calcualting the plane values at each ‘Årsm’ and ‘Mil’ data pair is straightforward using the scatteredInterpolant function to create the interpolant, then giving it the appropriate data pairs to calculate the plane value at those points, and then subtracting the ‘Pris’ value from those values to get the desired ‘Variation’ value. (The mean of those values should be 0 within floating-point approximation error, and they should be normally distributed.)
This uses that approach to calculate the ‘Variation’ values, adds them to the existing table, and plots them as green lines from the plane to the ‘Pris’ value —
in = readtable('FordFocusCMAX.xlsx', 'VariableNamingRule','preserve');
in = rmmissing(in(:,1:3)) % Remove 'NaN' Values
in = 66×3 table
Årsm Mil Pris ____ _____ _________ 7 3100 1.099e+05 7 6615 1.059e+05 6 5600 98500 8 3300 1.3e+05 8 3373 1.46e+05 8 4100 1.399e+05 3 10145 74500 4 8500 68900 7 6885 1.09e+05 5 6900 1.099e+05 7 4900 1.299e+05 7 6770 1.099e+05 9 1700 1.31e+05 6 10300 89900 7 5526 1.07e+05 7 3552 1.09e+05
B = [in{:,[1 2]} ones(size(in,1),1)] \ in{:,3} % Calculate Linear Regression Parameters
B = 3×1
1.0e+04 * 0.9093 -0.0006 9.0395
[xmin,xmax] = bounds(in{:,[1 2]});
xvc = [xmax; xmin];
[Xx,Xn] = ndgrid(xvc(:,1), xvc(:,2));
Pc = [Xx(:), Xn(:)] * B(1:2) + B(3);
P = reshape(Pc, 2, []);
% format longG
% Q = [Xx(:), Xn(:), P(:)]
Fplane = scatteredInterpolant(Xx(:), Xn(:), P(:)) % Calculate Interpolant Function For Plane
Fplane =
scatteredInterpolant with properties: Points: [4×2 double] Values: [4×1 double] Method: 'linear' ExtrapolationMethod: 'linear'
Pv = Fplane(in{:,1}, in{:,2}); % Plane Value At Each Data Pair Coordinate
Variation = in{:,3} - Pv; % Calculate 'Variation'
in = addvars(in, Variation) % Add 'Variation' To Existing 'in' Table
in = 66×4 table
Årsm Mil Pris Variation ____ _____ _________ _________ 7 3100 1.099e+05 -24583 7 6615 1.059e+05 -6405.2 6 5600 98500 -11117 8 3300 1.3e+05 -12314 8 3373 1.46e+05 4146.6 8 4100 1.399e+05 2633.7 3 10145 74500 20838 4 8500 68900 -4233.9 7 6885 1.09e+05 -1601.6 5 6900 1.099e+05 17578 7 4900 1.299e+05 6773.9 7 6770 1.099e+05 -1427.2 9 1700 1.31e+05 -30502 6 10300 89900 9938.2 7 5526 1.07e+05 -12176 7 3552 1.09e+05 -22631
%plot the data as a red asterisks
figure
scatter3(in{:,1}, in{:,2}, in{:,3}, '*r') % Plot Data
hold on
% scatter3(Xx(:), Xn(:), P(:), 200, 'g', 'p', 'filled')
surf(Xx, Xn, P, 'FaceColor',[1 1 1]*0.75, 'FaceAlpha',0.50) % Plot Regression Surface
plot3((in{:,1}*[1 1]).', (in{:,2}*[1 1]).', [Pv in{:,3}].', '-g', 'LineWidth',1) % Plot 'Variation' At EAch Coordinate Pair
hold off
xlabel('År');
ylabel('Mil');
zlabel('Pris');
title('Data With Regression Plane And ‘Variation’ Distances')
% view(-27,30)
axis([xmin(1) xmax(1) xmin(2) xmax(2)])
.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!