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

北战南征 提交于 2019-12-09 17:14:33

问题


I am trying to do an exercise to become more familiar with how to use the map function in purrr. I am creating some random data (10 columns of 10 datapoints) and then I wanted to use map to perform a series of regressions (i.e. lm(y ~ x, data = )) over the resulting columns in the data frame.

If I just repeatedly use the first column as 'y', I want to perform 10 regressions with each column from 1 to 10 as 'x'. Obviously the results are unimportant - it's just the method. I want to end up with a list of 10 linear model objects.

list_of_vecs <- list()
for (i in 1:10){ 
 list_of_vecs[[paste('vec_', i, sep = '')]] <- rnorm(10,0,1)
}
df_of_vecs <- as.data.frame(list_of_vecs)

Here, I get stuck:

map(df_of_vecs, ~ lm(df_of_vecs[[1]] ~ . ?)

Any tips would be appreciated.

Thanks.


回答1:


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.



来源:https://stackoverflow.com/questions/41112046/using-purrrmap-to-iterate-linear-model-over-columns-in-data-frame

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