用dplyr按组预测线性回归 [英] predicting linear regression by group with dplyr
本文介绍了用dplyr按组预测线性回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
建立此问题:添加预测列dplyr数据框的值
如果我在接受的答案中运行代码:
If I run the code in the accepted answer:
library(dplyr)
library(purrr)
library(tidyr)
# generate the inputs like in the question
example_table <- data.frame(x = c(1:5, 1:5),
y = c((1:5) + rnorm(5), 2*(5:1)),
groups = rep(LETTERS[1:2], each = 5))
models <- example_table %>%
group_by(groups) %>%
do(model = lm(y ~ x, data = .)) %>%
ungroup()
example_table <- left_join(tbl_df(example_table ), models, by = "groups")
# generate the extra column
example_table %>%
group_by(groups) %>%
do(modelr::add_predictions(., first(.$model))) %>% mutate(model = NULL)
我最终将预测存储在列表中:
I end up with the predictions stored in a list:
x y groups pred
1 1 1.798848 A 1.645775, 2.233358, 2.820940, 3.408523, 3.996105
2 2 2.936818 A 1.645775, 2.233358, 2.820940, 3.408523, 3.996105
3 3 1.513431 A 1.645775, 2.233358, 2.820940, 3.408523, 3.996105
4 4 3.300870 A 1.645775, 2.233358, 2.820940, 3.408523, 3.996105
5 5 4.554734 A 1.645775, 2.233358, 2.820940, 3.408523, 3.996105
6 1 10.000000 B 10, 8, 6, 4, 2
7 2 8.000000 B 10, 8, 6, 4, 2
8 3 6.000000 B 10, 8, 6, 4, 2
9 4 4.000000 B 10, 8, 6, 4, 2
10 5 2.000000 B 10, 8, 6, 4, 2
有没有办法让每个行(y〜x)有1个预测值?而不是整个组的列表吗?
Is there any way to have each row (y ~ x) have 1 predicted value? And not a list for the whole group?
推荐答案
与 xts $ c $发生冲突c>包。解决了它:
example_table %>%
group_by(groups) %>%
do(modelr::add_predictions(., dplyr::first(.$model))) %>% mutate(model = NULL)
这篇关于用dplyr按组预测线性回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文