Reputation: 1211
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.
Upvotes: 8
Views: 2495
Reputation: 43334
You need to construct the formulas from the column names, and then map lm
as the last step. You can do this with two map
s:
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.
Upvotes: 10