Combining dplyr :: do () with dplyr :: mutate?

I would like to do the following: for each subgroup of the data set, I would like to perform a regression, and the remnants of this regression should be saved as a new variable in the original frame. For instance,

 group_by(mtcars, gear) %>% mutate(res = residuals(lm(mpg~carb, .)))

indicates that I think it should work, but not (does anyone want to explain why it does not work?). One way to get leftovers is to do the following:

 group_by(mtcars, gear) %>% do(res = residuals(lm(mpg~carb, .)))

which gives me a data block in which dbl-objects are stored, i.e. containing residues for each group. However, it seems that they do not contain the original names of the growths, which will help me combine them back with the original data.

So my question is: how can I achieve what I want to do in dplyr style?

Obviously, this can be achieved in other ways. To give you an example, the following works very well:

 dat <- mtcars
 dat$res <- NA
 for(i in unique(mtcars$gear)){
   dat[dat$gear==i, "res"]  <- residuals(lm(mpg ~ disp, data=dat[dat$gear==i,]))
 }

However, I understand that it was created for this purpose dplyr, so should there be a style dplyr?

Any hints / tips / comments appreciated.

Note: this question is very similar to lm (), called in mutate () , except that in this question only one parameter is saved for each group, which makes the mergeapproach easy. I have a whole vector without growth names, so I have to rely on the ordering of the vector to do this, and it seems unpleasant to me.

+1
source share
2 answers
library(lazyeval)
eq <- "y ~ x"
dat <- mtcars
dat %>% 
    group_by(gear) %>% 
    mutate(res=residuals(lm(interp(eq, y = mpg, x = disp))))

or without lazyeval

dat %>% 
    group_by(gear) %>% 
    mutate(res=residuals(lm(deparse(substitute(mpg~disp)))))
+4
source
#This gives you the residuals. You can then combine this with original data. 
mtcars %>%
     group_by(cyl) %>%
     do(model = lm(mpg ~ wt, data=.)) %>%
     do((function(reg_mod) {
        data.frame(reg_res = residuals(reg_mod$model))
     })(.))
0

Source: https://habr.com/ru/post/1665812/


All Articles