What is the best way to start a regression cycle in R?

Suppose I have data sources X and Y that are indexable, say matrices. And I want to run a set of independent regressions and save the result. My initial approach would be

results = matrix(nrow=nrow(X), ncol=(2))
for(i in 1:ncol(X)) {
        matrix[i,] = coefficients(lm(Y[i,] ~ X[i,])

}

But, the loops are bad, so I could do it with a foot like

out <- lapply(1:nrow(X), function(i) { coefficients(lm(Y[i,] ~ X[i,])) } )

Is there a better way to do this?

+3
source share
3 answers

, lm(). lsfit(), , , ( ). (X'X) ^ {- 1} X'y qr() qrcoef(). ; .

Z # design matrix
Y # matrix of observations (each row is a vector of observations)
## Estimation via multivariate multiple linear regression                    
beta <- qr.coef(qr(Z), Y)
## Fitted values                                                             
Yhat <- Z %*% beta
## Residuals                                                                 
u <- Y - Yhat

? , Z, .

0

, , . , - , . for-loop, .

+6

I am doing this type with plyr, but I agree that this is not a processing efficiency issue, nor is it convenient for you to read and write.

+1
source

Source: https://habr.com/ru/post/1741542/


All Articles