Asked by Clifford Shelton
on 16 Dec 2012

I am having to do regressions of data regularly so I need help with the following:

On a graph I have two lines:

1) the randomly generated data.

2) a line forecasting into the future 5 additional data points 'fit' to the actual data line.

If you cut and paste the code below into Matlab, you will visually see that my forecasting line needs to be "stretched" vertically in order to fit with the random data graph line.

Anyone know how to code that to happen?

This is difficult to explain... so I've included some example code that can be run with a simple cut & paste into Matlab to see what I am trying to explain. Help is MOST APPRECIATED!!!

%Some randomly generated data and variables needed to do the regression

Score = rand(1,192); % stream.State = savedState; Allow = rand(1,192); Size = 192; Scorecycle = 95; Allowcycle = 96; %The length of the data will be 192 steps. I will create a forecast line %an additional 5 steps. Forecast = 197;

%Additional Variables to do the mathematics needed in a regression FourierScore = fft(Score); FourierScore(1)=[]; A = length(FourierScore); powerScore = abs(FourierScore(1:floor(A/2))).^2; powerScore = powerScore(:); nyquist = 1/2; freqScore = (1:A/2)/(A/2)*nyquist; freqScore = freqScore(:);

FourierAllow = fft(Allow); FourierAllow(1)=[]; B = length(Allow); powerAllow = abs(FourierAllow(1:floor(B/2))).^2; powerAllow = powerAllow(:); freqAllow = (1:B/2)/(B/2)*nyquist; freqAllow = freqAllow(:);

Size = Size; Forecast = Forecast; Scorecycles = Scorecycle; Allowcycles = Allowcycle;

FourierScore = fft(Score); FourierScore(1)=[]; A = length(FourierScore); powerScore = abs(FourierScore(1:floor(B/2))).^2; powerScore = powerScore(:); nyquist = 1/2; freqScore = (1:A/2)/(A/2)*nyquist; freqScore = freqScore(:);

FourierAllow = fft(Allow); FourierAllow(1)=[]; B = length(Allow); powerAllow = abs(FourierAllow(1:floor(B/2))).^2; powerAllow = powerAllow(:); freqAllow = (1:B/2)/(B/2)*nyquist; freqAllow = freqAllow(:);

yScore = Score(:); yAllow = Allow(:);

n = Size; t = (1:Size)'; tt = (Size:Forecast);

games = 1:Size;

%The Regression for the Data called "Score" is started here

Scoredata(1:4) = struct('XScore',NaN(Size,3),'bhatScore',NaN(3,1),'yhatScore',NaN,'yhatScorePred',NaN);

for ii = 1:Scorecycles tmpScore = 2*pi*(freqScore(ii))*t; tmpScore2 = 2*pi*(freqScore(ii))*(tt)'; Scoredata(ii).XScore = rand(Size,3); Scoredata(ii).XScore(:,2) = cos(tmpScore)'; Scoredata(ii).XScore(:,3) = sin(tmpScore)'; Scoredata(ii).bhatScore = Scoredata(ii).XScore\yScore; Scoredata(ii).yhatScore = Scoredata(ii).bhatScore(1)+Scoredata(ii).bhatScore(2)*cos(tmpScore)+Scoredata(ii).bhatScore(3)*sin(tmpScore); Scoredata(ii).yhatScorePred = Scoredata(ii).bhatScore(1)+Scoredata(ii).bhatScore(2)*cos(tmpScore2)+Scoredata(ii).bhatScore(3)*sin(tmpScore2);

end

yhatScore = [Scoredata.yhatScore]; yhatScoreM = sum(horzcat(Scoredata.yhatScore),2) ./Scorecycles; yhatScoreMPred = sum(horzcat(Scoredata.yhatScorePred),2) ./Scorecycles; yhatScoreForecast = [yhatScoreM; yhatScoreMPred]; %A forecast line for the data called "Score" is completed

%The Regression for the Data called "Allow" is done here

yAllow = Allow(:); Allowdata(1:4) = struct('XAllow',NaN(Size,3),'bhatAllow',NaN(3,1),'yhatAllow',NaN,'yhatAllowPred',NaN);

for xx = 1:Allowcycles tmpAllow = 2*pi*(freqAllow(xx))*t; tmpAllow2 = 2*pi*(freqAllow(xx))*(tt)'; Allowdata(xx).XAllow = rand(Size,3); Allowdata(xx).XAllow(:,2) = cos(tmpAllow)'; Allowdata(xx).XAllow(:,3) = sin(tmpAllow)'; Allowdata(xx).bhatAllow = Allowdata(xx).XAllow\yAllow; Allowdata(xx).yhatAllow = Allowdata(xx).bhatAllow(1)+Allowdata(xx).bhatAllow(2)*cos(tmpAllow)+Allowdata(xx).bhatAllow(3)*sin(tmpAllow); Allowdata(xx).yhatAllowPred = Allowdata(xx).bhatAllow(1)+Allowdata(xx).bhatAllow(2)*cos((tmpAllow2))+Allowdata(xx).bhatAllow(3)*sin((tmpAllow2));

end

yhatAllow = [Allowdata.yhatAllow]; yhatAllowM = sum(horzcat(Allowdata.yhatAllow),2) ./Allowcycles; yhatAllowMPred = sum(horzcat(Allowdata.yhatAllowPred),2) ./Allowcycles; yhatAllowForecast = [yhatAllowM; yhatAllowMPred]; %A forecast line for the data called "Allow" is completed

%The two Regressions are plotted in a yyplot here %The Random Data is in Blue %The Forecast Line is in Green %I have NO CLUE how to code so that the Green and Blue lines fit together %both vertically and horizontally.

%In other words, is there a way to have the minimums and maximums of both %lines match up better?? subplot (2,1,1), plotyy(t,yScore,1:Forecast+1,yhatScoreForecast)

title(' How Do I Code so that both lines on each graphs fit better vertically?');

subplot (2,1,2), plotyy(t,yAllow,1:Forecast+1,yhatAllowForecast)

*No products are associated with this question.*

Answer by Image Analyst
on 16 Dec 2012

Accepted answer

Wow, I don't think I've ever seen such a nice demo created to illustrate the problem for us. Thank you for that. You need to set the ylim property of the axes. Replace your last line of code with this:

subplot (2,1,2); [axesHandles,H1,H2] = plotyy(t,yAllow,1:Forecast+1,yhatAllowForecast) set(axesHandles(1), 'ylim', [0 1]) lowerBound = min(yhatAllowForecast); upperBound = max(yhatAllowForecast); set(axesHandles(2), 'ylim', [lowerBound upperBound]) set(H2, 'LineWidth', 2); % Make line 2 thicker and more noticeable. % Enlarge figure to full screen. set(gcf, 'units','normalized','outerposition',[0 0 1 1]);

Clifford Shelton
on 16 Dec 2012

Related Content

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreOpportunities for recent engineering grads.

Apply Today
## 0 Comments