[R] Regression lines
Roger Bivand
Roger.Bivand at nhh.no
Fri Jan 12 10:59:06 CET 2007
On Fri, 12 Jan 2007, Tom Backer Johnsen wrote:
> My simpleminded understanding of simple regression is that when
> plotting regression lines for x on y and y on x in the same plot, the
> lines should cross each other at the respective means. But, given the
> R function below, abline (lm(y~x)) works fine, but abline (lm(x~y))
> does not. Why?
>
> function () {
> attach (attitude)
> x <- rating
> y <- learning
> detach (attitude)
> plot (x, y)
> abline(v=mean(x))
> abline(h=mean(y))
> abline (lm(y~x))
> abline (lm(x~y))
> }
The axes are getting reversed:
xylm <- lm(x~y)
newdata <- data.frame(y=0:80)
lines(predict(xylm, newdata), newdata$y, col="blue")
gets them back.
Roger
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-help
mailing list