Dplyr, do (), extracting parameters from a model without losing a variable grouping

A slightly modified example from the R help for do ():

by_cyl <- group_by(mtcars, cyl) models <- by_cyl %>% do(mod = lm(mpg ~ disp, data = .)) coefficients<-models %>% do(data.frame(coef = coef(.$mod)[[1]])) 

In the data data coefficients, there is a first linear model coefficient for each group of cylinders. My question is how can I create a data framework that contains not only a column with coefficients, but also a column with a grouping variable.

===== Editing: I am expanding this example to try to make my problem more clear.

Suppose I want to extract model coefficients and some prediction. I can do it:

 by_cyl <- group_by(mtcars, cyl) getpars <- function(df){ fit <- lm(mpg ~ disp, data = df) data.frame(intercept=coef(fit)[1],slope=coef(fit)[2]) } getprediction <- function(df){ fit <- lm(mpg ~ disp, data = df) x <- df$disp y <- predict(fit, data.frame(disp= x), type = "response") data.frame(x,y) } pars <- by_cyl %>% do(getpars(.)) prediction <- by_cyl %>% do(getprediction(.)) 

The problem is that the code is redundant because I fit the model twice. My idea was to create a function that returns a list with all the information:

 getAll <- function(df){ results<-list() fit <- lm(mpg ~ disp, data = df) x <- df$disp y <- predict(fit, data.frame(disp= x), type = "response") results$pars <- data.frame(intercept=coef(fit)[1],slope=coef(fit)[2]) results$prediction <- data.frame(x,y) results } 

The problem is that I don’t know how to use do () with the getAll function to get, for example, only a data file with parameters (for example, paragraphs dataframe).

+7
r dplyr
source share
2 answers

Like this?

 coefficients <-models %>% do(data.frame(coef = coef(.$mod)[[1]], group = .[[1]])) 

getting

  coef group 1 40.87196 4 2 19.08199 6 3 22.03280 8 
+7
source share

Using the Hadley Wickham approach in this video :

 library(dplyr) library(purrr) library(broom) fitmodel <- function(d) lm(mpg ~ disp, data = d) by_cyl <- mtcars %>% group_by(cyl) %>% nest() %>% mutate(mod = map(data, fitmodel), pars = map(mod, tidy), pred = map(mod, augment)) pars <- by_cyl %>% unnest(pars) prediction <- by_cyl %>% unnest(pred) 
+2
source share

All Articles