[R] efficient use of lm over a matrix vs. using apply over rows

Gabor Grothendieck ggrothendieck at gmail.com
Sun Oct 5 19:36:52 CEST 2008


Sorry, I missed the fact that Duncan had this solution as well at
the end of his response.

On Sun, Oct 5, 2008 at 1:31 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> In that case, using the example data from the prior response all you need
> is:
>
> coef(lm(t(mat) ~ x))
>
>
> On Sun, Oct 5, 2008 at 1:18 PM, Mark Kimpel <mwkimpel at gmail.com> wrote:
>> Sorry for the vagueness of my question, your interpretation, however, was
>> spot on. Correct me if I am wrong, but my impression is that apply is a more
>> compact way of a for loop, but that the way R handles them computationally
>> are the same. In the article I seem to remember, there was a significant
>> increase in speed with your second approach, presumably because function
>> calls are avoided in R and the heavy lifting is done in C. I will use your
>> second approach anyway, but can I expect increased computational efficiency
>> with it and, if so, is my reasoning in the prior sentence correct?
>>
>> BTW, it appears as though my own attempt was almost correct, but I did not
>> transpose the matrix. In genomics, our response variables (genes) are the
>> rows and the predictor values are the column names. The BioConductor
>> packages I routinely use are very good at hiding this and I just didn't come
>> to mind.
>>
>> Mark
>> ------------------------------------------------------------
>> Mark W. Kimpel MD  ** Neuroinformatics ** Dept. of Psychiatry
>> Indiana University School of Medicine
>>
>> 15032 Hunter Court, Westfield, IN  46074
>>
>> (317) 490-5129 Work, & Mobile & VoiceMail
>> (317) 399-1219  Home
>> Skype:  mkimpel
>>
>> ******************************************************************
>>
>>
>> On Sun, Oct 5, 2008 at 10:28 AM, Duncan Murdoch <murdoch at stats.uwo.ca>wrote:
>>
>>> On 05/10/2008 10:08 AM, Mark Kimpel wrote:
>>>
>>>> I have a large matrix, each row of which needs lm applied. I am certain
>>>> than
>>>> I read an article in R-news about this within the last year or two that
>>>> discussed the application of lm to matrices but I'll be darned if I can
>>>> find
>>>> it with Google. Probably using the wrong search terms.
>>>>
>>>> Can someone steer me to this article of just tell me if this is possible
>>>> and, if so, how to do it? My simplistic attempts have failed.
>>>>
>>>
>>> You don't give a lot of detail on what you mean by applying lm to a row of
>>> a matrix, but I'll assume you have fixed predictor variables, and each row
>>> is a different response vector.  Then you can use apply() like this:
>>>
>>> x <- 1:10
>>> mat <- matrix(rnorm(200), nrow=20, ncol=10)
>>>
>>> resultlist <- apply(mat, 1, function(y) lm(y ~ x))
>>> resultcoeffs <- apply(mat, 1, function(y) lm(y ~ x)$coefficients)
>>>
>>>
>>> "resultlist" will contain a list of 20 different lm() results,
>>> "resultcoeffs" will be a matrix holding just the coefficients.
>>>
>>> lm() also allows the response to be a matrix, where the columns are
>>> considered different components of a multivariate response.  So if you
>>> transpose your matrix you can do it all in one call:
>>>
>>> resultmulti <- lm(t(mat) ~ x)
>>>
>>> The coefficients of resultmulti will match resultcoeffs.
>>>
>>> Duncan Murdoch
>>>
>>> Duncan Murdoch
>>>
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>



More information about the R-help mailing list