detrend using cubic splines

11 次查看(过去 30 天)
How can I detrend a time series using cubic spline interpolation? I would like to get this done over for eg., 0.2 year bins.

采纳的回答

Shashank Prasanna
Venkatessh, detrending is just a process of removing long term deterministic patterns or in short trends. For example your data may look like it is exponentially increasing, but with variation. You are more interested in the variation because the exponential increase renders the data non-stationary.
It is as simple as fitting your data to the best possible curve and subtracting it from your data.
Simple example:
Generate data and get a spline fitted trend:
x = 0:100;
y = 100*randn(1,length(x))+x.^2;
xx = 0:10:100;
yy = spline(x,y,xx);
Substract the trend from your original data and plot the detrended variation sequence.
ytrend = interp1(xx,yy,x,'spline');
subplot(2,1,1)
plot(x,y,'.',x,ytrend);
subplot(2,1,2)
plot(x,y-ytrend)
  18 个评论
Shashank Prasanna
Honestly, I don't know if there is a definition written on stone somewhere east of here, but that is atleast the accepted convention. All the following methods fit into the non-parametric model framework: Splines, Neural Networks, SVM (and other kernel estimators), regression trees etc
Anyway this made for a nice discussion. I wish this topic wasn't embedded in another post, would loved to have see more responses.
Image Analyst
Image Analyst 2013-2-8
I'm not sure what "I need to be very careful in discarding the data. " means. Of course any type of fitting, regression, or detrending is going to discard data. Even though the data may go into creating the fit, eventually the data is going to be discarded and replaced by the fitted/estimated/smoothed values. If you didn't want to do that, you'd just keep your original data. So you are going to discard data and replace it - the only issue to decide is how much to smooth the data.

请先登录,再进行评论。

更多回答(2 个)

Matt J
Matt J 2013-2-8
编辑:Matt J 2013-2-8
Here's an example of how to do a cubic spline regression using interpMatrix. You would have to choose how finely you wanted to space the control points, which would affect the stiffness of the spline fit. In the example, the control points occur every 9 samples. After obtaining the 'trend' you would of course subtract it off the original time series to detrend it.
s = @(t) cos(2*pi*t).*exp(-abs(2*t))+ 2; %timeseries to fit
cubicBspline = @(t) (t>-1 & t<1).*(2/3 - t.^2 +abs(t).^3/2) +...
(abs(t)>=1 & abs(t)<2).*((2-abs(t)).^3/6);
tCtrlPts=linspace(-1.2, 1.2,9); %CtrlPts sample locations on t-axis
dtCtrlPts=tCtrlPts(2)-tCtrlPts(1);
tFine=linspace(-1.2, 1.2,81); %Fine sample locations on t-axis
dtFine=tFine(2)-tFine(1);
timeseries=s(tFine(:));
%create regression matrix
SampRatio=round(dtCtrlPts/dtFine); %Sampling ratio
kernel=cubicBspline(-2:1/SampRatio:2 );
nCtrlPts=length(tCtrlPts);
A=interpMatrix(kernel, 'max', nCtrlPts, SampRatio, 'mirror');
%%Do the fit!!!
trend = A*(A\timeseries);
plot(tFine,timeseries,tFine,trend,'*-');
legend('Time Series','Fitted Trend')
  2 个评论
Venkatessh
Venkatessh 2013-2-8
This is a clinical approach to the solution. Thanks.

请先登录,再进行评论。


Image Analyst
Image Analyst 2013-2-8
Because a spline is an interpolation rather than a regression, and so it goes through all the points, I don't see how it could detrend. Why not use detrend() or sgolay()?
  2 个评论
SAAlly
SAAlly 2018-10-17
编辑:SAAlly 2018-10-17
Hi there, if you look at Shashank Prasanna's answer you'll notice x has a step of 1, while xx has a step of 10. Therefore the spline would only go through every tenth point.
Bruno Luong
Bruno Luong 2018-10-17
It's still an arbitrary and poor choice IMO.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Spline Postprocessing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by