R - 对每组数据使用不同的模型进行预测和置信区间

R - Making predictions and confidence intervals with different models for each group of data

有人问了一个非常相似的问题 ,但我想为置信区间添加列。他们的工作示例:

x <- mtcars %>% 
     group_by(gear) %>% 
     do(model = lm(mpg ~ hp + wt, data = .))

x
Source: local data frame [3 x 2]
Groups: <by row>

# A tibble: 3 x 2
gear model   
* <dbl> <list>  
1     3 <S3: lm>
2     4 <S3: lm>
3     5 <S3: lm>

mtcars %>% 
group_by(gear) %>% 
nest %>% 
inner_join(x) %>% 
mutate(preds = map2(model, data, predict)) %>% 
unnest(data, preds)

这行得通,并为 mtcars 生成了一个附加列,其中包含使用每个分组的单独模型生成的预测值。现在我想做的是包括来自 predict()

的置信区间列
 mtcars %>% 
 group_by(gear) %>% 
 nest %>% 
 inner_join(x) %>% 
 mutate(preds = map2(model, data, predict, interval = "confidence")) %>% 
 unnest(data, preds)

这个returns错误:

Error in vec_rbind(!!!x, .ptype = ptype) : Internal error in `vec_assign()`: `value` should have been recycled to fit `x`.

错误在最后一行的unnest()中触发。我认为问题与 predict() 的输出格式有关,它是一个 3 列数据框(fit、upr、lwr)。如有任何帮助,我们将不胜感激!

predict的输出是一个矩阵,将其转换为dataframe,然后unnest

library(tidyverse)

mtcars %>% 
  group_by(gear) %>% 
  nest %>% 
  inner_join(x) %>% 
  mutate(preds = map2(model, data, 
         ~as.data.frame(predict(.x, .y, interval = "confidence")))) %>%
  unnest(cols = c(preds, data))


#   gear   mpg   cyl  disp    hp  drat    wt  qsec    vs    am  carb model    fit   lwr   upr
#   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <list> <dbl> <dbl> <dbl>
# 1     4  21       6 160     110  3.9   2.62  16.5     0     1     4 <lm>    22.0  19.6  24.4
# 2     4  21       6 160     110  3.9   2.88  17.0     0     1     4 <lm>    21.2  19.2  23.2
# 3     4  22.8     4 108      93  3.85  2.32  18.6     1     1     1 <lm>    25.1  23.0  27.1
# 4     4  24.4     4 147.     62  3.69  3.19  20       1     0     2 <lm>    26.0  21.5  30.6
# 5     4  22.8     4 141.     95  3.92  3.15  22.9     1     0     2 <lm>    22.2  19.9  24.4
# 6     4  19.2     6 168.    123  3.92  3.44  18.3     1     0     4 <lm>    17.8  15.1  20.5
# 7     4  17.8     6 168.    123  3.92  3.44  18.9     1     0     4 <lm>    17.8  15.1  20.5
# 8     4  32.4     4  78.7    66  4.08  2.2   19.5     1     1     1 <lm>    28.7  26.6  30.8
# 9     4  30.4     4  75.7    52  4.93  1.62  18.5     1     1     2 <lm>    32.3  29.3  35.3
#10     4  33.9     4  71.1    65  4.22  1.84  19.9     1     1     1 <lm>    30.0  27.5  32.5
# … with 22 more rows