rpurrrtidymodelsbroom

Tidy multiple univariate regressions


db = tibble(a = rnorm(100), b = rnorm(100), c = rnorm(100))

If I want a tidy multivariate linear regression, I just can go

lm(data = db, 0 + a ~ b + c) %>% tidy()

But if I want multiple univariate regressions I would go

lm(data = db, a ~ 0 + b) %>% tidy() %>%
  add_row(lm(data = db, a ~ 0 + c) %>% tidy())

Now, given many regressor columns, I would like to avoid to code every single regressor as a new add_row, how should I make the code more synthetic?

This has a partial solution here:

Tidy output from many single-variable models using purrr, broom

I think the code can be even more lean than in the example?


Solution

  • My answer

    db %>%
      select(-a) %>%
      names() %>%
      paste('a~0+',.)%>%
      map_df(~tidy(lm(as.formula(.x), 
                   data= db, 
                   )))