[R] how can I save the estimates of a regression model in a file?

hadley wickham h.wickham at gmail.com
Wed Nov 5 20:41:35 CET 2008


On Wed, Nov 5, 2008 at 11:12 AM, pilar schneider
<pilar.schneider at gmail.com> 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/



More information about the R-help mailing list