[R] apply lm() to each row of a matrix

Martin Batholdy batholdy at googlemail.com
Sun Jan 29 23:05:47 CET 2012


Hi,


I would like to fit lm-models to a matrix with 'samples' of a dependent variable (each row represents one sample of the dependent variable).
The independent variable is a vector that stays the same:


y <- c(1:10)
x <- matrix(rnorm(5*10,0,1), 5, 10)



now I would like to avoid looping over the rows, since my original matrix is much larger;



for(t in 1:dim(x)[1]) {

	print(lm(y ~ x[t,]))

}


Is there a time-efficient way to do this?



More information about the R-help mailing list