我试图在一个loess拟合上使用增广,但是我收到以下错误:
Error in data.frame(..., check.names = FALSE) :
arguments imply differing number of rows: 32, 11
在错误信息中,11恰好等于一个段落中观察值的数量,而32是总观察值的数量。以下是代码。
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)
}