[R] plotting polynomial regression line

Jason Morgan jwm-r-help at skepsi.net
Sun Dec 20 19:47:14 CET 2009


Hello Amit,

On 2009.12.20 19:35:09, Amit wrote:
> Dear All,
> I am trying to plot polynomial regression line to a scatterplot. I did
> following so far:
> 
> >x=c(1:9335)
> >y=read.table("gp.txt",header=T,sep="\t")
> > length(y$PCC) # y$PCC has values between 1 to 0 in decreasing order
> [1] 9335
> > plot(x,y$PCC,col="red") #scatterplot between x and y$PCC
> > reg=lm(y$PCC~poly(x,6)) # calculating polynomial fit with degree 6
> > abline(reg,col="blue")
> Warning message:
> In abline(reg, col = "blue") :
>   only using the first two of 7regression coefficients
> 
> After the above warning a line is drawn in the graph parallel to the
> y-axis. But I was expecting a curve line through the scatterplot.
> Am I doing something wrong? Please help!

Take a look at ?predict. Briefly, I think this will give you what you
want:

> reg <- lm(y$PCC ~ poly(x,6))
> plot(x, y$PCC, col="red")
> lines(x, predict(reg), col="blue")

Cheers,
Jason


-- 
Jason W. Morgan
Graduate Student
Department of Political Science
*The Ohio State University*
154 North Oval Mall
Columbus, Ohio 43210




More information about the R-help mailing list