Home > Blockchain >  Using purrr:map2 to perform regression where the predictor and the criterion are stored in different
Using purrr:map2 to perform regression where the predictor and the criterion are stored in different

Time:09-30

for the purposes of this question, let's create the following setup:

mtcars %>% 
  group_split(carb) %>% 
  map(select, mpg) -> criterion

mtcars %>% 
  group_split(carb) %>% 
  map(select, qsec) -> predictor

This code will create two lists of length 6. What I want to do is to perform 6 linear regressions within each of these 6 groups. I read about the map2 function and I thought that the code should look like this:

map2(criterion, predictor, lm(criterion ~ predictor))

But that doesn't seem to work. So in which way could this be done?

CodePudding user response:

simplify2array (you need a list of vectors, not a list of data frames) and use a lambda-function with ~:

map2(simplify2array(criterion), simplify2array(predictor), ~ lm(.x ~ .y))

CodePudding user response:

While the direct answer to your question is already given, note that we can also use dplyr::nest_by() and then proceed automatically rowwise.

Now your models are stored in the mod column and we can use broom::tidy etc. to work with the models.

library(dplyr)
library(tidyr)

mtcars %>% 
  nest_by(carb) %>% 
  mutate(mod = list(lm(mpg ~ qsec, data = data)),
         res = list(broom::tidy(mod))) %>% 
  unnest(res) %>% 
  filter(term != "(Intercept)")

#> # A tibble: 6 x 8
#> # Groups:   carb [6]
#>    carb                data mod    term  estimate std.error statistic p.value
#>   <dbl> <list<tibble[,10]>> <list> <chr>    <dbl>     <dbl>     <dbl>   <dbl>
#> 1     1            [7 x 10] <lm>   qsec   -1.26       4.51    -0.279    0.791
#> 2     2           [10 x 10] <lm>   qsec    0.446      0.971    0.460    0.658
#> 3     3            [3 x 10] <lm>   qsec   -2.46       2.41    -1.02     0.493
#> 4     4           [10 x 10] <lm>   qsec    0.0597     0.991    0.0602   0.953
#> 5     6            [1 x 10] <lm>   qsec   NA         NA       NA       NA    
#> 6     8            [1 x 10] <lm>   qsec   NA         NA       NA       NA

Created on 2022-09-30 by the reprex package (v2.0.1)

  • Related