How do I fit a piecewise linear model to my timeseries data?
7 views (last 30 days)
Show older comments
MathWorks Support Team
on 8 Nov 2016
Edited: MathWorks Support Team
on 25 Aug 2021
I want to segment my timeseries data by identifying linear trends in the data. In particular, I would like to accomplish this by fitting a piecewise linear model to the data, and use the endpoints of each line segment as an indication of trend boundaries.
Accepted Answer
MathWorks Support Team
on 29 Oct 2018
Edited: MathWorks Support Team
on 25 Aug 2021
In general, fitting a piecewise linear model to data is a very large question with many possible algorithms and approaches. Thankfully, there is a very large base of user-written programs which can be found at the MATLAB Central File Exchange. In particular, I came across the following toolbox which seems well suited to the task at hand. Please note that while I was personally unable to "install" this toolbox, downloading the ZIP file worked perfectly for me.
There is a huge amount of functionality within this toolbox that you can explore, however I created the short example below to show you a simple case of fitting a piecewise linear model to data.
load stockreturns.mat;
y = stocks(:,1);
x = 1:length(y);
prescription = slmset('order',2); % piecewise linear
slm = slmengine(x,y,prescription); % create model
fitLine = slmeval(x,slm); % evaluate model
figure;plot(y);
hold on;plot(fitLine);
I believe that a workflow similar to this example should help you segment your timeseries data according to linear trends. Please note that while we encourage users to explore and use the File Exchange, these are not files from The MathWorks and questions regarding 3rd-party programs should be directed towards the author of that software. However, please feel free to reach out if you have any other questions not directly related to the SLM Toolbox, and I will be happy to help.
0 Comments
More Answers (0)
See Also
Categories
Find more on Time Series in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!