绘制多项多项式回归曲线 [英] Plot multiple polynomial regression curve
问题描述
我试图只绘制几条回归线,而不是任何点.(没有 fitted
,因为我有超过 7000 个点.)我知道如何使用线性回归来做到这一点,但不知道如何使用多项式回归.我的数据在
但是,如果我尝试创建多项式回归,则无法执行此操作.我想要这些回归的相同类型的图,但是 termplot
确实与自变量一起工作并单独进行.
Off_exp=lm(Z_Salary~OBPM+I(OBPM^2),data=plot_data)Def_exp=lm(Z_Salary~DBPM+I(DBPM^2),data=plot_data)Tot_exp=lm(Z_Salary~BPM+I(BPM^2),data=plot_data)
@Dason 已经给你提示了.在下面找到一些代码以使其工作.
Off_exp=lm(Z_Salary~OBPM+I(OBPM^2),data=plot_data)x=seq(from=range(plot_data$OBPM)[1], to=range(plot_data$OBPM)[2])y=predict(Off_exp, newdata=list(OBPM=x))情节(x,y,类型=l",col=蓝色",xlab=OBPM",ylab=Z_Salary")图例(左上角",图例 =d2",col =蓝色",lty = 1,标题 =polys")
它看起来像这样:
希望有帮助.
I am trying to plot only a few regression lines and not any of the points. (No fitted
, because I have over 7 thousand points.) I know how to do this with linear regressions, but not with polynomial regression. My data is here. With a few linear regressions:
plot_data=read.csv("plot_data.csv") #read data
#linear regressions
Off_linear=lm(Z_Salary~OBPM,data=plot_data)
Def_linear=lm(Z_Salary~DBPM,data=plot_data)
Tot_linear=lm(Z_Salary~BPM,data=plot_data)
#try to plot. This works. Not sure how to add legend
termplot(Def_linear, ylab='Z_Salary',xlab='BPM',ylim=c(-2, 2))
abline(Off_linear)
abline(Tot_linear,col='blue')
However I cannot do this if I try to create polynomial regressions. I want the same kind of plot with these regressions, but termplot
does work with independent variables together and does them separately.
Off_exp=lm(Z_Salary~OBPM+I(OBPM^2),data=plot_data)
Def_exp=lm(Z_Salary~DBPM+I(DBPM^2),data=plot_data)
Tot_exp=lm(Z_Salary~BPM+I(BPM^2),data=plot_data)
@Dason already gave you the hint. Find below some code to make it work.
Off_exp=lm(Z_Salary~OBPM+I(OBPM^2),data=plot_data)
x=seq(from=range(plot_data$OBPM)[1], to=range(plot_data$OBPM)[2])
y=predict(Off_exp, newdata=list(OBPM=x))
plot(x, y, type="l", col="blue", xlab="OBPM", ylab="Z_Salary")
legend("topleft", legend="d2", col="blue", lty=1, title="polys")
It would look something like this:
Hope it helps.
这篇关于绘制多项多项式回归曲线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!