{"action":"created","issue":{"url":"https://api.github.com/repos/tidyverse/purrr/issues/464","repository_url":"https://api.github.com/repos/tidyverse/purrr","labels_url":"https://api.github.com/repos/tidyverse/purrr/issues/464/labels{/name}","comments_url":"https://api.github.com/repos/tidyverse/purrr/issues/464/comments","events_url":"https://api.github.com/repos/tidyverse/purrr/issues/464/events","html_url":"https://github.com/tidyverse/purrr/issues/464","id":296805618,"number":464,"title":"map2() - Specify predict type","user":{"login":"tatewr","id":29631660,"avatar_url":"https://avatars1.githubusercontent.com/u/29631660?v=4","gravatar_id":"","url":"https://api.github.com/users/tatewr","html_url":"https://github.com/tatewr","followers_url":"https://api.github.com/users/tatewr/followers","following_url":"https://api.github.com/users/tatewr/following{/other_user}","gists_url":"https://api.github.com/users/tatewr/gists{/gist_id}","starred_url":"https://api.github.com/users/tatewr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tatewr/subscriptions","organizations_url":"https://api.github.com/users/tatewr/orgs","repos_url":"https://api.github.com/users/tatewr/repos","events_url":"https://api.github.com/users/tatewr/events{/privacy}","received_events_url":"https://api.github.com/users/tatewr/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-13T16:40:34Z","updated_at":"2018-02-13T17:18:55Z","closed_at":"2018-02-13T17:18:55Z","author_association":"NONE","body":"Good day.  I've been scouring the web for the last few days looking at the documentation for map2.  I have taken a training set, nested the data and created **coxph** models for it, saving those models in the nested table.  Now I want to **predict** from that model, but I want to use a **type=\"expected\"**.  I'm not seeing how to get that to work.  I'm still pretty new to R; learned a lot from the RStudio conference and tidyverse workshop, so really looking to continue my learning in tidy-land. Thanks in advance for the help! -Wendy\r\n\r\nUsing the example from the documentation, how would I add the type=\"expected\" to this function?\r\n# Split into pieces, fit model to each piece, then predict\r\nby_cyl <- mtcars %>% split(.$cyl)\r\nmods <- by_cyl %>% map(~ lm(mpg ~ wt, data = .))\r\nmap2(mods, by_cyl, predict)\r\n\r\nWHAT I'VE TRIED SO FAR:\r\n\r\nI've tried making a formula for the predict function (which works on my single group, non-nested set\r\n`predict.F <- function(model1, data1){\r\n  predict(model1, newdata=data1, type=\"expected\")\r\n}`\r\nbut get the following error: \"Error in mutate_impl(.data, dots) : Evaluation error: requires numeric/complex matrix/vector arguments\"\r\n\r\nWhen I try and put the formula directly in the map2 argument:\r\n`df <- df %>%\r\n mutate(prediction = map2(model1, data1, predict(model1, newdata=data1, type=\"expected\") `\r\nI get the following error: Evaluation error: no applicable method for 'predict' applied to an object of class \"list\"\r\n\r\nI also tried an anonymous function:\r\n`df <- df %>%\r\n mutate(prediction = map2(model1, data, ~ predict(.x, newdata=.y, type=\"expected\") `\r\nbut get the following error: \"Error in mutate_impl(.data, dots) : Evaluation error: requires numeric/complex matrix/vector arguments\"\r\n\r\nFinally, I tried putting the function directly into map2:\r\n`df <- df %>%\r\n mutate(prediction = map2(model1, data, function(model, data) predict(model, newdata=data, type=\"expected\") `\r\nbut get the following error: \"Error in mutate_impl(.data, dots) : Evaluation error: requires numeric/complex matrix/vector arguments\""},"comment":{"url":"https://api.github.com/repos/tidyverse/purrr/issues/comments/365337973","html_url":"https://github.com/tidyverse/purrr/issues/464#issuecomment-365337973","issue_url":"https://api.github.com/repos/tidyverse/purrr/issues/464","id":365337973,"user":{"login":"hadley","id":4196,"avatar_url":"https://avatars3.githubusercontent.com/u/4196?v=4","gravatar_id":"","url":"https://api.github.com/users/hadley","html_url":"https://github.com/hadley","followers_url":"https://api.github.com/users/hadley/followers","following_url":"https://api.github.com/users/hadley/following{/other_user}","gists_url":"https://api.github.com/users/hadley/gists{/gist_id}","starred_url":"https://api.github.com/users/hadley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hadley/subscriptions","organizations_url":"https://api.github.com/users/hadley/orgs","repos_url":"https://api.github.com/users/hadley/repos","events_url":"https://api.github.com/users/hadley/events{/privacy}","received_events_url":"https://api.github.com/users/hadley/received_events","type":"User","site_admin":false},"created_at":"2018-02-13T17:18:55Z","updated_at":"2018-02-13T17:18:55Z","author_association":"OWNER","body":"This sort of question is a better fit for <https://community.rstudio.com>. Do you mind asking it over there? (You might want to read <https://www.tidyverse.org/help/> first to maximise your chances of getting a good answer)"}}
{"actor":{"display_login":"hadley"}}