what is the fastest way to do many univariate regression? same Y against different x, one x at a time
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (2)
Alfonso Nieto-Castanon
le 9 Mai 2015
Modifié(e) : Alfonso Nieto-Castanon
le 9 Mai 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.
0 commentaires
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!