0

I am working on a data frame where I am trying to regress two columns(female dummy & scores) while grouping them by another column (country), and extracting the coefficient on female dummy.

I have tried using dplyr, by first grouping my data frame by country, using group_by(), then applying a regression, using group_map(). First off, the coefficients that are shown in the result are all the same, for each group. Second I cannot seem to extract only the second coefficient, and when I try, the code says I cannot implement on a list

f1 %>% group_by(background) %>%
group_map(~ coef(lm(pv1math ~ female, data = f1))) %>%
group_map(~ coef[2])

I essentially want a series of the second coefficient.

I keep getting error for group_split.

error in UseMethod("group_split") : no applicable method for 'group_split' applied to an object of class "list"

ashwin agrawal
  • 1,603
  • 8
  • 16
king_sules
  • 39
  • 8
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 30 '19 at 21:18
  • 1
    If you only ever want the second coef, why not just do `~coef(lm(pv1math ~ female, data = f1))[2]` Also note that you aren't using anything passed to the `group_map` because you have hard coded `f1` there. If you want to perform the regression separately for each group, use `~coef(lm(pv1math ~ female, data = .))[2]` – MrFlick Oct 30 '19 at 21:20
  • Thanks MrFlick, I originally put ```~coef[2](lm(pv1math ~ female, data = f1))``` but I see I mixed up the position of [2]. As for hard coding, I had no idea that was the source of my error. Your code works like a charm. – king_sules Oct 30 '19 at 21:33

0 Answers0