我有一些与此相似的代码:
library(tidyverse)
library(broom)
df <- tribble(~group, ~set, ~x, ~y,
0, "training", 1, 2,
0, "training", 2, 4,
0, "training", 4, 9,
0, "training", 5, 9,
0, "training", 15, 29,
0, "training", 20, 42,
0, "training", 21, 42,
0, "training", 25, 49,
0, "test", 10, NA,
)
df %>%
filter(set == "training") %>%
group_by(group) %>%
do(pm = lm(y ~ x, data = .)) %>%
left_join(df, ., by = "group")
所以我得到这样的东西:
group set x y model
<dbl> <chr> <dbl> <dbl> <list>
1 0. training 1. 2. <S3: lm>
2 0. training 2. 4. <S3: lm>
3 0. training 4. 9. <S3: lm>
4 0. training 5. 9. <S3: lm>
5 0. training 15. 29. <S3: lm>
6 0. training 20. 42. <S3: lm>
7 0. training 21. 42. <S3: lm>
8 0. training 25. 49. <S3: lm>
9 0. test 10. NA <S3: lm>
在第 set 等于“ test”的第9行中,如何使用线性模型从 x 预测 y ? 预先感谢您的帮助
答案 0 :(得分:0)
在按“分组”分组后,我们可以基于同一行上的replace
模型,根据NA
输出的predict
lm
{y}中的library(dplyr)
df %>%
filter(set == "training") %>%
group_by(group) %>%
do(pm = lm(y ~ x, data = .)) %>%
left_join(df, ., by = "group") %>%
group_by(group) %>%
mutate(y = replace(y, is.na(y), predict(pm[[1]],
newdata = data.frame(x = x[set == "test"]))))
# A tibble: 9 x 5
# Groups: group [1]
# group set x y pm
# <dbl> <chr> <dbl> <dbl> <list>
#1 0 training 1 2 <lm>
#2 0 training 2 4 <lm>
#3 0 training 4 9 <lm>
#4 0 training 5 9 <lm>
#5 0 training 15 29 <lm>
#6 0 training 20 42 <lm>
#7 0 training 21 42 <lm>
#8 0 training 25 49 <lm>
#9 0 test 10 20.0 <lm>
元素>
setClickable(true)