我希望从线性模型结果lm
中创建带有重新设计的列的干净数据框,我的最终目标是编写出色的数据框以绘制图表并审核模型残差。首先,示例数据:
df1 <- cbind.data.frame(dt = seq.Date(as.Date('2019-01-01'),
as.Date('2019-01-10'),
by = 'day' ),
depVar = rnorm(10,2,1),
indepVar1 = rnorm(10,4,3),
indepVar2 = rnorm(10,7,2)
)
现在运行模型:
modRes <- lm(depVar~ indepVar1, data=df1)
avf1 <- broom::augment(modRes)
library(dplyr)
avf1 <- avf1 %>%
# drop what we don't need
select(-c(.se.fit, .hat, .sigma, .cooksd, .std.resid)) %>%
cbind(df1)
以上方法运行良好,但是我想用%>%
添加另一个管道select(dt, everything())
,以便重新排序列。以下返回错误:
avf1 <- avf1 %>%
select(-c(.se.fit, .hat, .sigma, .cooksd, .std.resid)) %>% # drop what we don't need
cbind(df1) %>%
select(dt, everything())
Error: Can't bind data because some arguments have the same name
Call `rlang::last_error()` to see a backtrace
为什么会失败?
答案 0 :(得分:-1)
avf1 <- modRes %>%
augment() %>%
select(-c(.se.fit, .hat, .sigma, .cooksd, .std.resid)) %>%
bind_cols(df1) %>%
select(dt, everything())