r语言 - 使用dplyr将预测值列添加到数据帧



我有一个模型列的数据框架,我试图添加一列预测值。一个最小的例子是:

exampleTable <- 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 <- exampleTable %>% group_by(groups) %>% do(model = lm(y ~ x, data = .))
exampleTable <- left_join(tbl_df(exampleTable), models)
estimates <- exampleTable %>% rowwise() %>% do(Est = predict(.$model, newdata = .["x"]))

如何在exampleTable中添加一列数字预测?我尝试使用mutate直接将列添加到表中,但没有成功。

exampleTable <- exampleTable %>% rowwise() %>% mutate(data.frame(Pred = predict(.$model, newdata = .["x"])))

错误:'predict'的方法不适用于' list '类的对象

现在我使用bind_colsestimates添加到exampleTable,但我正在寻找更好的解决方案。

estimates <- exampleTable %>% rowwise() %>% do(data.frame(Pred = predict(.$model, newdata = .["x"])))
exampleTable <- bind_cols(exampleTable, estimates)

如何在一个步骤中完成?

使用modelr,有一个使用tidyverse的优雅解决方案。

输入

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)))

解释

add_predictions使用给定模型向数据框架添加新列。不幸的是,它只接受一个模型作为参数。这是do。使用do,我们可以在每个组上单独运行add_prediction

.表示分组数据帧,.$model表示模型列,first()表示每组的第一个模型。

只有一个模型,add_predictions就能很好地工作。

# take one of the models
model <- example_table$model[[6]]
# generate the extra column
example_table %>%
  modelr::add_predictions(model)

现在,整理宇宙正在从modelr包转移到recipes,所以一旦这个包成熟,这可能是新的方式。

使用整理空间:

library(dplyr)
library(purrr)
library(tidyr)
library(broom)
exampleTable <- data.frame(
  x = c(1:5, 1:5),
  y = c((1:5) + rnorm(5), 2*(5:1)),
  groups = rep(LETTERS[1:2], each = 5)
)
exampleTable %>% 
  group_by(groups) %>%
  nest() %>% 
  mutate(model = data %>% map(~lm(y ~ x, data = .))) %>% 
  mutate(Pred = map2(model, data, predict)) %>% 
  unnest(Pred, data)
# A tibble: 10 × 4
   groups      Pred     x          y
   <fctr>     <dbl> <int>      <dbl>
1       A  1.284185     1  0.9305908
2       A  1.909262     2  1.9598293
3       A  2.534339     3  3.2812002
4       A  3.159415     4  2.9283637
5       A  3.784492     5  3.5717085
6       B 10.000000     1 10.0000000
7       B  8.000000     2  8.0000000
8       B  6.000000     3  6.0000000
9       B  4.000000     4  4.0000000
10      B  2.000000     5  2.0000000

嗯,这只是稍微好一点:

answer = 
  exampleTable %>%
  group_by(groups) %>%
  do(lm( y ~ x , data = .) %>% 
       predict %>% 
       data_frame(prediction = .)) %>%
  bind_cols(exampleTable)

我希望这将工作,但它没有。

answer = 
  exampleTable %>%
  group_by(groups) %>%
  mutate(prediction = 
           lm( y ~ x , data = .) %>% 
           predict)

最新更新