Skip to content

lmList, tapply() and lm()

3 messages · Marc Belisle, Chuck Cleland, Jonathan Baron

#
Howdee,

*** I know that the lmList() function exists, yet I don't want to use it.
***

Would anyone be kind enough to tell how I can apply the function lm() to
each level of a given factor so to obtain the intercept and slope for each
factor level within a matrix?

For instance, suppose a dataframe containing 3 variables: id, x and y.

I want to compute the function lm() for each level contained in id, as
lmList would do...

Thanks for your time,

Marc

===================
Marc B?lisle
Professeur adjoint
Chaire de recherche du Canada en ?cologie spatiale et en ?cologie du paysage
D?partement de biologie
Universit? de Sherbrooke
2500 Boul. de l'Universit?
Sherbrooke, Qu?bec
J1K 2R1 Canada

T?l: +1-819-821-8000 poste 61313
Fax: +1-819-821-8049
Courri?l: Marc.M.Belisle at USherbrooke.ca
#
On 2/15/2008 11:00 AM, Marc Belisle wrote:
Something like this?

t(sapply(split(df, list(df$id)),
    function(subd){coef(lm(y ~ x, data = subd))}))

  
    
#
Here is an example I just did.  I _LOVE_ lmList().  It is such a great
convenience compared to the following.  But right now it doesn't work
for "family=binomial", so I had to do it differently.  This is not
your example, but it should give you an idea.  The [[5]] was because I
was interested only in the 5th coefficient for each subject (the
interaction term).  Note that I used coef() because I was interested
only in the coefficients.

l1g <- by(d1,subject,function(x)
          coef(glm(achoice ~ lnlra*session+lnpodd,data=x,family=binomial))[[5]])
On 02/15/08 11:00, Marc Belisle wrote: