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

Bert Gunter gunter.berton at gene.com
Sun Jan 29 23:52:19 CET 2012


Reread ?lm and note that the lhs can be a matrix. I believe this is
exactly what you want.

-- Bert

On Sun, Jan 29, 2012 at 2:05 PM, Martin Batholdy
<batholdy at googlemail.com> wrote:
> 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?
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list