I am having a hard time using pmap. I would like to pass a model to pmap and then again to predict(). I am able to get what I need with map2 but I want to know how to do this with pmap.
# r 4.1, native pipe has no _ placeholder
library(tidyverse)
diamonds |>
group_by(cut, color) |>
nest() |>
mutate(
mod.lm = map(data, ~ lm(price ~ depth, data = .x)),
# map2 works, I want to just do the same with pmap
#test_data = map2(data, mod.lm, ~ .x |> mutate(prediction_lm = predict(object = .y))) # works
test_data = pmap(list(data, mod.lm), function(x, y) x |> mutate(prediction_lm = predict(object = y)))
)
Gives error:
Error: Problem with
mutate()columntest_data. ℹtest_data = pmap(list(data, mod.lm), function(x, y) mutate(x, prediction_lm = predict(object = y))). x Problem withmutate()columnprediction_lm. ℹprediction_lm = predict(object = y). x no applicable method for 'predict' applied to an object of class "c('double', 'numeric')"
How can I pass mod.lm to predict within pmap()?