How can I plot the linear variation?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Sergio
am 28 Feb. 2024
Kommentiert: Star Strider
am 29 Feb. 2024
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 Kommentar
Akzeptierte Antwort
Star Strider
am 28 Feb. 2024
Bearbeitet: Star Strider
am 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
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 Kommentare
Star Strider
am 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
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)])
.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Interpolation finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!