【问题标题】:using lm in list column to predict new values using purrr在列表列中使用 lm 使用 purrr 预测新值
【发布时间】:2017-11-26 08:25:45
【问题描述】:

我正在尝试将一列预测添加到具有包含 lm 模型的列表列的数据框中。我采用了this post的部分代码。

我在这里做了一个玩具例子:

library(dplyr)
library(purrr)
library(tidyr)
library(broom)

set.seed(1234)

exampleTable <- data.frame(
  ind = c(rep(1:5, 5)),
  dep = rnorm(25),
  groups = rep(LETTERS[1:5], each = 5)
) %>%
group_by(groups) %>%
nest(.key=the_data) %>%
mutate(model = the_data %>% map(~lm(dep ~ ind, data = .))) %>%
mutate(Pred = map2(model, the_data, predict))

exampleTable <- exampleTable %>%
  mutate(ind=row_number())

这给了我一个看起来像这样的小标题:

# A tibble: 5 × 6
  groups         the_data    model      Pred   ind 
  <fctr>           <list>   <list>    <list> <int> 
1      A <tibble [5 × 2]> <S3: lm> <dbl [5]>     1 
2      B <tibble [5 × 2]> <S3: lm> <dbl [5]>     2 
3      C <tibble [5 × 2]> <S3: lm> <dbl [5]>     3 
4      D <tibble [5 × 2]> <S3: lm> <dbl [5]>     4 
5      E <tibble [5 × 2]> <S3: lm> <dbl [5]>     5 

要使用特定组的 lm 模型获得预测值,我可以使用:

predict(exampleTable[1,]$model[[1]], slice(exampleTable, 1) %>% select(ind))

产生这个结果:

> predict(exampleTable[1,]$model[[1]], slice(exampleTable, 1) %>% select(ind))
         1 
-0.4822045

我希望对每个组都有一个新的预测。我尝试使用 purrr 来获得我想要的:

exampleTable %>%
  mutate(Prediction = map2(model, ind, predict))

但这给出了以下错误:

Error in mutate_impl(.data, dots) : object 'ind' not found

我能够通过以下怪物得到我想要的结果:

exampleTable$Prediction <- NA

for(loop in seq_along(exampleTable$groups)){
  lmod <- exampleTable[loop, ]$model[[1]]
  obs <- filter(exampleTable, row_number()==loop) %>%
    select(ind)
  exampleTable[loop, ] $Prediction <- as.numeric(predict(lmod, obs))
}

这给了我一个看起来像这样的小标题:

# A tibble: 5 × 6
  groups         the_data    model      Pred   ind Prediction
  <fctr>           <list>   <list>    <list> <int>      <dbl>
1      A <tibble [5 × 2]> <S3: lm> <dbl [5]>     1 -0.4822045
2      B <tibble [5 × 2]> <S3: lm> <dbl [5]>     2 -0.1357712
3      C <tibble [5 × 2]> <S3: lm> <dbl [5]>     3 -0.2455760
4      D <tibble [5 × 2]> <S3: lm> <dbl [5]>     4  0.4818425
5      E <tibble [5 × 2]> <S3: lm> <dbl [5]>     5 -0.3473236

一定有办法以“整洁”的方式做到这一点,但我就是无法破解它。

【问题讨论】:

  • mutate(Pred = map2_dbl(model, 1:5, ~predict(.x, newdata = data.frame(ind = .y)))) 怎么样?
  • 明白了!如果您将其更改为答案,我会将其标记为这样。非常感谢。

标签: r tidyverse purrr broom


【解决方案1】:

您可以利用newdata 参数到predict

我使用map2_dbl,所以它只返回单个值而不是列表。

mutate(Pred = map2_dbl(model, 1:5, ~predict(.x, newdata = data.frame(ind = .y))))

# A tibble: 5 x 4
  groups         the_data    model       Pred
  <fctr>           <list>   <list>      <dbl>
1      A <tibble [5 x 2]> <S3: lm> -0.4822045
2      B <tibble [5 x 2]> <S3: lm> -0.1357712
3      C <tibble [5 x 2]> <S3: lm> -0.2455760
4      D <tibble [5 x 2]> <S3: lm>  0.4818425
5      E <tibble [5 x 2]> <S3: lm> -0.3473236

如果您在预测之前将ind 添加到数据集,则可以使用该列而不是1:5

mutate(ind = 1:5) %>%
    mutate(Pred = map2_dbl(model, ind, ~predict(.x, newdata = data.frame(ind = .y) )))

# A tibble: 5 x 5
  groups         the_data    model   ind       Pred
  <fctr>           <list>   <list> <int>      <dbl>
1      A <tibble [5 x 2]> <S3: lm>     1 -0.4822045
2      B <tibble [5 x 2]> <S3: lm>     2 -0.1357712
3      C <tibble [5 x 2]> <S3: lm>     3 -0.2455760
4      D <tibble [5 x 2]> <S3: lm>     4  0.4818425
5      E <tibble [5 x 2]> <S3: lm>     5 -0.3473236

【讨论】:

    猜你喜欢
    • 2021-11-20
    • 2020-07-07
    • 2020-10-28
    • 2018-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多