Regression Examples
install.packages("UsingR",repos = "http://cran.us.r-project.org")
##
## The downloaded binary packages are in
## /var/folders/4z/9kr7k7jd1q7gc792xjkrwf040000gn/T//RtmpEBWhff/downloaded_packages
library(UsingR)
## Loading required package: MASS
## Loading required package: HistData
## Loading required package: Hmisc
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 3.3.2
##
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:base':
##
## format.pval, round.POSIXt, trunc.POSIXt, units
##
## Attaching package: 'UsingR'
## The following object is masked from 'package:survival':
##
## cancer
data(galton)
head(galton)
## child parent
## 1 61.7 70.5
## 2 61.7 68.5
## 3 61.7 65.5
## 4 61.7 64.5
## 5 61.7 64.0
## 6 62.2 67.5
par(mfrow=c(1,2))
hist(galton$child, col="blue", breaks=100)
abline(h = mean(galton$child), v = mean(galton$child), col="red", lwd=1, lty=1)
hist(galton$parent, col="red", breaks=100)
abline(h = mean(galton$parent), v = mean(galton$parent), col="blue", lwd=1, lty=1)

The least squares estimate is the empirical mean
hist(galton$child, col="blue", breaks = 100)
meanChild<-mean(galton$child)
lines(rep(meanChild, 100), seq(0, 150, length=100), col="red", lwd=5)

Comparing Childrens Height and their Parents heights
plot(galton$parent, galton$child, pch=19, col="blue")

Complete linear regression
install.packages("dplyr",repos = "http://cran.us.r-project.org")
##
## The downloaded binary packages are in
## /var/folders/4z/9kr7k7jd1q7gc792xjkrwf040000gn/T//RtmpEBWhff/downloaded_packages
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:Hmisc':
##
## combine, src, summarize
## The following object is masked from 'package:MASS':
##
## select
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
freqData <- as.data.frame(table(galton$child, galton$parent))
names(freqData) <- c("child", "parent", "freq")
freqData$child <- as.numeric(as.character(freqData$child))
freqData$parent <- as.numeric(as.character(freqData$parent))
g <- ggplot(filter(freqData, freq > 0), aes(x = parent, y = child))
g <- g + scale_size(range = c(2, 10), guide = "none" )
g <- g + geom_point(colour="blue", aes(size = freq+10, show_guide = FALSE))
## Warning: Ignoring unknown aesthetics: show_guide
g <- g + geom_point(aes(colour=freq, size = freq))
g <- g + scale_colour_gradient(low = "white", high="red")
lm1 <- lm(galton$child ~ galton$parent)
g <- g + geom_abline(intercept = coef(lm1)[1], slope = coef(lm1)[2], size = 1, colour = grey(.5))
g
