pdf("g01.pdf");options(width=64)
#Examine the parent-child height data.  Data are from
#https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/T0HSJ1
#Documentation claims that these are formatted for Stata; fortunately R reads 
#them just fine.  Tell R that the first row contains column names.
galton<-read.table("galton-stata11.tab",header=TRUE)
#First examine daughters.
daughters<-galton[galton$female==1,]
#Families have multiple children.  Most of what we will do this semester will
#require observations to be independent, and so keep only the first daughter.
first<-c(TRUE,daughters$family[-1]!=daughters$family[-length(daughters$family)])
fd<-daughters[first,c("mother","father","height")]
#Examine column headings in larger data set.
names(galton)
#Plot heights of first daughters.
plot(fd$mother,fd$height,
   main="Heights of Mother-First Daughter Pairs, Galton Data",
   xlab="Parent height (in)", ylab="Child height (in)")
abline(a=0,b=1)
abline(lm(height~mother,data=fd),lty=2)
#Examine sons the same way.
sons<-galton[galton$male==1,]
#Families have multiple children.  Most of what we will do this semester will
#require observations to be independent, and so keep only the first daughter.
first<-c(TRUE,sons$family[-1]!=sons$family[-length(sons$family)])
firstsons<-sons[first,c("father","height")]
plot(firstsons$father,firstsons$height,
   main="Heights of Father-First Son Pairs, Galton Data",
   xlab="Parent height (in)", ylab="Child height (in)")
abline(a=0,b=1)
abline(lm(height~father,data=firstsons),lty=2)
plot(fd$mother,fd$height,
   main="Heights of Mother-First Daughter Pairs, Galton Data",
   xlab="Parent height (in)", ylab="Child height (in)")
lm(height~mother,data=fd)
attach(fd)
beta1hat<-sum((mother-mean(mother))*(height-mean(height)))/sum(
   (mother-mean(mother))^2)
beta0hat<-mean(height)-beta1hat*mean(mother)
matrix<-cbind(mother,(mother-mean(mother))^2,height,
   (height-mean(height))*(mother-mean(mother)))
detach(fd)
# The more traditional results are given in the library NonparametricHeuristic.
# This is my catch-all package for teaching materials, and is hosted on a
# site github.  To use this you must have previously run
# install.packages("devtools"); library(devtools) ; install_github("kolassa-dev/NonparametricHeuristic")
library(NonparametricHeuristic)# For squaresplot
attach(fd)
squaresplot(mother,height)
squaresplot(mother,height,centerx=TRUE)
sigmasqhat<-sum((height-beta0hat-beta1hat*mother)^2)/(length(mother)-2)
print(sigmasqhat)
detach(fd)
<\/pre>