How to fix Matrix dimension must agree when using a multiple regression?

1 view (last 30 days)
I am trying to find the residuals of the following formula:
x_t = alpha + beta1*xt-1 + beta2*xt_2 + u
When calculating the residuals, we do the following:
u = (alpa - beta1*xt_1 - beta2*xt_2)
I got it to work with an AR(1) model as follows:
l = 90;
h = 110;
x = (h-l).*rand(1000,1) + l;
a = 4.5831
b = 0.95
u=x(2:end) - a - b*x(1:end-1); # Works
However; when I extend this code to this:
b2 = 0.65
u=x(2:end) - a - b*x(1:end-1) - b2*x(2:end-2); # Falls, Matrix dimension must agree
Then I get the error:
Matrix dimensions must agree.
  1 Comment
dpb
dpb on 21 Sep 2019
u=x(2:end) - a - b*x(1:end-1) - b2*x(2:end-2);
If numel(x) = 100, say, then
2:end --> 100-2 = 98
1:end-1 --> (100-1)-1 --> 99-1 = 98
2:end-2 --> (100-2)-2 --> 98-2 = 96
The last term is shorter than the first two which is the problem in that you can't add vectors of disparate lengths.
Need to augment the last term with zero elements to make up the same overall length as LHS numel()

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!