I'm trying to run many individual linear regressions with one Y variable and many x variables. My data has 300+ x variables. I've been trying to do this with purrr and broom but cannot figure out how to get the output how I'd like.
Example:
iris <- iris %>%
select_if(is.numeric)
iris %>%
map(~lm(Sepal.Length ~ .x, data = iris)) %>%
map(summary) %>%
map_df(tidy)
This produces the following output:
# A tibble: 6 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 0 3.79e-17 0. 1.00e+ 0
2 .x 1 6.43e-18 1.56e17 0.
3 (Intercept) 6.53 4.79e- 1 1.36e 1 6.47e- 28
4 .x -0.223 1.55e- 1 -1.44e 0 1.52e- 1
5 (Intercept) 4.31 7.84e- 2 5.49e 1 2.43e-100
6 .x 0.409 1.89e- 2 2.16e 1 1.04e- 47
Which is close to what I'm looking for, but not quite! I want the variable names to in the 'term' column here and I don't want the intercept pasted for each model. The results I'm looking for a more like:
# A tibble: 6 x 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 Sepal.Width 0 3.79e-17 0. 1.00e+ 0
2 Petal.Width 1 6.43e-18 1.56e17 0.
3 Petal.Length 6.53 4.79e- 1 1.36e 1 6.47e- 28
Any help getting to that point would be greatly appreciated!! And of course extra appreciation for explanations of the process (i'm learning)
Cheers