I'm estimating regressions models by groups in my dataset and then I wish to add the correct fitted values for all groups.
I'm trying the following:
library(dplyr)
library(modelr)
df <- tribble(
~year, ~country, ~value,
2001, "France", 55,
2002, "France", 53,
2003, "France", 31,
2004, "France", 10,
2005, "France", 30,
2006, "France", 37,
2007, "France", 54,
2008, "France", 58,
2009, "France", 50,
2010, "France", 40,
2011, "France", 49,
2001, "USA", 55,
2002, "USA", 53,
2003, "USA", 64,
2004, "USA", 40,
2005, "USA", 30,
2006, "USA", 39,
2007, "USA", 55,
2008, "USA", 53,
2009, "USA", 71,
2010, "USA", 44,
2011, "USA", 40
)
rmod <- df %>%
group_by(country) %>%
do(fitModels = lm("value ~ year", data = .))
df <- df %>%
add_predictions(rmod)
which throws the error:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "c('rowwise_df', 'tbl_df', 'tbl', 'data.frame')"
I would like to get either one column with each of the fitted values for the country or one column with predictions per country. Somehow the add_predictions()
function doesn't seem to work when the models are saved as a list after a do()
call.