Extending a sine regression to forecast

3 vues (au cours des 30 derniers jours)
Clifford Shelton
Clifford Shelton le 6 Mai 2012
I would like to know the best way to extend my newly constructed sine wave fit to my data into the future another 20 days.
I have constructed a regression of 151 different sine waves to a data set across 501 days at a sample of 1 day.
y = Score(:);
n = 501;
t = (1:501)';
games = 1:501;
data(1:151) = struct('X',NaN(501,3),'bhat',NaN(3,1),'yhat',NaN);
for ii = 1:151
tmp = 2*pi*(sincos(ii))*t;
data(ii).X = rand(501,3);
data(ii).X(:,2) = cos(tmp)';
data(ii).X(:,3) = sin(tmp)';
data(ii).bhat = data(ii).X\y;
data(ii).yhat = data(ii).bhat(1)+data(ii).bhat(2)*cos(tmp)+data(ii).bhat(3)*sin(tmp);
end
After that, I have combined or superposed all of the 151 different sin waves into one sine wave
sum(horzcat(data.yhat),2) ./ 151
So ideally, I would like the combined sine wave to extend to 521 days (for a 20 day forecast) while my data set remains to be only 501 days long and plot them both. Help much appreciated!

Réponse acceptée

Wayne King
Wayne King le 6 Mai 2012
n = (0:500)';
% create some fake data
x = 1.5*cos(2*pi*(1/4)*n)+randn(size(n));
X = ones(501,3);
X(:,2) = cos(2*pi*(1/4)*n);
X(:,3) = sin(2*pi*(1/4)*n);
beta = X\x;
nn = 0:520;
xhat = beta(1)+beta(2)*cos(2*pi*(1/4)*n)+beta(3)*sin(2*pi*(1/4)*n);
xhatpred = beta(1)+beta(2)*cos(2*pi*(1/4)*(501:520)')+beta(3)*sin(2*pi*(1/4)*(501:520)');
xhat = [xhat; xhatpred];
plot(n,x,'k');
hold on;
plot(1:521,xhat,'r');
set(gca,'xlim',[490 512]);
legend('original data','prediction for next 21 days');
grid on;
  3 commentaires
Clifford Shelton
Clifford Shelton le 6 Mai 2012
Does it seem strange to you that the 'original' data and 'prediction' data aren't in line with each other at all before the forecast? I thought they should be more in line so as to have a more accurate forecast. U?
Thanks for all your help!
Wayne King
Wayne King le 6 Mai 2012
I would not expect them to agree exactly. You have to keep in mind that the regression model will not fit the data exactly. The regression model parameters minimize the overall sum of squares of the residuals.

Connectez-vous pour commenter.

Plus de réponses (1)

Wayne King
Wayne King le 6 Mai 2012
Once you have the parameters, which are the amplitudes of the cosines and sines, then you can easily extend your model to make predictions by simply increasing the length of the time vector. Of course how far into the future you can make reasonable predictions is a tricky question.
n = (0:500)';
% create some fake data
x = 1.5*cos(2*pi*(1/4)*n-pi/4)+randn(size(n));
X = ones(501,3);
X(:,2) = cos(2*pi*(1/4)*n);
X(:,3) = sin(2*pi*(1/4)*n);
beta = X\x;
nn = 0:520;
xhat = beta(1)+beta(2)*cos(2*pi*(1/4)*nn)+beta(3)*sin(2*pi*(1/4)*nn);
  2 commentaires
Clifford Shelton
Clifford Shelton le 6 Mai 2012
ok..but I'm still not sure how to plot the extension.
When I try to plot it...the plot ends where my last data point is in the original data. :-(
Clifford Shelton
Clifford Shelton le 6 Mai 2012
umm..furthermore when I add the nn variable and add it to my calculations of the xhat varable..it affects the fit drastically.
I like the fit I am able to generate originally. Is there a simpler way to keep the current fit and then just project the xhat line forward a few more steps?
Thanks a bunch!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by