what is the fastest way to do many univariate regression? same Y against different x, one x at a time
조회 수: 4 (최근 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에서 Linear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!