lm() called within mutate()

后端 未结 2 439
遥遥无期
遥遥无期 2021-01-19 02:49

I wonder if it is possible to use lm() within mutate() of dplyr package. Currently I have a dataframe of \"date\", \"company\", \"return\" and \"market.ret\" reproducible as

2条回答
  •  走了就别回头了
    2021-01-19 03:45

    This seems to work for me:

    group_by(x, company) %>%
        do(data.frame(beta = coef(lm(return ~ market.ret,data = .))[2])) %>%
        left_join(x,.)
    

提交回复
热议问题