按组对 Dataframe 应用R glm预测功能

iq3niunx  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(102)

目前我正在尝试对一个 Dataframe 应用GLM预测。这个 Dataframe 相当大,因此我想应用块预测。
我已经找到了一个解决方案,但是很不方便。我先创建一个空的 Dataframe ,然后使用rbind。有没有更有效的方法来做到这一点?

df=data[c(),]
for (x in split(data, factor(sort(rank(row.names(data))%%10)))) {
     x["prediction"]=predict(model, x, type="response")
     df=rbind(df,x)
}
nx7onnlm

nx7onnlm1#

正如评论中提到的,一个您希望输出 Dataframe 看起来像什么的示例将非常有帮助。
但是我认为你可以通过先创建一个分组变量然后使用'group_by'来实现你想要的,类似于:

df <- data %>% 
  mutate(group = rep(1:10, times = nrow(.)/10)) %>% # make an arbitrary grouping factor for this example
  group_by(group) %>%  # group by whatever your grouping factor is
  summarise(predictions = predict(model, x, type = 'response')) # summarise could be replaced by mutate

相关问题