[R] abline and linearity over groups

Adaikalavan Ramasamy ramasamy at cancer.org.uk
Thu Aug 4 03:05:15 CEST 2005


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:
> But those two lines are almose identical The difference between i=0.4432, s=104.1688 and i=0.8776,s=108.1313 is almost negligible.
> 
> What I see is that abline draws a line with a v.similar slope but intercept is about 90 instead of 0.8776. Try running the example to see what I mean.
> 
> ronggui <0034058 at fudan.edu.cn> wrote:>?abline
> and you can see
> ...
> '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.
> ...
> 
> and
> > plot(test$l~test$t)
> > abline(lm(test$l~test$t))
> > (lm(test$l~test$t))
> 
> Call:
> lm(formula = test$l ~ test$t)
> 
> Coefficients:
> (Intercept) test$t 
> 0.4432 104.1688 
> 
> > test$tF=factor(test$t)
> > plot(test$l~test$tF)
> > abline(lm(test$l~test$tF))
> > (lm(test$l~test$tF))
> 
> Call:
> lm(formula = test$l ~ test$tF)
> 
> Coefficients:
> (Intercept) test$tF1 test$tF2 
> -0.8776 108.1313 208.3376 
> 
> when test$tF is factor,these are 3 coef and the first two are used to drow the line,with Intercept = -0.8776 and slope= 108.1313 ,and abline(lm(test$l~test$tF)) is abline(-0.8776,108.1313)
> 
> 
> ======= 2005-08-03 22:23:57 Ð´=======
> 
> >
> >
> >Dear R users, please can you help me understand the behaviour of abline using function lm.
> >
> >I'm trying to learn linearity over groups. So I make three groups with 10 values each:
> >
> >test=data.frame(cbind(
> >l=c(rnorm(10,0,30),rnorm(10,100,30),rnorm(10,200,30)),
> >t = c(rep(0,10), rep(1,10), rep(2,10))
> >))
> >
> >when I do: 
> >
> >plot(test$l~test$t)
> >abline(lm(test$l~test$t))
> >
> >
> >the abline is a straight line through the centre of the points of each of the groups.
> >
> >If, however, I factorise the groups (in order to do e.g. anova analysis) and then plot the data
> >
> >test$tF=factor(test$t)
> >plot(test$l~test$tF)
> >abline(lm(test$l~test$tF))
> >
> >
> >the abline is now shifted up and to the left of where I would expect the line to go (through the centre of the points of each of the groups).
> >
> >If there is a simple explanation, could someone tell me it?
> >
> > 
> >
> > 
> >---------------------------------
> >To help you stay safe and secure online, we've developed the all new Yahoo! Security Centre.
> > [[alternative HTML version deleted]]
> >
> >______________________________________________
> >R-help at stat.math.ethz.ch mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-help
> >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
> = = = = = = = = = = = = = = = = = = = =
> 
> 
> 
> 
> 
> 2005-08-03
> 
> ------
> Deparment of Sociology
> Fudan University
> 
> Blog:http://sociology.yculblog.com
> 
> 
> 		
> ---------------------------------
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list