1

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")
Nazer
  • 3,654
  • 8
  • 33
  • 47
  • Thanks, I was under the assumption that your 'pred_pop' worked – akrun Oct 18 '19 at 19:41
  • 1
    Do you have to do it with `group_by`?Usually one would do this using `nest` and run the `predict` in a `mutate(data = map(data, ~ predict...)`. – TimTeaFan Oct 18 '19 at 20:16
  • I'm willing to use anything! What would `data` be in the above example? fit_mods? – Nazer Oct 18 '19 at 20:21

1 Answers1

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")))
TimTeaFan
  • 17,549
  • 4
  • 18
  • 39