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
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
B = [in{:,[1 2]} ones(size(in,1),1)] \ in{:,3} % Calculate Linear Regression Parameters
[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 个评论
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
B = [in{:,[1 2]} ones(size(in,1),1)] \ in{:,3} % Calculate Linear Regression Parameters
[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
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
%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 Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!