我在我的数据集中按组估计回归模型,然后我希望为所有组添加正确的拟合值。
我正在尝试以下操作:
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)
引发错误:
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "c('rowwise_df', 'tbl_df', 'tbl', 'data.frame')"
我想得到一个包含该国家/地区每个拟合值的列,或者一个包含每个国家/地区预测的列。当模型在调用后保存为列表时,该add_predictions()
功能似乎不起作用。do()