Re: [R] plotting polynomial regression line
Thanks very much it solved my problem. cheers, Amit On Sun, Dec 20, 2009 at 7:56 PM, David Winsemius dwinsem...@comcast.net wrote: On Dec 20, 2009, at 1:35 PM, 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! abline is designed to draw lines of the form y=a+bx, ... hence the name. (And as documented on its help page for regression objects). An effective method would be with lines and predict: plot(cars, main=Stopping Distance versus Speed) lines(cars$speed, predict(lm(dist~poly(speed, 6) ,data=cars) ), col=red) Best Amit ___ David Winsemius, MD Heritage Laboratories West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] plotting polynomial regression line
On 20.12.2009 19:35, 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 abline() draws straight lines. 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! You can predict() some points along your x values and connect them via lines(). Best wishes, Uwe Ligges Best Amit __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] plotting polynomial regression line
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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] plotting polynomial regression line
Amit, please provide gp.txt file. On Sun, Dec 20, 2009 at 1:47 PM, Jason Morgan jwm-r-h...@skepsi.net wrote: 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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.htmlhttp://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] plotting polynomial regression line
On Dec 20, 2009, at 1:35 PM, 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! abline is designed to draw lines of the form y=a+bx, ... hence the name. (And as documented on its help page for regression objects). An effective method would be with lines and predict: plot(cars, main=Stopping Distance versus Speed) lines(cars$speed, predict(lm(dist~poly(speed, 6) ,data=cars) ), col=red) Best Amit ___ David Winsemius, MD Heritage Laboratories West Hartford, CT __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.