Folks, I have a 3000 x 4 matrix (y), which I need to regress row-by-row against a 4-vector (x) to create a matrix lm.y of intercepts and slopes. To illustrate: y <- matrix(rnorm(12000), ncol = 4) x <- c(1/12, 3/12, 6/12, 1) system.time(lm.y <- t(apply(y, 1, function(z) lm(z ~ x)$coefficient))) [1] 44.72 18.00 69.52 NA NA Takes more than a minute to do (and I need to do many similar regressions a day). Is there a more efficient way of handling this? I'm running R 2.4.1 on Windows XP Service Pack 2 on a Intel Xeon dual-core 2.66GHz with 3GB RAM. Thanks very much, Murali --------------------------------------------------------------------------- This message (including any attachments) is confidential and...{{dropped}}
murali.menon at uk.abnamro.com said the following on 9/19/2007 9:50 AM:> Folks, > > I have a 3000 x 4 matrix (y), which I need to regress row-by-row against a > 4-vector (x) to create a > matrix lm.y of intercepts and slopes. To illustrate: > > y <- matrix(rnorm(12000), ncol = 4) > x <- c(1/12, 3/12, 6/12, 1) > > system.time(lm.y <- t(apply(y, 1, function(z) lm(z ~ x)$coefficient))) > [1] 44.72 18.00 69.52 NA NA > > Takes more than a minute to do (and I need to do many similar regressions > a day). > > Is there a more efficient way of handling this? > > I'm running R 2.4.1 on Windows XP Service Pack 2 on a Intel Xeon dual-core > 2.66GHz with 3GB RAM. > > Thanks very much, > > Murali >Yes. Try, set.seed(1) y <- matrix(rnorm(12000), ncol = 4) x <- c(1/12, 3/12, 6/12, 1) system.time(lm.y <- t(apply(y, 1, function(z) lm(z ~ x)$coefficient))) ## user system elapsed ## 1.10 0.00 38.69 system.time(lm.y2 <- t(coef(lm(t(y) ~ x)))) ## user system elapsed ## 0.02 0.00 0.04 all.equal(lm.y, lm.y2) ## [1] TRUE HTH, --sundar
murali.menon at uk.abnamro.com wrote:> Folks, > > I have a 3000 x 4 matrix (y), which I need to regress row-by-row against a > 4-vector (x) to create a > matrix lm.y of intercepts and slopes. To illustrate: > > y <- matrix(rnorm(12000), ncol = 4) > x <- c(1/12, 3/12, 6/12, 1) > > system.time(lm.y <- t(apply(y, 1, function(z) lm(z ~ x)$coefficient))) > [1] 44.72 18.00 69.52 NA NA > > Takes more than a minute to do (and I need to do many similar regressions > a day). > > Is there a more efficient way of handling this? > > I'm running R 2.4.1 on Windows XP Service Pack 2 on a Intel Xeon dual-core > 2.66GHz with 3GB RAM. > > Thanks very much, > > Muraliy <- matrix(rnorm(12000), ncol = 4) x <- c(1/12, 3/12, 6/12, 1) system.time(lm.y1 <- t(coef(lm(t(y) ~ x)))) user system elapsed 0.03 0.00 0.04 system.time(lm.y2 <- t(apply(y, 1, function(z) lm(z ~ x)$coefficient))) user system elapsed 19.70 0.05 20.45 all.equal(lm.y1, lm.y2) [1] TRUE> > --------------------------------------------------------------------------- > This message (including any attachments) is confidential and...{{dropped}} > > ______________________________________________ > 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.-- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894
> system.time( lm( t(y) ~ x ) )user system elapsed 0.008 0.000 0.010 On Wed, 19 Sep 2007, murali.menon at uk.abnamro.com wrote:> Folks, > > I have a 3000 x 4 matrix (y), which I need to regress row-by-row against a > 4-vector (x) to create a > matrix lm.y of intercepts and slopes. To illustrate: > > y <- matrix(rnorm(12000), ncol = 4) > x <- c(1/12, 3/12, 6/12, 1) > > system.time(lm.y <- t(apply(y, 1, function(z) lm(z ~ x)$coefficient))) > [1] 44.72 18.00 69.52 NA NA > > Takes more than a minute to do (and I need to do many similar regressions > a day). > > Is there a more efficient way of handling this? > > I'm running R 2.4.1 on Windows XP Service Pack 2 on a Intel Xeon dual-core > 2.66GHz with 3GB RAM. > > Thanks very much, > > Murali > > > --------------------------------------------------------------------------- > This message (including any attachments) is confidential and...{{dropped}} > > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901