Trend lines with moving average
32 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Max1234
el 26 de Feb. de 2023
Respondida: Steven Lord
el 26 de Feb. de 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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1307645/image.png)
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!
0 comentarios
Respuesta aceptada
Star Strider
el 26 de Feb. de 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'))
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.
.
0 comentarios
Más respuestas (2)
the cyclist
el 26 de Feb. de 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.
0 comentarios
Steven Lord
el 26 de Feb. de 2023
My first thought would be to try using the Remove Trends Live Editor Task or the trenddecomp function.
0 comentarios
Ver también
Categorías
Más información sobre Scatter Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!