[R] Plotting data with a fitted curve
Deepayan Sarkar
deepayan.sarkar at gmail.com
Tue Apr 17 02:16:13 CEST 2007
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
More information about the R-help
mailing list