Reputation: 2100
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)
Upvotes: 0
Views: 1274
Reputation: 3947
@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.
Upvotes: 1