Trend lines with moving average
30 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
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'))
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 个评论
更多回答(2 个)
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.
0 个评论
Steven Lord
2023-2-26
My first thought would be to try using the Remove Trends Live Editor Task or the trenddecomp function.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Descriptive Statistics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!