How can I decompose a time series in linear Trend, Residual and Seasonal trend?
22 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Is there a way to decompose a time series in linear Trend, Residual and Seasonal trend like in the example below? It is taken from the web and made by Python but I would like to do something similar in MATLAB. Is there any "decomposition" function or tool to do it in few lines? I haven't found anything similar...
Thanks a lot!
0 Commenti
Risposte (2)
Kaiguang Zhao
il 2 Apr 2022
Modificato: Kaiguang Zhao
il 2 Apr 2022
TLDR: One way to decompose time series is a Matlab tool called BEAST I developed. Below is a minimal example.
eval(webread('http://b.link/beast',weboptions('cert',''))) % install BEAST and some test data
load('googletrend.mat') % Monthly Google search trend data of 'beach' since Jan 2004
o = beast( beach ) % Apply BEAST to the 'beach time series
plotbeast(o) % plot the results: o.season.Y and o
plotbeast(o,'vars',["st","s","t","error"]) % Choose out variables to plot
Below is a long answer copied from my reply to a similar question asked at https://www.mathworks.com/matlabcentral/answers/780517-how-to-decompose-time-series-data-into-trend-and-seasonality?s_tid=prof_contriblnk.
Numerous time series decomposition algorithms are possible and the results are sensitive to the algorithim choices. (An excerpt from here: The notional of seasonal variation is always intrinsically ambiguous: whether the temporal variation should be considered Seasonal, Trend, or Remainder is, to a degree, a matter of opinion and determined by choice of model and model parameters. This is true in STL as well as any seasonal variational approach).
In case that somebody is looking for an alternative, one choice is a Bayesian method called BEAST (Bayesian estimator of Abrupt change, Seasonality, and Trend) available from this FileExchange entry or https://github.com/zhaokg/Rbeast. It can be instally installed by running eval(webread('http://b.link/beast',weboptions('cert',''))). BEAST actually does the time series decomposition and changepoint detection at the same time. Below is a quick illustration using a monthly Google search Trend time series on the keyword 'beach in the US:
eval(webread('http://b.link/beast',weboptions('cert',''))) % Quick installation of BEAST to a tmp path
load('googletrend.mat') % Monthly Google search trend data of 'beach' since Jan 2004
o = beast( beach ) % Apply BEAST to the 'beach time series: beach is a data vector only; the time
% info can be supplied using the start and deltat
% keywords, as in the next commented line
%o = beast( beach,'start',[2004,1],'deltat',1/12 )
printbeast(o) % print the changepoints detected
plotbeast(o) % plot the results: o.season.Y and o.trend.Y are the seasonal and trend compoents
Below is the plotted result. The decomposed seasonal signal and trend are in the 'seasona' and 'trend' subplots, respectively. In the fitted seasonality and trend, seasonal changepoints (scp) and trend changepoints (tcp) are detected seperately. As a Bayesian method, it not just tells when there are some changepoints but also quanitifies the probablity of changepoint occurrence over time (the Pr(scp) and Pr(tcp) subplots where the peaks indicate the times when the changepoints most likely occur).
Some possible interpretations of the results: There was a sudden jump (or structural break) in the summer of 2011 (The summer of 2011 was the hottest one on record for the US: the time series 'beach' again refers to the US online search popularity for 'beach'). There was also an abrupt rise at the start of 2016, again possibly attribute to the abormal high temperature (January 2016 Was the Most Abnormally Warm Month Ever Recorded: https://weather.com/news/climate/news/record-warmest-january-global-2016). There was a sharp drop in the search popularity around April 2020 (attributed apparently to the covid outbreak).
BEAST can also handle trend-only time series. Below is also an example to explain the meaining of tOrder and sOrder (e.g., the y labels in the figure above)..
y = [zeros(1,100) 1:100 99:-1:50 50*ones(1,250)] + 10*rand(1,500); % a trend-only time series without perodic/seasonal variation
o = beast(y, 'season','none') % season='none': y has no periodic/season component
plotbeast(o)
printbeast(o)
The trend is fitted using a piecewise polynomial model. Again, as a Bayesian method, BEAST assumes the order of the polynomial as uknowns. The orders of the polynomial needed to adequately fit the trend are estimated over time, as depicted iin the tOrder subplot. The 1st and 4th segments are flat lines, so their estimated poly orders are close to zeros.
uninstallbeast % Uninstall BEAST
0 Commenti
Star Strider
il 11 Nov 2020
I have no idea how those are calculated, or how the trend is calculated. On option is likely the timeseries version of detrend, another option is to convert your data to a timetable and use the retime function.
I have never done what you want to do, so I have no experience with these.
0 Commenti
Vedere anche
Categorie
Scopri di più su Time Series in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!