[R] using abline and a fitted 2nd degree formula

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Nov 9 11:36:15 CET 2005


On Wed, 9 Nov 2005, CG Pettersson wrote:

> Hello all,
>
> R2.1.1, Wk2
           ^^^  week 2?
> I am doing some two-step plotting, first using plot() to illustrate the
> datapoints and then using abline() to place a trend line from a fitted
> model into the plot.
>
> Everything works well as long as the formula of the fitted model i of the
> type:
>
> m1 <- lm(Dependent ~ Independent)
> then abline(m1) puts the proper straight line into the plot.
>
> But if I use:
>
> m2 <- lm(Dependent ~ Independent + I(Independent^2))
> abline(m2) produces a straight line, only from the first order term.
>
> Why, and what should I do about it?

Why: because that is what it is documented to do:

      'reg' is a regression object which contains 'reg$coef'.  If it is
      of length 1 then the value is taken to be the slope of a line
      through the origin, otherwise, the first 2 values are taken to be
      the intercept and slope.

What to do about it: first, read the help page and second, use predict and 
lines.  There is an example of the latter on ?cars.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list