[R] how can I save the estimates of a regression model in a file?
Dear all I need some help with R. How can I save the estimates of a regression model in a file? here is what I did: 1) this is my regression model: fit1 - lm(logmilk ~ logdays + days, data=data2) 2) however, I want to get the parameters estimates for each individual (by group): so i did the following: by(data2, list(data2$V2),function(.data2) lm(logmilk ~ logdays + days, data= .data2)) 3) Then to keep the estimates in a file I did: res1 - by(data2, list(data2$V2),function(.data2) lm(logmilk ~ logdays + V5, data= .data2)) 4) and this is what I get for each individual: : 1 Call: lm(formula = logmilk ~ logdays + days, data = .data2) Coefficients: (Intercept) logdays days 3.414105 0.069387-0.001732 --- : 2 Call: lm(formula = logmilk ~ logdays + days, data = .data2) Coefficients: (Intercept) logdays days 3.22761140.1223412 -0.0006836 and so on: 5) THE QUESTION is: there is a way in R to get an output file as you get in SAS when you use: prog reg data=xxx outest=estimate; I would need an output that looks like this: individual intercept logdays days etc 1 3.4141050.069387 -0.0006836 2 3.2276114 0.1223412 -0.0006836 n .. . THANKS FOR YOUR HELP Maria -- - [EMAIL PROTECTED] [[alternative HTML version deleted]] __ R-help@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.
Re: [R] how can I save the estimates of a regression model in a file?
On 11/5/2008 12:12 PM, pilar schneider wrote: Dear all I need some help with R. How can I save the estimates of a regression model in a file? here is what I did: 1) this is my regression model: fit1 - lm(logmilk ~ logdays + days, data=data2) 2) however, I want to get the parameters estimates for each individual (by group): so i did the following: by(data2, list(data2$V2),function(.data2) lm(logmilk ~ logdays + days, data= .data2)) 3) Then to keep the estimates in a file I did: res1 - by(data2, list(data2$V2),function(.data2) lm(logmilk ~ logdays + V5, data= .data2)) 4) and this is what I get for each individual: : 1 Call: lm(formula = logmilk ~ logdays + days, data = .data2) Coefficients: (Intercept) logdays days 3.414105 0.069387-0.001732 --- : 2 Call: lm(formula = logmilk ~ logdays + days, data = .data2) Coefficients: (Intercept) logdays days 3.22761140.1223412 -0.0006836 and so on: 5) THE QUESTION is: there is a way in R to get an output file as you get in SAS when you use: prog reg data=xxx outest=estimate; I would need an output that looks like this: individual intercept logdays days etc 1 3.4141050.069387 -0.0006836 2 3.2276114 0.1223412 -0.0006836 n .. . THANKS FOR YOUR HELP Maria Consider something like this: as.data.frame(t(sapply(split(data2, data2$V2), function(x){coef(lm(logmilk ~ logdays + days, data = x))}))) -- 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 __ R-help@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.
Re: [R] how can I save the estimates of a regression model in a file?
On Wed, Nov 5, 2008 at 11:12 AM, pilar schneider [EMAIL PROTECTED] wrote: Dear all I need some help with R. How can I save the estimates of a regression model in a file? here is what I did: 1) this is my regression model: fit1 - lm(logmilk ~ logdays + days, data=data2) 2) however, I want to get the parameters estimates for each individual (by group): so i did the following: by(data2, list(data2$V2),function(.data2) lm(logmilk ~ logdays + days, data= .data2)) 3) Then to keep the estimates in a file I did: res1 - by(data2, list(data2$V2),function(.data2) lm(logmilk ~ logdays + V5, data= .data2)) Here's one way that uses the plyr package (http://had.co.nz/) # Name the model fitting function fit_model - function(df) lm(logmilk ~ logdays + days, data = df) # Create a list of models for each value of V2 # this is basically equivalent to by, but it deals with labelling a bit better models - dlply(data2, .(V2), fit_model) # Produce a data frame containing the coefficients for each model ldply(models, coef) Hadley -- http://had.co.nz/ __ R-help@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.