Predict y value for a given x in R

懵懂的女人 提交于 2019-12-02 01:37:33

If your purposes are related to just one prediction you can just grab your coefficient with

coef(mod)

Or you can just build a simple equation like this.

coef(mod)[1] + "Your_Value"*coef(mod)[2]

Its usually more robust to use the predict method of lm:

f2<-data.frame(age=c(10,20,30),weight=c(100,200,300))
f3<-data.frame(age=c(15,25))
mod<-lm(weight~age,data=f2)
pred3<-predict(mod,f3)

This spares you from wrangling with all of the coefs when the models can be potentially large.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!