using predict with a list of lm() objects

前端 未结 6 1931
甜味超标
甜味超标 2020-12-07 23:25

I have data which I regularly run regressions on. Each \"chunk\" of data gets fit a different regression. Each state, for example, might have a different function that expla

6条回答
  •  离开以前
    2020-12-07 23:54

    You need to use mdply to supply both the model and the data to each function call:

    dataList <- dlply(newData, "state")
    
    preds <- mdply(cbind(mod = modelList, df = dataList), function(mod, df) {
      mutate(df, pred = predict(mod, newdata = df))
    })
    

提交回复
热议问题