Hi, I wrote the code below to remove trends :
%%Removing trends without using detrend code
t = length(pass);
y = pass;
[r m b] = regression(t,y);
for t_pass = 1:t
y_pass(t_pass) = m * t_pass;
res(t_pass) = pass(t_pass) - y_pass(t_pass);
end
figure(3)
plot(y_pass)
but it isn't correct and I get wrong answer. would anyone help to how I should change this code to detrend my data?

2 commentaires

Rik
Rik le 30 Mar 2017
You are only plotting y_pass. Was that your intention, or should you plot res against y_pass?
BTW, it might be helpful to attach a sample of your data, as the succes of de-trending depends enormously on what data you put in.
Ghazal Hnr
Ghazal Hnr le 30 Mar 2017
Modifié(e) : Ghazal Hnr le 30 Mar 2017
I want to remove trends and plot my detrended data or calculate mean of it to show that I removed trends. Using detrend code I reached to this:

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 30 Mar 2017

0 votes

Try this:
t = 0:50; % Create ‘t’
y = sin(2*pi*t/5)+5 + t/5; % Create ‘y’
b_orig = polyfit(t,y,1); % Fit Linear Regression
y_detrend = y - polyval(b_orig, t); % Detrend Data
b_dt = polyfit(t,y_detrend,1);
figure(1)
plot(t, y,'-b', t,polyval(b_orig,t),'--r')
hold on
plot(t,y_detrend, '-k', t,polyval(b_dt,t),'--r')
hold off
See the documentation for the legend function to understand how to add the legend to your plot.

4 commentaires

Ghazal Hnr
Ghazal Hnr le 30 Mar 2017
Modifié(e) : Ghazal Hnr le 30 Mar 2017
Thank you, it worked.
%%Removing trends without using detrend code
t = ones(96,1);
for i = 1:96
t(i) = t(i) * i;
end
N = pass;
y_pass = polyfit(t,N,1);
N_pre = polyval(y_pass,t); % N_pre is what we predict for number of passengers
N_det = N - N_pre; % Detrend data
y_det = polyfit(t,N_det,1);
figure(3)
plot(N,'k','linewidth',3);
hold on;
plot(N_pre,':r','linewidth',3);
hold on
plot(N_det,'b','linewidth',3);
hold on
plot(polyval(y_det,t),':r','linewidth',3);
legend('Original Data','Trend','Detrended Data',...
'Mean of Detrended Data');
xlabel('Time (month)','fontsize',12,'fontweight','b');
ylabel('Number Of Passengers (*1000)','fontsize',12,'fontweight','b');
hold off
So it's not possible to do it with 'regression', right?
Star Strider
Star Strider le 30 Mar 2017
My pleasure.
It might be, but regression is a Neural Network Toolbox function, and you have to understand how to use neural networks to use it. The polyfit and polyval functions are the easiest ways to do what you want.
Ghazal Hnr
Ghazal Hnr le 31 Mar 2017
Thank you for your time.
Star Strider
Star Strider le 31 Mar 2017
My pleasure.

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by