using predict with a list of lm() objects

前端 未结 6 1941
甜味超标
甜味超标 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-08 00:06

    Here's my attempt:

    predNaughty <- ddply(newData, "state", transform,
      value=predict(modelList[[paste(piece$state[1])]], newdata=piece))
    head(predNaughty)
    #   year state    value
    # 1   50    50 5176.326
    # 2   51    50 5274.907
    # 3   52    50 5373.487
    # 4   53    50 5472.068
    # 5   54    50 5570.649
    # 6   55    50 5669.229
    predDiggsApproved <- ddply(newData, "state", function(x)
      transform(x, value=predict(modelList[[paste(x$state[1])]], newdata=x)))
    head(predDiggsApproved)
    #   year state    value
    # 1   50    50 5176.326
    # 2   51    50 5274.907
    # 3   52    50 5373.487
    # 4   53    50 5472.068
    # 5   54    50 5570.649
    # 6   55    50 5669.229
    

    JD Long edit

    I was inspired enough to work out an adply() option:

    pred3 <- adply(newData, 1,  function(x)
        predict(modelList[[paste(x$state)]], newdata=x))
    head(pred3)
    #   year state        1
    # 1   50    50 5176.326
    # 2   51    50 5274.907
    # 3   52    50 5373.487
    # 4   53    50 5472.068
    # 5   54    50 5570.649
    # 6   55    50 5669.229
    

提交回复
热议问题