I'm trying to do a sunflowerplot of Galton's data, with both regression lines and data ellipses, and I must be doing something wrong, because the lines do not intersect at \bar{x}, \bar{y}. The problem is likely in the line for x.y, but I don't know how to specify that correctly. The data is read in grouped form( galton), and then ungrouped (galton2): galton <- read.table("~/sasuser/data/galton.txt", header=TRUE) # ungroup galton2<-galton[rep(1:nrow(galton), galton$frequency), 1:2] attach(galton) sunflowerplot(child, parent, number=frequency, xlim=c(61,75), ylim=c(61,75), xlab="Child height", ylab="Mid Parent height") # both attempts plot the same, wrong regression lines y.x <- lm(parent ~ child, weights=frequency) abline(y.x) x.y <- lm(child ~ parent, weights=frequency) abline(x.y, col="gray") attach(galton2) y.x <- lm(parent ~ child) abline(y.x, lwd=2) x.y <- lm(child ~ parent) abline(x.y, col="gray", lwd=2) library(car) data.ellipse(child, parent, plot.points=FALSE, levels=c(0.40, 0.68), lty=2) The resulting figure may be seen at http://euclid.psych.yorku.ca/SCS/Gallery/Private/galton.jpg -Michael -- Michael Friendly Email: friendly at yorku.ca Professor, Psychology Dept. York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html Toronto, ONT M3J 1P3 CANADA
Michael Friendly <friendly at yorku.ca> writes:> The data is read in grouped form( galton), and then ungrouped (galton2): > > galton <- read.table("~/sasuser/data/galton.txt", header=TRUE) > # ungroup > galton2<-galton[rep(1:nrow(galton), galton$frequency), 1:2] > > attach(galton) > sunflowerplot(child, parent, number=frequency, xlim=c(61,75), > ylim=c(61,75), > xlab="Child height", ylab="Mid Parent height") > > # both attempts plot the same, wrong regression linesNot precisely, though.> y.x <- lm(parent ~ child, weights=frequency) > abline(y.x) > x.y <- lm(child ~ parent, weights=frequency) > abline(x.y, col="gray") > > attach(galton2) > y.x <- lm(parent ~ child) > abline(y.x, lwd=2) > x.y <- lm(child ~ parent) > abline(x.y, col="gray", lwd=2) > > library(car) > data.ellipse(child, parent, plot.points=FALSE, levels=c(0.40, 0.68), lty=2)The thing that you need in the x.y case is that x = a + by implies y = 1/b x - a/b so you need cc <- coef(x.y) abline(-cc[1]/cc[2], 1/cc[2], ....) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Friday 20 February 2004 08:17, Michael Friendly wrote:> I'm trying to do a sunflowerplot of Galton's data, with both regression > lines and data ellipses, > and I must be doing something wrong, because the lines do not intersect > at \bar{x}, \bar{y}. > The problem is likely in the line for x.y, but I don't know how to > specify that correctly.[...]> # both attempts plot the same, wrong regression lines > y.x <- lm(parent ~ child, weights=frequency) > abline(y.x) > x.y <- lm(child ~ parent, weights=frequency) > abline(x.y, col="gray") > > attach(galton2) > y.x <- lm(parent ~ child) > abline(y.x, lwd=2) > x.y <- lm(child ~ parent) > abline(x.y, col="gray", lwd=2)This looks like you are getting a line with equation of the form x = c0 + c1 y and then plotting the line y = c0 + c1 y You should probably do cx.y <- coef(x.y) cx.y <- c(-cx.y[1], 1) / cx.y[2] abline(cx.y) [Code untested, though] Deepayan
Dear Mike, I believe that the following is the problem: For the regression of x on y, you have to move y to the left-hand-side of the equation to plot the line in {x, y}; so if the regression equation from the model x.y is x = a + b y, you need abline(-a/b, 1/b). I hope that this helps, John At 09:17 AM 2/20/2004 -0500, Michael Friendly wrote:>I'm trying to do a sunflowerplot of Galton's data, with both regression >lines and data ellipses, >and I must be doing something wrong, because the lines do not intersect at >\bar{x}, \bar{y}. >The problem is likely in the line for x.y, but I don't know how to specify >that correctly. > >The data is read in grouped form( galton), and then ungrouped (galton2): > >galton <- read.table("~/sasuser/data/galton.txt", header=TRUE) ># ungroup >galton2<-galton[rep(1:nrow(galton), galton$frequency), 1:2] > >attach(galton) >sunflowerplot(child, parent, number=frequency, xlim=c(61,75), ylim=c(61,75), > xlab="Child height", ylab="Mid Parent height") > ># both attempts plot the same, wrong regression lines >y.x <- lm(parent ~ child, weights=frequency) >abline(y.x) >x.y <- lm(child ~ parent, weights=frequency) >abline(x.y, col="gray") > >attach(galton2) >y.x <- lm(parent ~ child) >abline(y.x, lwd=2) >x.y <- lm(child ~ parent) >abline(x.y, col="gray", lwd=2) > >library(car) >data.ellipse(child, parent, plot.points=FALSE, levels=c(0.40, 0.68), lty=2) > >The resulting figure may be seen at >http://euclid.psych.yorku.ca/SCS/Gallery/Private/galton.jpg > >-Michael > >-- >Michael Friendly Email: friendly at yorku.ca Professor, Psychology Dept. >York University Voice: 416 736-5115 x66249 Fax: 416 736-5814 >4700 Keele Street http://www.math.yorku.ca/SCS/friendly.html >Toronto, ONT M3J 1P3 CANADA > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >https://www.stat.math.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox