I intend to perform separate regression by each level of a factor in a data.frame. I used to be able to do it by using plyr::ddply . However, when I tried to use the pipe flow of analysis, I encountered the following errors. Please advise how to overcome it, or I will have to revert to plyr::ddply , etc. Thanks.
d = data.frame(
Gender = c("M","F"),
Age = rnorm(20, mean = 40, sd = 3),
Weight = rnorm(20, mean=70, sd=5)
)
fit <- d %>% group_by(Gender) %>%
summarise(
Intercept = coef(lm(Weight ~ Age))[1],
Slope = coef(lm(Weight ~ Age))[2]
)
Error: invalid term in model formula