Created
May 30, 2023 09:20
-
-
Save TimTeaFan/9563059bf5e5332a3f792f7db3e989ac to your computer and use it in GitHub Desktop.
Tidyverse many models using several dependent variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
``` r | |
library(tidyverse) | |
indep_vars <- c("vs", "am", "gear", "carb") | |
dep_vars <- c("mpg", "disp", "hp") | |
init_grid <- tibble(dep_var = dep_vars, | |
indep_vars = list(indep_vars)) | |
res_grid <- init_grid %>% | |
rowwise() %>% | |
mutate(mod = list(lm(reformulate(indep_vars, dep_var), | |
data = mtcars)), | |
res = list(broom::tidy(mod))) | |
res_grid | |
#> # A tibble: 3 × 4 | |
#> # Rowwise: | |
#> dep_var indep_vars mod res | |
#> <chr> <list> <list> <list> | |
#> 1 mpg <chr [4]> <lm> <tibble [5 × 5]> | |
#> 2 disp <chr [4]> <lm> <tibble [5 × 5]> | |
#> 3 hp <chr [4]> <lm> <tibble [5 × 5]> | |
``` | |
<sup>Created on 2023-05-30 with [reprex v2.0.2](https://reprex.tidyverse.org)</sup> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment