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?
If it's a simple one variable OLS regression and you only need regression coefficients, you'll probably get best performance by hard-coding the closed form solutions. apply() might help a little (since it's a very good loop) but ultimately you'll be best served by deciding exactly what you want and calculating that. If you feel more comfortable setting up the regression yourself, you can eliminate R's work in setting up the regression model & go straight to the lm.fit() workhorse inside of lm(). Perhaps you can say a little more about what exactly you need? Michael On Sun, Jan 29, 2012 at 5: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.
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