Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用扫帚(增强)和dplyr配合时出错_R_Dplyr_Broom - Fatal编程技术网

使用扫帚(增强)和dplyr配合时出错

使用扫帚(增强)和dplyr配合时出错,r,dplyr,broom,R,Dplyr,Broom,我尝试在黄土拟合上使用“增强”,但收到以下错误: 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

我尝试在黄土拟合上使用“增强”,但收到以下错误:

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)
}
require(扫帚)
需要(dplyr)
#本例使用lm方法,它可以正常工作
回归%group_by(cyl)%>%do(拟合=lm(wt~mpg,))
回归%>%增大(拟合)
#此示例使用黄土方法,并生成错误
回归2%组单位(cyl)%>%do(拟合=黄土(wt~mpg,)
回归2%>%增大(拟合)
#以下代码使用geom_smooth适当绘制黄土拟合。
#我目前的解决办法是在geom#u smooth中作为aes对象进行全局定义`
cylc=唯一(mtcars$cyl)%>%sort()
对于(1中的i:长度(圆柱体)){
印刷品(一)
打印(气缸[i])

p这似乎是一个与
do()
操作符相关的问题:当我们在一个黄土模型对象上检查
model.frame()
时,我们将返回所有32行,而不是对应于该模型的子集

解决方法是保留数据而不仅仅是模型,并将其作为第二个参数传递给
augment()


谢谢你戴维的详细回应。我读了那一章,这个解决方案既清晰又明智。你可能会考虑在这里更新这个小插图:因为这是我使用do-()的地方。@Steve你完全正确!有一些新的计划和资源用于维护broom,这意味着这些更新可能会在今年夏天发生。再次感谢你的帮助!作为一个可复制的解决方案,添加library(tidyverse),library(broom)。
regressions2 <- mtcars %>%
  group_by(cyl) %>%
  do(fit = loess(wt ~ mpg, .),
     data = (.)) %>%
   augment(fit, data)
library(tidyr)
library(purrr)

mtcars %>%
  nest(-cyl) %>%
  mutate(fit = map(data, ~ loess(wt ~ mpg, .))) %>%
  unnest(map2(fit, data, augment))