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).
r dplyr
danilinares
source share