Find predictions for linear model that is grouped_by

左心房为你撑大大i 提交于 2019-12-11 08:29:56

问题


I would like to get predicted values based on a model I fit to a training set of data. I have done this before, but now I have a grouping factor and it is throwing me off. I want to predict biomass based on population for each environment.

library(tidyverse)

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

Ultimately, I will want to find at which population biomass is the greatest. Usually I would do this by creating a grid and running the model on my new values and finding the max value, but I'm blanking on how to do this with the grouping. Usual way:

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

Here is some dummy data:

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

回答1:


In a tidyverse many models approach you could do it the following way:

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


来源:https://stackoverflow.com/questions/58456935/find-predictions-for-linear-model-that-is-grouped-by

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!