Skip to content

Instantly share code, notes, and snippets.

@michiel
Created March 21, 2015 15:52
Show Gist options
  • Save michiel/ce720390c2c44d6e9a32 to your computer and use it in GitHub Desktop.
Save michiel/ce720390c2c44d6e9a32 to your computer and use it in GitHub Desktop.
Galton parent/child frequency plot
library(HistData)
galton <- Galton
y <- galton$child
x <- galton$parent
freqData <- as.data.frame(table(galton$child, galton$parent))
names(freqData) <- c("child", "parent", "freq")
plot(as.numeric(as.vector(freqData$parent)),
as.numeric(as.vector(freqData$child)),
pch = 21, col = "black", bg = "lightblue",
cex = .07 * freqData$freq, xlab = "parent", ylab = "child")
#original regression line, children as outcome, parents as predictor
abline(mean(y) - mean(x) * cor(y, x) * sd(y) / sd(x), #intercept
sd(y) / sd(x) * cor(y, x), #slope
lwd = 3, col = "red")
#new regression line, parents as outcome, children as predictor
abline(mean(y) - mean(x) * sd(y) / sd(x) / cor(y, x), #intercept
sd(y) / cor(y, x) / sd(x), #slope
lwd = 3, col = "blue")
#assume correlation is 1 so slope is ratio of std deviations
abline(mean(y) - mean(x) * sd(y) / sd(x), #intercept
sd(y) / sd(x), #slope
lwd = 2)
points(mean(x), mean(y), cex = 2, pch = 19) #big point of intersection
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment