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:> 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...Something like this? t(sapply(split(df, list(df$id)), function(subd){coef(lm(y ~ x, data = subd))}))> 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 > > ______________________________________________ > 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
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:> 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,-- Jonathan Baron, Professor of Psychology, University of Pennsylvania Home page: http://www.sas.upenn.edu/~baron