what is the fastest way to do many univariate regression? same Y against different x, one x at a time
2 次查看(过去 30 天)
显示 更早的评论
try to use for loop
i have Y, a column vector, 133x1 and a data matrix, X, 133x6; the first column are ones, starting from second column x1, x2,x3, x4, and x5
i was thinking to use for loop e.g. a=1:5 Z=[X(:,1) X(:,1+a)] % so each time, it takes the column of ones and one x variable and form a new matrix of 133x2
then to run another loop to regress Y against each of those new matrix
anyone has another idea how to do?
0 个评论
回答(2 个)
Alfonso Nieto-Castanon
2015-5-9
编辑:Alfonso Nieto-Castanon
2015-5-9
Not terribly different in practice, but if you want you could compute all of those regressions simultaneously using a sparse block diagonal data matrix, e.g.:
[N,M] = size(X);
Xblk = sparse(repmat(1:N*(M-1),2,1)', repmat([1:2:2*(M-1) 2:2:2*(M-1)],N,1), X(:,[ones(1,M-1) 2:M]));
B = Xblk \ repmat(Y,M-1,1);
B will contain first the two coefficients of the first regression, followed by the two coefficients of the second regression, etc.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!