STA2020 Simple Linear Regression
STA2020 Simple Linear Regression
Grace Carmichael
2024
3 Example Problem
4 Correlation Analysis
Statistical Inference
Hypothesis Testing
Example Problem
90
2 10 20
3 38 59
4 27 34
80
5 45 71
6 26 55
70
7 35 50
8 45 78
9 48 81
60
10 20 28
marks
11 30 50
50
12 27 47
13 38 77
40
14 12 18
15 28 41
16 40 79
30
17 38 68
18 47 88
20
19 36 66
20 40 70 10 20 30 40
lectures
Correlation Analysis
Correlation coefficient
y2
y3
x x x
r = −1 r = −0.8 r = 0.08
y4
y5
y6
x x x2
Simple Linear Regression 15 / 51
Correlation Analysis
Example in R 2024-01-29
#-------------------------------------------------------------------------------
# Perform Correlation Analysis on example data using first principals
#-------------------------------------------------------------------------------
x <- lectures
y <- marks
# calculate correlation
(r <- SSxy/sqrt(SSx*SSy))
## [1] 0.9497185
#-------------------------------------------------------------------------------
# Confirm result using base R cor() function
#-------------------------------------------------------------------------------
# cor() just takes the variables as inputs and outputs the correlation
cor(x,y)
## [1] 0.9497185
#-----------------------------------------------------------------------
# Perform hypothesis test on correlation
#-----------------------------------------------------------------------
# using [Link]() which conducts the entire hypothesis test for you
[Link](x,y)
##
## Pearson’s product-moment correlation
##
## data: x and y
## t = 12.869, df = 18, p-value = 1.625e-10
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.8748863 0.9802637
## sample estimates:
## cor
## 0.9497185
Recall the questions we want to answer about our two variables of interest:
Correlation analysis allows us to answer the first two questions but the
limitation of correlation analysis is the inability to quantify the impact of
one variable on another and to predict the value of one variable from the
other.
y = mx + c
Where:
$i = yi → ŷi
So:
n
# n $
# %2
$2i = yi → (βˆ0 + βˆ1 xi )
i=1 i=1
90
80
70
60
marks
ε
50
ε j
40
30
20
10 20 30 40
lectures
90
80
70
60
marks
50
40
30
20
10 20 30 40
lectures
15
20
10
10
y
y
0
5
−10
0
−20
0 5 10 15 20 0 5 10 15 20
x x
Where each line on the plot is the regression line from a different sample.
Simple Linear Regression 31 / 51
Simple Linear Regression
#--------------------------------------------------------------------
# Perform hypothesis test on slope cofficient
#--------------------------------------------------------------------
## [1] 1.622399e-10
##
## Call:
## lm(formula = marks ~ lectures)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.590 -5.215 -0.240 4.348 11.335
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3.6851 5.0333 -0.732 0.474
## lectures 1.8250 0.1418 12.869 1.62e-10 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## Residual standard error: 6.868 on 18 degrees of freedom
## Multiple R-squared: 0.902, Adjusted R-squared: 0.8965
## F-statistic: 165.6 on 1 and 18 DF, p-value: 1.625e-10
## Coefficients:
#---------------------------------------------------------------
# Calculate confidence interval of regression estimates in R
confint(fit)
##
## Call:
## lm(formula = marks ~ lectures)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.590 -5.215 -0.240 4.348 11.335
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3.6851 5.0333 -0.732 0.474
## lectures 1.8250 0.1418 12.869 1.62e-10 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## Residual standard error: 6.868 on 18 degrees of freedom
## Multiple R-squared: 0.902, Adjusted R-squared: 0.8965
## F-statistic: 165.6 on 1 and 18 DF, p-value: 1.625e-10
##
## Call:
## lm(formula = marks ~ lectures)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.590 -5.215 -0.240 4.348 11.335
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3.6851 5.0333 -0.732 0.474
## lectures 1.8250 0.1418 12.869 1.62e-10 ***
## ---
## Signif. codes: 0 ’***’ 0.001 ’**’ 0.01 ’*’ 0.05 ’.’ 0.1 ’ ’ 1
##
## Residual standard error: 6.868 on 18 degrees of freedom
## Multiple R-squared: 0.902, Adjusted R-squared: 0.8965
## F-statistic: 165.6 on 1 and 18 DF, p-value: 1.625e-10
Model Checking
When fitting linear regression models there are some assumptions that we
make about the relationship we are modelling.
1 We assume that the relationship between the dependent and
independent variable is linear.
2 We assume that the errors in our model are normally distributed with
a mean of 0. ($i ∼ N (0, σ 2 )).
3 We assume errors have constant variance (no heteroscedasticity)
4 We assume errors are independent (ie. there is no pattern in the
residuals).
We need to check that the models we fit do not violate any of these
assumptions because if they do then linear regression is not an appropriate
model for the data.
Assessing linearity
To ensure that the relationship we are modelling is linear we can just plot
a scatter plot of x and y and look if it looks linear.
50
2
40
1
Sample Quantiles
30
Frequency
0
20
−1
−2
10
−3
−3 −2 −1 0 1 2 3 0 −3 −2 −1 0 1 2 3
0
−1
−2
−3
6 8 10 12 14
Fitted values
0
−1
−2
−3
−4 −2 0 2 4
50
2
40
Sample Quantiles
Frequency
30
0
20
−1
−2
10
−3
0
−3 −2 −1 0 1 2 3 −3 −2 −1 0 1 2 3
2
1
1
Residuals
Residuals
0
0
−1
−1
−2
−2
−3
−3
6 8 10 12 14 −4 −2 0 2 4
Fitted values x
30
25
10
Sample Quantiles
Frequency
20
5
15
0
10
−5
5
−10
0
−3 −2 −1 0 1 2 3 −10 −5 0 5 10 15
15
10
10
Residuals
Residuals
5
5
0
0
−5
−5
−10
−10
24 26 28 30 32 −4 −2 0 2 4
Fitted values x
Prediction
! = β0 + β1 × lectures
mark
! = →3.6851 + 1.825 × lectures
mark
! = →3.6851 + 1.825 × 35
mark
! = 60.19
mark
So a student who attends 35 lectures can expect a mark of 60.19% for the
course.
Simple Linear Regression 48 / 51
Simple Linear Regression
The CI for the average y will always be narrower than the PI for an
individual y. This is because predicting for an individual is always more
uncertain that predicting the mean.
Simple Linear Regression 49 / 51
Simple Linear Regression
Grace Carmichael
Prediction in R 2024-02-23
#-------------------------------------------------------------------------------
#Predicting for a single individual
#-------------------------------------------------------------------------------
ind1_lectures <- 35
predict(fit, newdata = list(lectures=ind1_lectures), interval = "prediction")
#-------------------------------------------------------------------------------
#Predicting over lecture range
#-------------------------------------------------------------------------------
## 1 2 3 4 5 6 7 8
## 14.56490 16.38990 18.21491 20.03991 21.86492 23.68992 25.51492 27.33993
## 9 10 11 12 13 14 15 16
## 29.16493 30.98994 32.81494 34.63995 36.46495 38.28995 40.11496 41.93996
## 17 18 19 20 21 22 23 24
## 43.76497 45.58997 47.41498 49.23998 51.06498 52.88999 54.71499 56.54000
## 25 26 27 28 29 30 31 32
## 58.36500 60.19001 62.01501 63.84001 65.66502 67.49002 69.31503 71.14003
## 33 34 35 36 37 38 39
## 72.96503 74.79004 76.61504 78.44005 80.26505 82.09006 83.91506
A note on causality