Grouped regression is running well model1 with "do". But recently, it is told that do is superseded and suggested to use "across" but no example is given in the help file. Model2 is given in "do" help, and it is running well without "map" or "across". I don't understand how the regression is looping over those groups without map. When I tried using map in model3, I am getting errors. Model4 is given in Hadley's book, R for data science using split and working well. How to tell map function to consider the list "data". Any suggestions?
library(purrr) #> Warning: package 'purrr' was built under R version 3.6.3 library(tidyverse) #> Warning: package 'tidyverse' was built under R version 3.6.3 #> Warning: package 'ggplot2' was built under R version 3.6.3 #> Warning: package 'tidyr' was built under R version 3.6.3 #> Warning: package 'dplyr' was built under R version 3.6.3 #> Warning: package 'stringr' was built under R version 3.6.3 #> Warning: package 'forcats' was built under R version 3.6.3 model1 = mtcars %>% group_by(cyl) %>% do(mod = lm(mpg ~ disp, data = .)) model1 #> # A tibble: 3 x 2 #> # Rowwise: #> cyl mod #> <dbl> <list> #> 1 4 <lm> #> 2 6 <lm> #> 3 8 <lm> ## from "do" help file model2 = mtcars %>% nest_by(cyl) %>% mutate(mod = list(lm(mpg ~ disp, data = data))) model2 #> # A tibble: 3 x 3 #> # Rowwise: cyl #> cyl data mod #> <dbl> <list<tbl_df[,10]>> <list> #> 1 4 [11 x 10] <lm> #> 2 6 [7 x 10] <lm> #> 3 8 [14 x 10] <lm> ## using map model3 = mtcars %>% nest_by(cyl) %>% mutate(fit = map(data, ~lm(mpg ~ disp, data = .))) #> Error: Problem with `mutate()` input `fit`. #> x numeric 'envir' arg not of length one #> i Input `fit` is `map(data, ~lm(mpg ~ disp, data = .))`. #> i The error occured in row 1. ##model4 model4 = mtcars %>% split(.$cyl) %>% map(~lm(mpg ~ disp, data = .)) model4 #> $`4` #> #> Call: #> lm(formula = mpg ~ disp, data = .) #> #> Coefficients: #> (Intercept) disp #> 40.8720 -0.1351 #> #> #> $`6` #> #> Call: #> lm(formula = mpg ~ disp, data = .) #> #> Coefficients: #> (Intercept) disp #> 19.081987 0.003605 #> #> #> $`8` #> #> Call: #> lm(formula = mpg ~ disp, data = .) #> #> Coefficients: #> (Intercept) disp #> 22.03280 -0.01963 Created on 2020-08-02 by the reprex package (v0.3.0)