detrend using cubic splines

조회 수: 7 (최근 30일)
Venkatessh
Venkatessh 2013년 2월 8일
댓글: Bruno Luong 2018년 10월 17일
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
Shashank Prasanna 2013년 2월 8일
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
Shashank Prasanna 2013년 2월 8일
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일
What does that mean?

댓글을 달려면 로그인하십시오.


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