How to use for loop for linear regression using long data frame in R?

Here is an example of performing multiple linear regressions on a long data frame using purrr instead of loops.

library(dplyr)
library(purrr)
library(broom)

fit_model <- function(df) lm(Sepal.Length ~ Sepal.Width, data = df)
get_slope <- function(mod) tidy(mod)$estimate[2]
get_p_value <- function(mod) tidy(mod)$p.value[2]

iris %>% 
    group_nest(Species) %>% 
    mutate(model = map(data, fit_model),
           slope = map_dbl(model, get_slope),
           p_value = map_dbl(model, get_p_value))
#> # A tibble: 3 × 5
#>   Species                  data model  slope  p_value
#>   <fct>      <list<tibble[,4]>> <list> <dbl>    <dbl>
#> 1 setosa               [50 × 4] <lm>   0.690 6.71e-10
#> 2 versicolor           [50 × 4] <lm>   0.865 8.77e- 5
#> 3 virginica            [50 × 4] <lm>   0.902 8.43e- 4

Created on 2021-07-28 by the reprex package (v2.0.0)

If you need more specific help, please provide a proper REPRoducible EXample (reprex) illustrating your issue.

1 Like