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)) }
Try this version of your function and then think about it tst <- function () { attach (attitude) x <- rating y <- learning detach (attitude) plot (x, y) abline(v=mean(x)) abline(h=mean(y)) abline (lm(y~x)) cc <- coef(lm(x ~ y)) abline (-cc[1]/cc[2], 1/cc[2]) }> 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)) > }[[alternative text/enriched version deleted]]
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
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?Where did you tell it 'x' was the abscissa and 'y' the ordinate? (Nowhere: R is lacking a mind_read() function!) From the help page: reg is a regression object with a coef method. If this returns a vector 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. There are some changes in R-devel, but not to recognize names of coefficients.> 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)) > } > > ______________________________________________ > 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. >-- 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
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?Well, abline() in fact plots a line using the estimated coefficients for intercept and slope and assumes you have plotted LeftHandSideOfFormula against RightHandSideOfFormula. If you did vice versa, abline() does not respect your mistake. Uwe Ligges> 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)) > } > > ______________________________________________ > 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.
This should do the trick: mind_reader <- function() { ll <- letters[round(runif(6, 1, 26))] ff <- ll[1] for (ix in 2:length(ll)) { ff <- paste(ff, ll[ix], sep = "") } if (exists(ff)) { cat("The function that you were thinking of is") return(ff) } else { cat("please update libopenmind to the patched or development version") } } Prof Brian Ripley wrote:> R is lacking a mind_read() function!)[[alternative text/enriched version deleted]]