使用dplyr存储回归输出

时间:2018-06-28 12:23:48

标签: r dplyr regression

df.h <- data.frame( 
                hour = factor(rep(1:24, each = 21)),
                price = runif(504, min = -10, max = 125),
                wind = runif(504, min = 0, max = 2500),
                temp = runif(504, min = - 10, max = 25))  

如果我想对风和价格对温度进行线性回归,可以这样做:

df.h %>% group_by(hour) %>% do(mod1 = lm(price ~ wind , data = .), 
                                   mod2 = lm(price ~ temp, data = .)) 

但是,我要提取的是mod1和mod2的残差并将残差存储为另外两个 列

df.h %>% group_by(hour) %>% 
         do(mod1 = lm(price ~ wind , data = .), 
            mod2 = lm(price ~ temp, data = .)) %>%
         mutate(mod1.resid = resid(mod1),
                mod2.resid = resid(mod2))

但是,这不起作用。请问你能帮帮我吗。

1 个答案:

答案 0 :(得分:0)

mod <- df.h %>% group_by(hour) %>% do(mod1 = resid(lm(price ~ wind , data = .)), 
                           mod2 = resid(lm(price ~ temp, data = .))) 

df.h <- df.h %>% 
     mutate(res1 = unlist(mod$mod1),
     res2 = unlist(mod$mod2))

也许我想找出一个更紧凑的解决方案。