library(ggplot2)
library(dplyr)
library(tidyverse)
library(knitr)
library(ggthemes)
library(ISLR2)
library(gglm)
theme_set(theme_bw())
\[Y\approx \beta_0+\beta_1X\]
\[\hat{y}=\hat{\beta_0}+\hat{\beta_1}x\]
\[e_i=y_i-\hat{y_i}\]
We define the residual sum of squares as \(\sum e_i^2\). We can show that the optimal estimators are:
\[\hat{\beta_1}=\frac{\sum_i(x_i-\bar{x})(y_i-\bar{y})}{\sum(x_i-\bar{x})^2}\] \[\hat{\beta_0}=\bar{y}-\hat{\beta_1}\bar{x}\]
These are the least squares coefficient estimates for simple linear regression.
\[\frac{\delta RSS(\beta_0,\beta_1)}{\delta\beta_0}= \frac{\delta \sum (y_i-\beta_0x_i-\beta_1x_i)^2}{\delta\beta_0} =-2\sum(y_i-\beta_0-\beta_1x_i)\implies n\beta_0=\sum(y_i)-\sum(\beta_1x_i)\] \[\implies \beta_0=\bar{y}-\beta_1\bar{x}\]
\[\frac{\delta RSS(\beta_0,\beta_1)}{\delta\beta_0}= -2\sum(x_iy_i-\beta_0x_i-\beta_1x_i^2)= -2\sum x_iy_i-\bar{y}x_i+\beta_1\bar{x}x_i-\beta_1x_i^2 \implies \beta_1\sum(x_i^2-\bar{x}x_i)=\sum(x_iy_i-x_i\bar{y})\]
\[\implies \beta_1=\frac{\sum(x_iy_i-x_i\bar{y})}{\sum(x_i^2-\bar{x}x_i)} =\frac{\sum(xiyi)-n\bar{x}\bar{y}}{\sum(x_i^2)-n\bar{x}^2} =\frac{\sum(xiyi)-n\bar{x}\bar{y}+n\bar{x}\bar{y}-n\bar{x}\bar{y}}{\sum(x_i^2)-n\bar{x}^2-n\bar{x}^2+n\bar{x}^2} =\frac{\sum(xiyi)-\bar{x}\sum y_i+\sum\bar{x}\bar{y}-\bar{y}\sum x_i}{\sum(x_i^2)-2\bar{x}\sum(x_i)+\sum(\bar{x}^2)}\]
\[=\frac{\sum (xiyi-\bar{x} y_i+\bar{x}\bar{y}-\bar{y} x_i)}{\sum(x_i^2-2\bar{x}x_i+\bar{x}^2)} \implies \beta_1=\frac{\sum_i(x_i-\bar{x})(y_i-\bar{y})}{\sum(x_i-\bar{x})^2}\]
set.seed(5)
X <- runif(50, 0,10)
Y <- 5+X+ rnorm(50, mean=0, sd=1)
my_df <- data.frame(X,Y)
g_train_pts <- my_df %>% ggplot(aes(x = X, y = Y))+
geom_point()+
xlim(c(0,10))+
ylim(c(4,15))
g_train_pts
## Warning: Removed 4 rows containing missing values or values outside the scale range
## (`geom_point()`).
lines<-data.frame(matrix(lm(Y~X,my_df)$coefficients,ncol=2)) # full set
for(i in 1:10){
tempDF<-my_df[sample(nrow(my_df)),][1:20,]#Randomly sort, take first 20
lines<-rbind(lines,data.frame(matrix(lm(Y~X,tempDF)$coefficients,ncol=2)))#Find slope
}
withlines<-g_train_pts+
geom_abline(slope=1,intercept=5,color="red",size=2)+ #trueline
geom_abline(slope=lines[1,2],intercept=lines[1,1],color="blue",size=2) #full set
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
withlines
## Warning: Removed 4 rows containing missing values or values outside the scale range
## (`geom_point()`).
withlines2<-withlines+
geom_abline(data=lines,aes(slope=X2,intercept=X1))#random sample
withlines2
## Warning: Removed 4 rows containing missing values or values outside the scale range
## (`geom_point()`).
The OLS regression estimates will not be exact, but they are generally reasonable (unbiased).
An estimator is unbiased if on average we expect the estimate \(\hat{\mu}=\mu\). If we could average a huge number of estimates of \(\hat{\mu}\) from a huge number of sets then it would exactly equal \(\mu\) (Law of Large Numbers). It does not systematically over or underestimate
The OLS estimates are unbiased.
How good is \(\hat{\mu}\)? Standard error:
\[\text{Var}(\mu)=\text{SE}(\hat{\mu})^2=\frac{\sigma^2}{n}\]
\[\text{SE}(\hat\beta_0)^2=\sigma^2\left[\frac{1}{n}+\frac{\bar{x}^2}{\sum(x_i-\bar{x})^2}\right]\] \[\text{SE}(\hat\beta_1)^2=\frac{\sigma^2}{\sum(x_i-\bar{x})^2}\]
where \(\sigma^2\)=Var(\(\epsilon\)). We assume that the variance is uncorrelated with x and the same for all points (homoskedastic). This is not always true, but tends to be a reasonable approximation. We obviously do not know \(\sigma^2\) but we can estimate it from the data using the residual standard error (strictly speaking, should have a hat, but it is normally dropped.) RSE=\(\sqrt{RSS/(n-2)}\).
95% confidence interval:\(\hat{\beta_1}\pm2\cdot\text{SE}(\hat{\beta_1})\). This is approximate (guassian errors, other assumptions, but it is close enough for this course right now. The 2 should be the 97.5% quantile of a t distribution for n-2 degrees of freedom)
normal test: \(H_0:\) there is no relationship between X and Y \(H_a\): there is a relationship between X and Y
\(H_0:\beta_1=0\)
\(H_a:\beta_1\neq0\)
t statistic:
\[t=\frac{\hat{\beta_1}-0}{SE(\hat{\beta_1})}\]
Basically, how many standard deviations is \(\hat{\beta_1}\) away from 0. If there is no relationship, we expect to follow a t distribution with n-2 degrees of freedom, which is approximately standard normal when n\(\geq 30\).
P value: the probability that \(|t|\) or larger would have been observed assuming \(\beta_1=0\).
Normally we talk about quantitative predictors, but qualitative ones can be used as well.
We have so far assumed that the relationship between predictors and response are additive and linear. We will cover this later in the course, although examples are below:
Identification and fixes are more of an art than a science. Tend to identify with residual plots.
Code taken from Professor Nate Wells.
set.seed(700)
X <- runif(80, 0, 1)
e <- rnorm(80, 0, .25)
Y <- 1 + 2*X + e
my_data <- data.frame(X,Y)
ggplot(my_data, aes(x = X , y = Y)) + geom_point()
Question 1 What is the underlying model? error term?
my_mod<-lm(Y ~ X, data = my_data)
gglm(data = my_mod)
Question 2 For each diagnostic image, what is represented along each axis? Why? What should we look for?
Based on the lab section of ISLR
Import the Boston Dataset from ISLR2
Question 3 Using the ?Boston command, tell me about the dataset
We can fit a linear model in R using the lm function. Syntax: lm(y~x,data=DataFrame)
lm.fit <- lm(lstat ~ age , data = bostonData)
We can then call it directly to get basic information
lm.fit
##
## Call:
## lm(formula = lstat ~ age, data = bostonData)
##
## Coefficients:
## (Intercept) age
## 2.1744 0.1528
Additional functions that are useful:
Question 4 Using the Boston Dataset
Fit a linear model where x=lstat and y=medv
Report the estimates, error, and p values for the two coeficients.
Using geom_point() and geom_abline(), plot the datapoints for lstat and medv, and plot the OLS line.
Plot the residuals using ggplot2
Use gglm to create the diagnostic plots
Based on the plots, do you believe that a linear model is appropriate? Why or why not?
Did you use X or Y for the residual value plots? Why? what happens if you use the other variable? When would you want to use one over the other?
Syntax: I(), allows for transforming variables within lm
Syntax: poly(x,power), creates a polynomial fit. By default, it orthogonalizes the predictors. use raw=TRUE to get the raw higher order terms.
For example:
lm.fit <- lm(lstat ~ age+I(age^2) , data = bostonData)
lm.fit
##
## Call:
## lm(formula = lstat ~ age + I(age^2), data = bostonData)
##
## Coefficients:
## (Intercept) age I(age^2)
## 8.177692 -0.108972 0.002175
lm.fit <- lm(lstat ~ poly(age,2,raw=TRUE) , data = bostonData)
lm.fit
##
## Call:
## lm(formula = lstat ~ poly(age, 2, raw = TRUE), data = bostonData)
##
## Coefficients:
## (Intercept) poly(age, 2, raw = TRUE)1
## 8.177692 -0.108972
## poly(age, 2, raw = TRUE)2
## 0.002175
Question 5
Question 6 Using the code from earlier as a start, explore how linear regressions change (get more/less accurate):
set.seed(5)
X <- runif(50, 0,10)
Y <- 5+X+ rnorm(50, mean=0, sd=1)
my_df <- data.frame(X,Y)
In this lab we practiced the following: