[R] Plotting polynomial fit

David Winsemius dwinsemius at comcast.net
Mon Apr 27 05:08:05 CEST 2009


On Apr 26, 2009, at 8:40 PM, Ronnen Levinson wrote:

> Hi.
>
> Is there an analog to abline() that can be used to plot a polynomial  
> fit?
>
> For example, I can draw the straight-line fit
>
>    fit <- lm(y ~ x)
>
> via
>
>    abline(coef=fit$coef)
>
> but I'm not sure how to draw the polynomial fit
>
>    fit <- lm(y ~ poly(x,2))
>
> I do see the function curve(), but not how to prepare an expr for
> curve() based on the coefficients returned by the polynomial fit.
>
> Thanks for your help,

Use instead the predict function. Something like:

lines(x=seq(min(x), max(x), length=100), y= predict( fit,  
newdata=  ....) #insert properly constructed dataframe



David Winsemius, MD
Heritage Laboratories
West Hartford, CT



More information about the R-help mailing list