Print

Print


I agree that broom is the right way to go here, and a valuable tool to
learn if you're planning to generate multiple models on data subsets.

I will just add that a useful "trick" is to use purrr::transpose for
dealing with lists of lists:
https://urldefense.proofpoint.com/v2/url?u=http-3A__purrr.tidyverse.org_reference_transpose.html&d=DwIBaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=ERABobiQkGrOZ7U5a2Ov3ETwpy2CAEkdpLYOVb_cRGI&s=1jIoulNC3xqhEI6IqzdYtSP-A9N9I0J_EZs4bpP7FYc&e=

In this case, applying it to a list of `lm` models should allow you to pull
out a single named element for the coefficients:
```
model_multi_1t_transpose <- purrr::transpose(model_multi_1t)
model_multi_1t_transpose$coefficients
```

Best,
--
Hao Ye
[log in to unmask]

On Wed, Feb 21, 2018 at 7:44 AM, Shawn Taylor <[log in to unmask]>
wrote:

> Thanks! I was just about to suggest this. But I usually run into issues
> with the variables inside the lm() function, if that gives any trouble try
>
> library(dplyr)
>
> model_multi_1T = bb_1 %>%
>     group_by(HERDCODE) %>%
>     do(broom::tidy( lm(.$conc_dry ~ .$pregsea, data=.)))
>
>
>
>
> On Wed, Feb 21, 2018 at 9:29 AM, Li,Daijiang <[log in to unmask]> wrote:
>
> > Try this:
> >
> > library(dplyr)
> > library(broom)
> > bb_1 %>%
> >   group_by(HERDCODE) %>%
> >   do(broom::tidy(lm(conc_dry ~ pregsea, data = .)))
> >
> >
> > ________________________________________
> > From: UF R Users List <[log in to unmask]> on behalf of
> > Pattamanont,Pornpamol <[log in to unmask]>
> > Sent: Wednesday, February 21, 2018 8:21 AM
> > To: [log in to unmask]
> > Subject: Re: How to store result of coefficients from the model
> >
> > coef(mode) doesn't work for me as well. It gives only 1 group at a time.
> > [cid:bca9fab7-94fc-4abb-bfd8-23d485e0f793]
> >
> > Do I need to write a loop to extract results for 1,200 groups? I don't
> > know how to write it anyway T_T.
> > ________________________________
> > From: Pattamanont,Pornpamol
> > Sent: Wednesday, February 21, 2018 8:18:20 AM
> > To: [log in to unmask]
> > Subject: Re: How to store result of coefficients from the model
> >
> >
> > It works for the whole data, but I want the results by group.
> >
> >
> > > broom::tidy(lm(as.numeric(bb_1$conc_dry) ~ bb_1$pregsea, data=bb_1))
> >            term     estimate std.error   statistic      p.value
> > 1   (Intercept) 166.78889823 0.2942663 566.7957739 <(566)%20795-7739>
> > 0.000000e+00
> > 2 bb_1$pregsea2  -0.54991912 0.4077014  -1.3488283 1.774491e-01
> > 3 bb_1$pregsea3   0.09162663 0.3971105   0.2307334 8.175308e-01
> > 4 bb_1$pregsea4  -1.75059740 0.3978579  -4.4000571 1.103208e-05
> >
> > This's my code:
> > model_multi_1T <- dlply(bb_1,c("HERDCODE"), function(bb_1)
> > lm(bb_1$conc_dry ~ bb_1$pregsea, data=bb_1))
> >
> > I want to store the coefficients for each group (111000200 is the number
> > of the 1st group)
> > [cid:6eb85a80-0160-49cb-90b6-12216571b0cc]
> >
> >
> >
> > ________________________________
> > From: UF R Users List <[log in to unmask]> on behalf of Shawn
> Taylor
> > <[log in to unmask]>
> > Sent: Wednesday, February 21, 2018 8:03:49 AM
> > To: [log in to unmask]
> > Subject: Re: How to store result of coefficients from the model
> >
> > tidy won't work on a list of model results, it needs to be run on the
> model
> > output directly like:
> >
> > broom::tidy(lm(y~x, data=d))
> >
> > Can you post or give a link to your analysis script and we can suggest
> > where to implement it?
> >
> >
> >
> > On Wed, Feb 21, 2018 at 7:57 AM, Pattamanont,Pornpamol <
> > [log in to unmask]
> > > wrote:
> >
> > > It doesn't work. I've got this error.
> > >
> > >
> > > [cid:53cc4333-2199-4ce2-9d8a-fad91ee9f438]
> > >
> > >
> > > I've tried broom earlier; it doesn't work for any function in it. I
> think
> > > the problem is because I run the model by group. I supposed to be able
> to
> > > use "ldply" as it works for the previous model of mind that also run by
> > > group. Only one difference that I can use ldply for this current model
> is
> > > because the predictor is a category.
> > >
> > >
> > > Any other suggestions?
> > >
> > >
> > >
> > >
> > >
> > >
> > >
> > > ________________________________
> > > From: UF R Users List <[log in to unmask]> on behalf of
> > > Chaudhary,Vratika <[log in to unmask]>
> > > Sent: Wednesday, February 21, 2018 6:35:29 AM
> > > To: [log in to unmask]
> > > Subject: Re: How to store result of coefficients from the model
> > >
> > > Pornpamol
> > >
> > > I am unsure if I understood your question right, but if storing model
> > > results is your problem- you could use broom::tidy, which cleans and
> > stores
> > > gym results in data frame. https://urldefense.proofpoint.com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=ERABobiQkGrOZ7U5a2Ov3ETwpy2CAEkdpLYOVb_cRGI&s=UkOyYK1x5OSVspT_Toigj9W_nImb11Ym58je8ET0i6U&e=.
> com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=
> pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=
> QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=
> KOLF1vxBdafTXhLO69TErJDpzpSd4q69Z4mESZUverg&s=
> g3FrOYAsMPrmKtNPtyf91yfYH2zsmKMOmitLM8wZE1w&e=.
> > com/v2/url?u=https-3A__cran.r-2Dproject.org_&d=DwIBaQ&c=pZJP
> > UDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggU
> > L7kQsdfEWUTu6qCGEw8Xuo91A&m=tRJQgGL9yUTACFh99WH_vy1dT68htuYJ
> > h-ugiZvUp_I&s=KBdGtazjdjQYkpp3v_beTgofUb0H-N4qJVuyre-v9b0&e=
> > > web/packages/broom/vignettes/broom.html
> > >
> > > If you have small numbers of models you could do so for each of them
> and
> > > then bind rows of the results in a table, else you could create a
> > function
> > > probably to do that
> > >
> > > Hope that helps
> > > Vratika
> > >
> > > > On 21-Feb-2018, at 5:59 AM, Pattamanont,Pornpamol <
> > [log in to unmask]>
> > > wrote:
> > > >
> > > > Hello everyone,
> > > >
> > > > I'm struggling with storing results of coefficients from the model. I
> > > was trying to use function "ldply" as ldply(model_multi_1T,coef) but
> > there
> > > was an error showing that "result do not have enough length". I have
> > around
> > > 1,200 lists in "model_multi_1T" . The attached picture is the
> > coefficients
> > > that I want to store in a table.
> > > >
> > > >
> > > > any suggestions are appreciated,
> > > >
> > > > Pornpamol
> > > >
> > > >
> > > >  [cid:5762deff-8145-4095-aba7-b6c43ec935f0]
> > > >
> > > >
> > > >
> > > > This list strives to be beginner friendly.  However, we still ask
> that
> > > you
> > > > PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=ERABobiQkGrOZ7U5a2Ov3ETwpy2CAEkdpLYOVb_cRGI&s=UkOyYK1x5OSVspT_Toigj9W_nImb11Ym58je8ET0i6U&e=.
> com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=
> pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=
> QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=
> KOLF1vxBdafTXhLO69TErJDpzpSd4q69Z4mESZUverg&s=
> g3FrOYAsMPrmKtNPtyf91yfYH2zsmKMOmitLM8wZE1w&e=.
> > com/v2/url?u=http-3A__www.R-2Dproject.org_&d=DwIBaQ&c=pZJPUD
> > Q3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7
> > kQsdfEWUTu6qCGEw8Xuo91A&m=tRJQgGL9yUTACFh99WH_vy1dT68htuYJh-
> > ugiZvUp_I&s=JxyBMGnGSSbvzgnGxb_rhGYCRWZG_3euARM8qQG7AjI&e=
> > > posting-guide.html
> > > > and provide commented, minimal, self-contained, reproducible code.
> > > > <ask R.jpg>
> > >
> > > This list strives to be beginner friendly.  However, we still ask that
> > you
> > > PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=ERABobiQkGrOZ7U5a2Ov3ETwpy2CAEkdpLYOVb_cRGI&s=UkOyYK1x5OSVspT_Toigj9W_nImb11Ym58je8ET0i6U&e=.
> com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=
> pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=
> QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=
> KOLF1vxBdafTXhLO69TErJDpzpSd4q69Z4mESZUverg&s=
> g3FrOYAsMPrmKtNPtyf91yfYH2zsmKMOmitLM8wZE1w&e=.
> > com/v2/url?u=http-3A__www.R-2Dproject.org_&d=DwIBaQ&c=pZJPUD
> > Q3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7
> > kQsdfEWUTu6qCGEw8Xuo91A&m=tRJQgGL9yUTACFh99WH_vy1dT68htuYJh-
> > ugiZvUp_I&s=JxyBMGnGSSbvzgnGxb_rhGYCRWZG_3euARM8qQG7AjI&e=
> > > posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> > > This list strives to be beginner friendly.  However, we still ask that
> > you
> > > PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=ERABobiQkGrOZ7U5a2Ov3ETwpy2CAEkdpLYOVb_cRGI&s=UkOyYK1x5OSVspT_Toigj9W_nImb11Ym58je8ET0i6U&e=.
> com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=
> pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=
> QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=
> KOLF1vxBdafTXhLO69TErJDpzpSd4q69Z4mESZUverg&s=
> g3FrOYAsMPrmKtNPtyf91yfYH2zsmKMOmitLM8wZE1w&e=.
> > com/v2/url?u=http-3A__www.R-2Dproject.org_&d=DwIBaQ&c=pZJPUD
> > Q3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7
> > kQsdfEWUTu6qCGEw8Xuo91A&m=tRJQgGL9yUTACFh99WH_vy1dT68htuYJh-
> > ugiZvUp_I&s=JxyBMGnGSSbvzgnGxb_rhGYCRWZG_3euARM8qQG7AjI&e=
> > > posting-guide.html
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
> > This list strives to be beginner friendly.  However, we still ask that
> you
> > PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=ERABobiQkGrOZ7U5a2Ov3ETwpy2CAEkdpLYOVb_cRGI&s=UkOyYK1x5OSVspT_Toigj9W_nImb11Ym58je8ET0i6U&e=.
> com/v2/url?u=http-3A__www.R-2Dproject.org_posti&d=DwIBaQ&c=
> pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=
> QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=
> KOLF1vxBdafTXhLO69TErJDpzpSd4q69Z4mESZUverg&s=
> 4ovnJ4K2j7Ku7EjEHRBaWpi5xixajvuD62JumZA6ANc&e=
> > ng-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> > This list strives to be beginner friendly.  However, we still ask that
> you
> > PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=ERABobiQkGrOZ7U5a2Ov3ETwpy2CAEkdpLYOVb_cRGI&s=UkOyYK1x5OSVspT_Toigj9W_nImb11Ym58je8ET0i6U&e=.
> com/v2/url?u=http-3A__www.R-2Dproject.org_posti&d=DwIBaQ&c=
> pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=
> QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=
> KOLF1vxBdafTXhLO69TErJDpzpSd4q69Z4mESZUverg&s=
> 4ovnJ4K2j7Ku7EjEHRBaWpi5xixajvuD62JumZA6ANc&e=
> > ng-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> > This list strives to be beginner friendly.  However, we still ask that
> you
> > PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=https-3A__urldefense.proofpoint&d=DwIBaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=ERABobiQkGrOZ7U5a2Ov3ETwpy2CAEkdpLYOVb_cRGI&s=UkOyYK1x5OSVspT_Toigj9W_nImb11Ym58je8ET0i6U&e=.
> com/v2/url?u=http-3A__www.R-2Dproject.org_posti&d=DwIBaQ&c=
> pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=
> QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=
> KOLF1vxBdafTXhLO69TErJDpzpSd4q69Z4mESZUverg&s=
> 4ovnJ4K2j7Ku7EjEHRBaWpi5xixajvuD62JumZA6ANc&e=
> > ng-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
> This list strives to be beginner friendly.  However, we still ask that you
> PLEASE do read the posting guide https://urldefense.proofpoint.com/v2/url?u=http-3A__www.R-2Dproject.org_&d=DwIBaQ&c=pZJPUDQ3SB9JplYbifm4nt2lEVG5pWx2KikqINpWlZM&r=QWxQId63qB2iSP1ggUL7kQsdfEWUTu6qCGEw8Xuo91A&m=ERABobiQkGrOZ7U5a2Ov3ETwpy2CAEkdpLYOVb_cRGI&s=lS4h4oldWw2GaRXeuq6c8t5G_bECcVyNHOvDXBXe4Mg&e=
> posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

This list strives to be beginner friendly.  However, we still ask that you
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.