what is the fastest way to do many univariate regression? same Y against different x, one x at a time

3 views (last 30 days)
bing cai
bing cai on 9 May 2015
Answered: bing cai on 9 May 2015
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?

Answers (2)

Alfonso Nieto-Castanon
Alfonso Nieto-Castanon on 9 May 2015
Edited: Alfonso Nieto-Castanon on 9 May 2015
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.

bing cai
bing cai on 9 May 2015
don't really get it...
But, do you know how to do it using for loop?

Community Treasure Hunt

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

Start Hunting!

Translated by