Effacer les filtres
Effacer les filtres

Trend lines with moving average

42 vues (au cours des 30 derniers jours)
Max1234
Max1234 le 26 Fév 2023
Hi guys,
I have a data set with the following double values (Test.mat). When i plot the Data with scatter i get this figure:
I now need two types of trend lines. The first should be linear and the second should represent as many "curves" as possible. How can I achieve this with a moving average?
I need the trend line as a function so that I can insert it together with other trend lines into a new plot and compare them.
Many thanks for your help!

Réponse acceptée

Star Strider
Star Strider le 26 Fév 2023
It is unlikely that it is posssible to get any meanigful information from these unless they are first sorted.
LD = load(websave('Test','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1307620/Test.mat'))
LD = struct with fields:
x_Test: [67×1 double] y_Test: [67×1 double]
x = LD.x_Test;
y = LD.y_Test;
xys = sortrows([x,y],1);
x = xys(:,1);
y = xys(:,2);
window = 3; % Window Over Which The Moving Average Is Computed
ymm = movmean(y,window);
DM = [x ones(size(x))];
B = DM \ y;
ylr = DM * B;
figure
plot(x, y, '.', 'DisplayName','Sorted Data')
hold on
plot(x, ymm, 'DisplayName','Moving Average')
plot(x, ylr, 'LineWidth',2, 'DisplayName','Linear Regression')
hold off
xlabel('X')
ylabel('Y')
grid
legend('Location','best')
The data needs to be sorted to compute the moving average (the movmean function). The regression does not care if the data are sorted or not, however the plot of the regression line very much cares.
.

Plus de réponses (2)

the cyclist
the cyclist le 26 Fév 2023
There are many ways to do the linear fit in MATLAB, including some in base MATLAB. Here is one way, using the fitlm function from the Statistics and Machine Learning Toolbox. I expect other folks will post answers using other functions.
load("Test.mat","x_Test","y_Test")
mdl = fitlm(x_Test,y_Test);
figure
hold on
scatter(x_Test,y_Test)
plot(sort(x_Test),predict(mdl,sort(x_Test)))
I'm unclear what you mean by the second thing you need, with "as many curves as possible". One could make a perfect fit through these points, with any number of curves (e.g. with a polynomial of extremely high order). You need to clarify what you mean here.
Also, I'm not sure what you mean by a moving average here. I know what a moving average is, but it's unclear how you want to include that along with a trend line.

Steven Lord
Steven Lord le 26 Fév 2023
My first thought would be to try using the Remove Trends Live Editor Task or the trenddecomp function.

Catégories

En savoir plus sur Scatter Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by