How to fix Matrix dimension must agree when using a multiple regression?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2019-9-21
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()
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!