Re: [R] how do I plot a regression curve with the data?

2009-10-28 Thread Peter Ehlers


Ken Ervin wrote:
I have a data set of 6 or so ordered pairs, and I've been able to graph 
them and have decided to use a high-order polynomial regression.  I've 
used the following piece of code:


regression - function(x,y) {
   x - c(insert_numbers_here)
   y - c(insert_other_numbers_here)
   fit - lm(y ~ x + I(x^2) + I(x^3) + I(x^4) + I(x^5) + I(x^6) + I(x^7) 
+ I(x^8) + I(x^9))

   summary(fit)

This gives me the coefficients for the regression very nicely, but I 
would like to plot both the data and the regression curve together.  How 
do I plot that regression curve as a function, and can I put it on the 
same set of axes as my data scatter plot?



Are you sure that fitting such a high-degree polynomial makes
sense? Is there any theory to support the model? If you really
want to do this, then use predict.lm():

## with xmin, xmax as the limits of your scatterplot
 xx - seq(xmin, xmax, length=51)
 yy - predict(fit, newdata=list(x=xx))
 lines(xx, yy)  ## add to scatterplot

 -Peter Ehlers



Thanks in advance for your help!

-KE

__
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] how do I plot a regression curve with the data?

2009-10-28 Thread Tom Gottfried
?curve

regards,
Tom

Ken Ervin schrieb:
 I have a data set of 6 or so ordered pairs, and I've been able to graph
 them and have decided to use a high-order polynomial regression.  I've
 used the following piece of code:
 
 regression - function(x,y) {
x - c(insert_numbers_here)
y - c(insert_other_numbers_here)
fit - lm(y ~ x + I(x^2) + I(x^3) + I(x^4) + I(x^5) + I(x^6) + I(x^7)
 + I(x^8) + I(x^9))
summary(fit)
 
 This gives me the coefficients for the regression very nicely, but I
 would like to plot both the data and the regression curve together.  How
 do I plot that regression curve as a function, and can I put it on the
 same set of axes as my data scatter plot?
 
 Thanks in advance for your help!
 
 -KE
 
 __
 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] how do I plot a regression curve with the data?

2009-10-28 Thread Kingsford Jones
On Wed, Oct 28, 2009 at 9:23 AM, Tom Gottfried tom.gottfr...@wzw.tum.de wrote:
 ?curve

 regards,
 Tom

and I was in the process of writing a curve example when I noticed Tom
sent this.  Here it is:

set.seed(777)
x - runif(100, 0, 100)
y - 10*x + x^2 - .01*x^3 + rnorm(100, 0, 500)
fit - lm(y ~ x + I(x^2) + I(x^3))
B - coef(fit)
plot(x, y)
curve(10*x + x^2 - .01*x^3, col = 4, lty = 4,
  lwd = 2, add = TRUE)
curve(B[1] + B[2]*x + B[3]*x^2 + B[4]*x^3, col = 2,
  lty = 2, lwd = 2, add = TRUE)
legend('topleft', c('truth', 'fit'), lty = c(4, 2),
   col = c(4, 2), lwd = 2)


btw, it's not possible to fit a 9th-degree polynomial model with lm
based on only '6 or so ordered pairs' (assuming that means 6 data
points).  A 5th degree polynomial would be a direct interpolater.

hth,

Kingsford


 Ken Ervin schrieb:
 I have a data set of 6 or so ordered pairs, and I've been able to graph
 them and have decided to use a high-order polynomial regression.  I've
 used the following piece of code:

 regression - function(x,y) {
    x - c(insert_numbers_here)
    y - c(insert_other_numbers_here)
    fit - lm(y ~ x + I(x^2) + I(x^3) + I(x^4) + I(x^5) + I(x^6) + I(x^7)
 + I(x^8) + I(x^9))
    summary(fit)

 This gives me the coefficients for the regression very nicely, but I
 would like to plot both the data and the regression curve together.  How
 do I plot that regression curve as a function, and can I put it on the
 same set of axes as my data scatter plot?

 Thanks in advance for your help!

 -KE

 __
 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.


__
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] how do I plot a regression curve with the data?

2009-10-27 Thread andrew
Hi Ken,

Perhaps something like

plot(x,y)
lines(sort(x), fit$fitted.values[order(x)])

should do what I think you want.  I think you have to be a bit careful
on the ordering of the fitted values so that they match up with the
order of the x variable, otherwise you get an odd looking line plot.

Regards,

Andrew

On Oct 28, 5:42 am, Ken Ervin ervinj...@gmail.com wrote:
 I have a data set of 6 or so ordered pairs, and I've been able to graph
 them and have decided to use a high-order polynomial regression.  I've
 used the following piece of code:

 regression - function(x,y) {
     x - c(insert_numbers_here)
     y - c(insert_other_numbers_here)
     fit - lm(y ~ x + I(x^2) + I(x^3) + I(x^4) + I(x^5) + I(x^6) +
 I(x^7) + I(x^8) + I(x^9))
     summary(fit)

 This gives me the coefficients for the regression very nicely, but I
 would like to plot both the data and the regression curve together.  How
 do I plot that regression curve as a function, and can I put it on the
 same set of axes as my data scatter plot?

 Thanks in advance for your help!

 -KE

 __
 r-h...@r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guidehttp://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.