繁体   English   中英

按组应用R glm预测function对dataframe

[英]Apply R glm predict function on dataframe by group

目前,我正尝试在 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)
}

正如评论中提到的,您希望 output 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
   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM