How to do lm with dplyr

Hi, I'm new in this community
and I have one question
Using albalon.csv in Kaggle

abalone %>% 
    group_by(Sex) %>% 
    lm(Diameter~Whole.weight, data=abalone)

you know what I mean?
I would like to derive the effect of diameter on total_weight by gender through linear regression analysis.
Male or Female

Try one of these:

library(dplyr)

iris %>%
  group_by(Sex) %>%
  group_map(~ broom::tidy(lm(Diameter ~ Whole.weight, data = .x)))

iris %>%
  group_by(Sex) %>%
  group_modify(~ broom::tidy(lm(Diameter ~ Whole.weight, data = .x)))
3 Likes

Here is an example with mtcars (pinched from http://varianceexplained.org/r/broom-intro/)

library(dplyr)
library(broom)
mtcars %>% group_by(am) %>% do(tidy(lm(mpg ~ wt, .)))

so in your case

library(dplyr)
library(broom)
abalone %>% 
    group_by(Sex)  %>% 
    do(tidy(lm(Diameter~Whole.weight, .)))

should work.

2 Likes

Thanks to your help
so I can deal with problem

thank you
I hadn't realize library(broom)

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.