Filter löschen
Filter löschen

detrend using cubic splines

15 Ansichten (letzte 30 Tage)
Venkatessh
Venkatessh am 8 Feb. 2013
Kommentiert: Bruno Luong am 17 Okt. 2018
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.

Akzeptierte Antwort

Shashank Prasanna
Shashank Prasanna am 8 Feb. 2013
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 Kommentare
Shashank Prasanna
Shashank Prasanna am 8 Feb. 2013
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 am 8 Feb. 2013
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Matt J
Matt J am 8 Feb. 2013
Bearbeitet: Matt J am 8 Feb. 2013
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 Kommentare
Venkatessh
Venkatessh am 8 Feb. 2013
This is a clinical approach to the solution. Thanks.
Image Analyst
Image Analyst am 8 Feb. 2013
What does that mean?

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 8 Feb. 2013
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 Kommentare
SAAlly
SAAlly am 17 Okt. 2018
Bearbeitet: SAAlly am 17 Okt. 2018
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 am 17 Okt. 2018
It's still an arbitrary and poor choice IMO.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by