I'm new to tidyverse and thus still struggling a bit to make it do stuff I knew how to do with base.
The issue: I want to loop through the columns of a data frame, input each of them separately into a lm call, and get the output as a tidy data frame. I don't care for the intercept, so all I want to save into the tidy output are the coefficients from the independent variable. I want the final output to look as follows: a data frame where the columns are the coefficients and the rows are each variable from the original data frame. I can do it with base using do.call("rbind", ...) but as I'm migrating to tidyverse, I wanted to see if there's a way to do it on tidyverse. purrr::map_dfr doesn't work on this case; a known issue.
Some reproducible code:
> library(tidyverse)
>
> set.seed(62442)
>
> iv <- rnorm(100)
> dvs <- as_tibble(replicate(5, iv + rnorm(100)), .name_repair = "universal")
New names:
* `` -> ...1
* `` -> ...2
* `` -> ...3
* `` -> ...4
* `` -> ...5
>
> # This doesn't work
> dvs %>% map_dfr(~ summary(lm(.x ~ iv))$coefficients[2, ])
# A tibble: 4 x 5
...1 ...2 ...3 ...4 ...5
<dbl> <dbl> <dbl> <dbl> <dbl>
1 8.78e- 1 1.09e+ 0 9.11e- 1 1.19e+ 0 8.80e- 1
2 1.05e- 1 1.17e- 1 9.86e- 2 9.33e- 2 1.16e- 1
3 8.34e+ 0 9.29e+ 0 9.24e+ 0 1.27e+ 1 7.60e+ 0
4 4.78e-13 4.16e-15 5.40e-15 1.97e-22 1.80e-11
>
> # It behaves exactly like:
> dvs %>% map_dfc(~ summary(lm(.x ~ iv))$coefficients[2, ])
# A tibble: 4 x 5
...1 ...2 ...3 ...4 ...5
<dbl> <dbl> <dbl> <dbl> <dbl>
1 8.78e- 1 1.09e+ 0 9.11e- 1 1.19e+ 0 8.80e- 1
2 1.05e- 1 1.17e- 1 9.86e- 2 9.33e- 2 1.16e- 1
3 8.34e+ 0 9.29e+ 0 9.24e+ 0 1.27e+ 1 7.60e+ 0
4 4.78e-13 4.16e-15 5.40e-15 1.97e-22 1.80e-11
>
> # All is left for me to do is:
> res <- dvs %>% map(~ summary(lm(.x ~ iv))$coefficients[2, ])
> do.call("rbind", res)
Estimate Std. Error t value Pr(>|t|)
...1 0.8776895 0.10525549 8.338658 0.0000000000004779501411861117
...2 1.0911362 0.11742588 9.292127 0.0000000000000041631074216992
...3 0.9113473 0.09863111 9.239958 0.0000000000000054021858298938
...4 1.1852848 0.09330950 12.702724 0.0000000000000000000001970469
...5 0.8799633 0.11579113 7.599575 0.0000000000179548788283525966