Skip to content
Prev 74897 / 398502 Next

abline and linearity over groups

I think ronggui is right and your example is just a coincidence. Here is
my example in which case the intercept is hugely different 
(with slightly different style of coding).


  set.seed(1)  # for reproducibility 
  y  <- c( rnorm(10, 0, 30), rnorm(10, 100, 30), rnorm(10, 200, 30) )
  x  <- rep( 1:3, each=10 )

  df <- cbind.data.frame( y=y, x1=x, x2=factor(x) )
  plot(df$x2, df$y)
  points(df$x1, df$y, col=2, pch=2)

  ( fit1 <- lm( y ~ x1, data=df ) )
      (Intercept)           x1
           -89.55        96.01


  ( fit2 <- lm( y ~ x2, data=df ) )
      (Intercept)          x22          x23
            3.966      103.499      192.024

  abline(fit1)
  abline(fit2, col="red")  # wrong

The line above is wrong because it is fitting 

   abline(3.966, 103.499, col="green", lty=3)

as documented in help(abline) and pointed out by ronggui.


Note that 'fit1' is a linear model for regression while 
'fit2' is a linear model for ANOVA and that the documentation
of help(abline) uses the word "regression". Perhaps

It is more reliable to plot the fitted or predicted values via

   points( df$x2, fit2$fitted, col=4, pch=20 ) 

and this works regardless whether the linear model if for regression
or ANOVA.

You could replace plot() with lines() but this is perhaps not
appropriate with an ANOVA fit which may not have numerical values for x.


Regards, Adai
On Wed, 2005-08-03 at 16:33 +0100, Jabez Wilson wrote: