Suppose you have a vector of data in x and response values in y. How do you plot together both the points (x,y) and the curve that results from the fitted model, if the model is not y ~ x, but a higher order polynomial, e.g. y~poly(x,2)? (In other words, abline doesn't work for this case.) Thanks, --Paul -- Paul Lynch Aquilent, Inc. National Library of Medicine (Contractor)
On 4/16/07, Paul Lynch <plynchnlm at gmail.com> wrote:> Suppose you have a vector of data in x and response values in y. How > do you plot together both the points (x,y) and the curve that results > from the fitted model, if the model is not y ~ x, but a higher order > polynomial, e.g. y~poly(x,2)? (In other words, abline doesn't work > for this case.)I like the following scheme because it's concise: u = rnorm(100) v = u^2 + rnorm(100, sd = .2) plot(u, v) curve(predict(lm(v ~ poly(u, 2)), newdata = list(u = x)), add = TRUE) The last line will only make sense when you realize that the symbol 'x' is special in curve(). -Deepayan
On 4/16/07, Paul Lynch <plynchnlm at gmail.com> wrote:> Suppose you have a vector of data in x and response values in y. How > do you plot together both the points (x,y) and the curve that results > from the fitted model, if the model is not y ~ x, but a higher order > polynomial, e.g. y~poly(x,2)? (In other words, abline doesn't work > for this case.)One way is to use ggplot: install.packages("ggplot") library(ggplot) qplot(mpg, wt, data=mtcars, type=c("point","smooth")) qplot(mpg, wt, data=mtcars, type=c("point","smooth"), method=lm) qplot(mpg, wt, data=mtcars, type=c("point","smooth"), method=lm, formula=y~poly(x,2)) library(splines) qplot(mpg, wt, data=mtcars, type=c("point","smooth"), method=lm, formula=y~ns(x,2)) Regards, Hadley