How can I plot the linear variation?

1 次查看(过去 30 天)
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

采纳的回答

Star Strider
Star Strider 2024-2-28
编辑:Star Strider 2024-2-29
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 个评论
Sergio
Sergio 2024-2-29
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 2024-2-29
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)])
.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interpolation 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by