Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

purrr

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.

like image 795
LucaS Avatar asked Dec 13 '16 00:12

LucaS


1 Answers

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.

like image 174
alistaire Avatar answered Oct 31 '22 20:10

alistaire