问题描述
我试图在黄土配合上使用增量,但收到以下错误:
I am trying to use augment on a loess fit, but I receive the following error:
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 32, 11
在错误消息中,11个恰好等于一个段中的观察数,而32是观察总数。代码如下。
In the error message, 11 happens to equal the number of observations in one segment and 32 is the total number of observations. The code is below.
require(broom)
require(dplyr)
# This example uses the lm method and it works
regressions <- mtcars %>% group_by(cyl) %>% do(fit = lm(wt ~ mpg, .))
regressions %>% augment(fit)
# This example uses the loess method and it generates the error
regressions2 <- mtcars %>% group_by(cyl) %>% do(fit = loess(wt ~ mpg, .))
regressions2 %>% augment(fit)
# The below code appropriately plots the loess fit using geom_smooth.
# My current # workaround is to do a global definition as an aes object in geom_smooth`
cylc = unique(mtcars$cyl) %>% sort()
for (i in 1:length(cyl)){
print(i)
print(cyl[i])
p<- ggplot(data=filter(mtcars,cyl==cylc[i]),aes(x=mpg,y=wt)) + geom_point() + geom_smooth(method="loess") + ggtitle(str_c("cyl = ",cyl[i]))
print(p)
}
推荐答案
这似乎是与以下问题有关的问题 do()
运算符:当我们检查LOESS模型对象之一的 model.frame()
时,我们返回所有32行,而不是返回与该模型对应的子集。
This appears to be a problem related to the do()
operator: when we check the model.frame()
on one of the LOESS model objects, we get back all 32 rows rather than the subset corresponding to that model.
一种解决方法是保留数据,而不仅仅是模型,并将其作为第二个 augment()
的参数:
A workaround is to hold on to the data and not just the model, and pass that as the second argument to augment()
:
regressions2 <- mtcars %>%
group_by(cyl) %>%
do(fit = loess(wt ~ mpg, .),
data = (.)) %>%
augment(fit, data)
通常建议与 augment()
一起使用,因为 model.frame()
不能获取所有原始列。
This is usually recommended with augment()
anyway, since model.frame()
doesn't get all the original columns.
偶然,我是扫帚的维护者,并且我通常不再建议使用 do()
方法(因为dplyr大多已放弃使用它)。
Incidentally, I'm the maintainer of broom and I generally no longer recommend the do()
approach (since dplyr has mostly been moving away from it).
相反,我建议使用tidyr的 nest()
和purrr的 map()
,如所述。这样可以更轻松地保留数据并将其合并到 augment()
中。
Instead, I suggest using tidyr's nest()
and purrr's map()
, as described in this chapter of R4DS. This makes it a little bit easier to hold on to the data and incorporate into augment()
.
library(tidyr)
library(purrr)
mtcars %>%
nest(-cyl) %>%
mutate(fit = map(data, ~ loess(wt ~ mpg, .))) %>%
unnest(map2(fit, data, augment))
这篇关于使用黄土色的扫帚(增强)和dplyr时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!