フィルターのクリア

How can I plot the linear variation?

1 回表示 (過去 30 日間)
Sergio
Sergio 2024 年 2 月 28 日
コメント済み: Star Strider 2024 年 2 月 29 日
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 件のコメント
Dyuman Joshi
Dyuman Joshi 2024 年 2 月 28 日
編集済み: Dyuman Joshi 2024 年 2 月 28 日
I am not sure what you mean/want to do, but try plot3.

サインインしてコメントする。

採用された回答

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 Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by