当我使用 broom:::glance
通过以下方式:
library(dplyr)
library(broom)
mtcars %>% do(model = lm(mpg ~ wt, .)) %>% glance(model)
我得到
Error in complete.cases(x) : invalid 'type' (list) of argument
但是,当我添加
group_by
时:mtcars %>% group_by(am) %>% do(model = lm(mpg ~ wt, .)) %>% glance(model)
确实给出了预期的结果:
Source: local data frame [2 x 12]
Groups: am
am r.squared adj.r.squared sigma statistic p.value df logLik AIC BIC deviance df.residual
1 0 0.589 0.565 2.53 24.4 1.25e-04 2 -43.5 93.1 95.9 108.7 17
2 1 0.826 0.810 2.69 52.3 1.69e-05 2 -30.2 66.4 68.1 79.3 11
我在这里遗漏了什么,还是 dplyr/broom 中的错误?
最佳答案
这是因为 do
,在未分组的表上执行时,会产生 tbl_df
而不是 rowwise_df
,意思是扫帚使用了不同的方法。我已经 fixed this在最新的开发版本中,您现在可以执行以下操作:
mtcars %>% do(model = lm(mpg ~ wt, .)) %>% glance(model)
#> r.squared adj.r.squared sigma statistic p.value df logLik
#> 1 0.7528328 0.7445939 3.045882 91.37533 1.293959e-10 2 -80.01471
#> AIC BIC deviance df.residual
#> 1 166.0294 170.4266 278.3219 30
我希望尽快在 CRAN (broom 0.4) 上安装它,或者您可以使用
devtools::install_github("dgrtwo/broom")
安装.同时,您还可以使用临时分组列来获得所需的行为:mtcars %>%
group_by(g = 1) %>%
do(model = lm(mpg ~ wt, .)) %>%
glance(model)
#> Source: local data frame [1 x 12]
#> Groups: g
#>
#> g r.squared adj.r.squared sigma statistic p.value df logLik
#> 1 1 0.7528328 0.7445939 3.045882 91.37533 1.293959e-10 2 -80.01471
#> Variables not shown: AIC (dbl), BIC (dbl), deviance (dbl), df.residual
#> (int)
关于r - 在具有单个 lm 对象的 dplyr 工作流中使用 `broom:::glance` 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32251265/