Using purrr::map to iterate linear model over columns in data frame

一世执手 提交于 2019-12-04 06:45:10

You need to construct the formulas from the column names, and then map lm as the last step. You can do this with two maps:

library(purrr)

df_of_vecs %>% 
    names() %>% 
    paste('vec_1 ~', .) %>% 
    map(as.formula) %>% 
    map(lm, data = df_of_vecs)

or one:

df_of_vecs %>% 
    names() %>% 
    paste('vec_1 ~', .) %>% 
    map(~lm(as.formula(.x), data = df_of_vecs))

Both return the same list of ten models.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!