查找 grouped_by 线性模型的预测

Find predictions for linear model that is grouped_by

我想根据适合训练数据集的模型获得预测值。我以前做过这个,但现在我有一个分组因素,它让我失望了。我想根据每个环境的人口预测生物量。

library(tidyverse)

fit_mods<-df %>%
  group_by(environ) %>%
  do(model = lm(biomass ~ poly(population, 2), data = .))

最终,我会想找到最大的种群生物量。通常我会通过创建一个网格和 运行 基于我的新值的模型并找到最大值来做到这一点,但我不知道如何通过分组来做到这一点。通常的方式:

min_pop <- min(df$population)
max_pop <- max(df$population)

grid_pop <- expand.grid(new = (seq(from = min_pop,
                                   to = max_pop, 
                                   length.out = 1000)),
                        environ = c("A", "B"))  

 #This is what I did with ungrouped data, but doesn't work now.
 pred_pop <- predict(object = fit_mods, 
                newdata = grid_pop,
                interval = "predict")  

这是一些虚拟数据:

  df <- as.data.frame(list(environ = c("a", "a", "a", "a", "a", "b", "b", "b", "b", "b"),
   population = c(2, 3, 4, 5, 6, 3, 4, 5, 6, 7), 
   biomass = c(1, 2.2, 3.5, 4.1, 3.8, 2.5, 3.6, 4.3, 5.2, 5.1)), class = "data.frame")

tidyverse many models 方法中,您可以按以下方式进行:

library(tidyverse)

   fit_mods <- df %>%
     nest(-environ) %>% 
     mutate(models = map(data, ~ lm(biomass ~ poly(population, 2), data = .x)),
         min_pop = map_dbl(data, ~ pull(.x, population) %>% min),
         max_pop = map_dbl(data, ~ pull(.x, population) %>% max),
         new = map2(min_pop, max_pop, ~ tibble(population = seq(from = .x,
                                                to = .y, 
                                                length.out = 1000))),
         pred = map2(models,
                     new,
                     ~ predict(object = .x,
                               newdata = select(.y,population),
                               interval = "predict")))