Predict() - Maybe I'm not understanding it

前端 未结 4 1778
梦毁少年i
梦毁少年i 2020-11-22 08:19

I posted earlier today about an error I was getting with using the predict function. I was able to get that corrected, and thought I was on the right path.

4条回答
  •  感动是毒
    2020-11-22 08:53

    To avoid error, an important point about the new dataset is the name of independent variable. It must be the same as reported in the model. Another way is to nest the two function without creating a new dataset

    model <- lm(Coupon ~ Total, data=df)
    predict(model, data.frame(Total=c(79037022, 83100656, 104299800)))
    

    Pay attention on the model. The next two commands are similar, but for predict function, the first work the second don't work.

    model <- lm(Coupon ~ Total, data=df) #Ok
    model <- lm(df$Coupon ~ df$Total) #Ko
    

提交回复
热议问题