Trend lines with moving average

조회 수: 32 (최근 30일)
Max1234
Max1234 2023년 2월 26일
답변: Steven Lord 2023년 2월 26일
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!

채택된 답변

Star Strider
Star Strider 2023년 2월 26일
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.
.

추가 답변 (2개)

the cyclist
the cyclist 2023년 2월 26일
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 2023년 2월 26일
My first thought would be to try using the Remove Trends Live Editor Task or the trenddecomp function.

카테고리

Help CenterFile Exchange에서 Scatter Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by