All - Not sure if this is a real programming question, but here goes: I have data that looks like Lake Length Weight 1 158 45 1 179 70 1 200 125 1 202 150 1 206 145 1 209 165 1 210 140 1 215 175 1 216 152 1 220 150 1 221 165 ... where lake goes from 1 - 84 and the number of rows for each lake is variable (but > ~20). I'm trying to do two things: 1) build a simple linear model of the form {lm(log10(Weight)~log10(Length)} for every separate lake in the data set; 2) I'd like to save the intercepts and slopes from each of these linear regressions into a seperate data frame. Any ideas? I think it would probably require some kind of 'for' statement, but I'm just not that smart. Thanks for your help, SR Steven H. Ranney Graduate Research Assistant (Ph.D) USGS Montana Cooperative Fishery Research Unit Montana State University PO Box 173460 Bozeman, MT 59717-3460 phone: (406) 994-6643 fax: (406) 994-7479 [[alternative HTML version deleted]]
On 8/22/2008 5:34 PM, Ranney, Steven wrote:> All - > > Not sure if this is a real programming question, but here goes: > > I have data that looks like > > Lake Length Weight > 1 158 45 > 1 179 70 > 1 200 125 > 1 202 150 > 1 206 145 > 1 209 165 > 1 210 140 > 1 215 175 > 1 216 152 > 1 220 150 > 1 221 165 > ... > > where lake goes from 1 - 84 and the number of rows for each lake is variable (but > ~20). > I'm trying to do two things: 1) build a simple linear model of the form > > {lm(log10(Weight)~log10(Length)} > > for every separate lake in the data set; 2) I'd like to save the intercepts and slopes > from each of these linear regressions into a seperate data frame. Any ideas? I think it would > probably require some kind of 'for' statement, but I'm just not that smart.Assuming the data are in a data frame called "mydf": library(nlme) fm1 <- lmList(log10(Weight)~log10(Length) | Lake, mydf) coef(fm1) ?lmList or t(sapply(split(mydf, mydf$Lake), function(x){coef(lm(log10(Weight)~log10(Length), data=x))}))> Thanks for your help, > > SR > > Steven H. Ranney > Graduate Research Assistant (Ph.D) > USGS Montana Cooperative Fishery Research Unit > Montana State University > PO Box 173460 > Bozeman, MT 59717-3460 > > phone: (406) 994-6643 > fax: (406) 994-7479 > > > [[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. >-- Chuck Cleland, Ph.D. NDRI, Inc. (www.ndri.org) 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
Dear Steven, Take a look at the lmList function in the nlme package. It does what you want to do. HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx op inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces op r-project.org [mailto:r-help-bounces op r-project.org] Namens Ranney, Steven Verzonden: vrijdag 22 augustus 2008 23:34 Aan: r-help op r-project.org Onderwerp: [R] Newbie programming help All - Not sure if this is a real programming question, but here goes: I have data that looks like Lake Length Weight 1 158 45 1 179 70 1 200 125 1 202 150 1 206 145 1 209 165 1 210 140 1 215 175 1 216 152 1 220 150 1 221 165 ... where lake goes from 1 - 84 and the number of rows for each lake is variable (but > ~20). I'm trying to do two things: 1) build a simple linear model of the form {lm(log10(Weight)~log10(Length)} for every separate lake in the data set; 2) I'd like to save the intercepts and slopes from each of these linear regressions into a seperate data frame. Any ideas? I think it would probably require some kind of 'for' statement, but I'm just not that smart. Thanks for your help, SR Steven H. Ranney Graduate Research Assistant (Ph.D) USGS Montana Cooperative Fishery Research Unit Montana State University PO Box 173460 Bozeman, MT 59717-3460 phone: (406) 994-6643 fax: (406) 994-7479 [[alternative HTML version deleted]] ______________________________________________ R-help op 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. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document