Simple Linear Regression
Simple Linear Regression
Regression
1
Part I
Background Review
2
Regression Analysis
Statistical model is a mathematical description of the
data structure/data generating mechanism
Parametric model
Easier to fit, interpret, infer
More powerful (statistically)
Model complexity is fixed
Nonparametric model
No distributional assumption
More flexible
Model complexity may grow
Semiparametric model
3
Regression Analysis
Example: exam scores
Parametric: approximate the class distribution by a normal
distribution with certain parameters (mean and variance)
(hence we can say mean +/- one standard deviation ~ 68%)
Nonparametric: use the histogram
4
Regression Analysis
Regression studies the relationship between
Response/outcome/dependent variables; and
Predictor/explanatory/independent variables
Variables
• Number of children
Discrete • Defects per hour
Quantitative/
Numerical
• Weight
Continuous • Voltage
6
Let’s look at the simplest
case
To study the relationship between two numerical
variables, such as
Exam score vs. Time spent on doing revision
Apartment price vs. Gross floor area
Electricity consumption vs. Air temperature
7
Linear Correlation
Analysis
Scatter plot
8
Linear Correlation
Analysis Cont’d
(Sample) Linear correlation coefficient,
Dimensionless
9
Linear Correlation Analysis Cont’d
t-test for correlation coefficient
Important!! Note the
(no linear correlation) slight abuse of notations
(linear correlation exists) • Upright t denotes the
value of statistic
t-statistic t • denotes the
distribution itself
p-value |t| • denotes its upper tail
denotes a distribution with degree of freedom
quantile (d.f.)
Reject if |t| > or p-value <
10
Example
Is residential apartment price related to its gross floor
area and age of the building?
12
Example • R will not process codes after #, use for comments
#set working directory
setwd(“C:/Users/chiwchu/Google Drive/Academic/CityU/MS3252/Lecture")
14
Example Cont’d
15
Conditional Distribution
Probability/density -> Distribution
Conditional probability/density -> Conditional
distribution
e.g. Let denote the random variable of whether it will
rain tomorrow (1=yes, 0=no)
If the probability of raining tomorrow is 0.4, then has a
Bernoulli(0.4) distribution,
But what if we know whether a typhoon is coming?
Let denote the random variable of whether a typhoon is
coming (1=yes, 0=no)
can be random itself, but we can think of it as fixed
16
Conditional Distribution
Given the information of , the probability of raining
tomorrow and hence the distribution of may change!
Say, conditional probability , then the conditional
distribution of is
Similarly, the conditional distribution of could be
17
Part II
Formulation and Estimation
18
Overview of Regression Analysis
Input
Response / outcome / dependent variable,
The variable we wish to explain or predict
Predictor / covariate / explanatory / independent variable,
The variable used to explain the response variable
Output
A (linear) function that allows us to
Model association: Explain the variation of the response caused
by the predictor(s)
Provide prediction: Estimate the value of the response based on
Regression Line 20
Simple Linear Regression -
Formulation
(Linear) regression model
Assumptions
Linearity of regression equation
is a linear function
Error normality (can be dropped if sample size is large, why?)
has a normal distribution for all
Zero mean of errors (not really an assumption with the intercept)
Error independence
are independent for all
21
Simple Linear Regression -
Formulation
Equivalently, the linear regression model can be written as
(mean function)
(variance function)
are independent and normally distributed
In other words, are independent
denotes a normal distribution with mean and variance
We also call it mean regression model
22
Simple Linear Regression -
Formulation
Framework: we have one response and predictors
here because we only have one
We obtain a random sample of size , containing the values
of and for each individual/subject/observation ,
Our goal is to model/infer about the conditional mean of
given
As the conditional mean is characterized by and , that
means we need to estimate and from the data
23
Simple Linear Regression -
Estimation
Goal: estimate and
Let’s denote these estimates by and
our notations for parameters: Greek alphabets represent
the population/true versions; English alphabets represent
the sample/estimated analogies.
Two methods (turn out to be equivalent for linear
regression):
Least Squares Estimator (LSE)/Ordinary Least Squares (OLS)
Maximum Likelihood Estimator (MLE)
24
Simple Linear Regression -
Estimation
𝑌
𝒀𝒊
𝒆𝒊
^
𝒀 𝒊
^ =𝒃 +𝒃 𝑿
𝒀 𝒊 𝟎 𝟏 𝒊
We are assuming
𝑋 normality of Y for
𝑋𝑖
every level of X
represents the sample intercept
represents the sample slope coefficient
represents the sample residual error 25
Simple Linear Regression -
Estimation
and are estimated using the least squares method,
which minimize the sum of squares errors (SSE)
26
Simple Linear Regression -
Estimation
The solution to and can be obtained by differentiating
with respect to and
That is to solve
and
simultaneously
27
Simple Linear Regression -
Estimation
The solutions are
and
28
Simple Linear Regression -
Estimation
Maximum Likelihood Estimation is to find the parameters
that maximize the likelihood/probability of observing the
sample
Recall that
The density function of is
Assume is known and equals 1 for simplicity…
The joint likelihood/probability of observing these given
these will be
Maximizing this likelihood function is equivalent to
minimizing , which is exactly the SSE, so MLE = LSE!
29
Example Cont’d
𝑏0
𝑏1
𝑆 𝑒= √ 𝑀𝑆𝐸
or
30
Example – The Model &
Interpretation of Coefficients Cont’d
The estimated simple linear regression equation
31
Example – The Model &
Interpretation of Coefficients Cont’d
The estimated simple linear regression equation
32
Example Cont’d
Regress Price against Age
33
Example Cont’d
The relationship between apartment price and age of
the building is
34
Confidence Interval (CI)
Confidence interval estimate for slope coefficient
R program
% CI for
36
Special Case II: Two Groups
Now, the are either 0 or 1 indicating which group the
observation belongs to
The linear regression model assumes that
are independent
are independent
This is equivalent to fitting two normal distributions to the
two groups respectively!!
37
Part III
Goodness of Fit,
Parameter Inference,
and Model Significance
38
Goodness of Fit and Model
Significance
We want to compare the fitted model with against the
null model without
Fitted/Full model = the model you considered
Null model = special case I = a horizontal line at
(Saturated model = data = the model with perfect fit)
Price
GrossFA
42
Example
Which independent variable, GrossFA or Age, provides a
better explanation to the variation of apartment price?
SSR
SSE
=MSE
For SST, use either of
• sum(anova(m1)[,2])
• var(Price)*(length(Price)-1)
43
Inferences about the Parameters –
A -Variable Significance
t-test for a slope coefficient
(no linear relationship)
(linear relationship exists)
t-statistic t
where = standard error of the slope
p-value |t|
has a distribution with d.f.
Reject if |t| > or p-value <
44
Inferences about the Parameters –
A -Variable Significance Cont’d
measures the variation in the slope of regression lines from
different possible samples (one color denotes one sample)
𝒀 𝒀
Small 𝑿 Large 𝑿
variation of the errors around the regression line
45
Inferences about the Parameters –
A -Variable Significance Cont’d
Recall , we can show that !!
Test for is equivalent to the t-test for linear correlation
coefficient
46
Example
Is GrossFA significantly affecting the apartment price?
𝑏1 𝑆𝑏 1
t p-value
d.f. =
47
Example Cont’d
Is GrossFA significantly affecting the apartment price?
In R, use
• qt(.975,78) to obtain C.V.
• 2*(1-pt(10.81,78)) to obtain p-value
t
In exam,
At = • use t-table to obtain C.V.
• p-value is not computable by hand,
d.f. = = but a range can be found at best
C.V. =
At =
d.f. = ) =
C.V. =
F
d.f. =
p-value
SSR MSR
SSE MSE
51
Example Cont’d
Is the model significant?
In R, use
• qf(.95,78) to obtain C.V.
• 1-pf(116.90,78) to obtain p-value
F
In exam,
• use F-table to obtain C.V.
At = • p-value is not computable by hand
d.f. = =
C.V. =
53
Part IV
Prediction and Diagnostics
54
Prediction of New Observations –
Point Prediction
Convert the given -value into the same measurement
scale as the observed -value
As the estimated slope coefficient is scale dependent
Ideally, only use the regression equation to predict the -
value when the given -value is inside the observed data
range
As we are not sure whether the linear relationship will go
beyond the range of observed -value
55
Example Cont’d
What is the estimated price for an apartment with gross floor
area ft2?
Prediction given by the simple linear regression equation
𝒀
^
𝒀 𝒊 Which prediction
^
𝒀 𝒊 we should trust?
^
𝒀 𝒊
𝑿𝒊 𝑿
57
Prediction of New Observations –
Interval Prediction Cont’d
Confidence interval estimate for the mean of -variable
given a -value
58
Prediction of New Observations –
Interval Prediction Cont’d
Prediction interval estimate for an individual -value given
a -value
where
R program
predict(m1,level=.95,interval=“prediction")
It is still a type of confidence interval, although we are using
the term prediction interval to differentiate them
59
Prediction of New Observations –
Interval Prediction Cont’d
𝒀 Prediction Interval for an
individual -value
𝑿𝒊 𝑿
60
Example Cont’d
Determine a % confidence interval for the mean
apartment price for flats of ft2 gross area
Also, construct a % prediction interval for the apartment
price for a flat of ft2 gross area
61
Regression Assumptions
Linearity of regression equation
is a linear function
Error normality
has a normal distribution for all
Constant variances of errors
Error independence
are independent for all
62
Residual Analysis
Check the regression assumptions by examining the
residuals
Residuals (or errors),
Plot
Residuals against the predictor for checking linearity and
constant variances
Residuals against index for checking error independence
Histogram of the residuals for examining error normality
63
Residual Analysis
e Cont’d
0
e
𝑿
Residuals has a systematic
0 pattern, and -variables are not
having a liner relationship, but a
𝑿 e curved one
e e
0 0
Index(Time) Index(Time)
Residuals displaying a
random pattern
Negative residuals are
associated mainly with the
early trials, and positive
residuals with the later trials,
time of the data being
collected affects the residuals
and -values
65
Residual Analysis Cont’d
35 35
30 30
25 25
20 20
%
%
15 15
10 10
5 5
0 0
-0.75 -0.5 -0.25 0 0.25 0.5 0.75 -0.75 -0.5 -0.25 0 0.25 0.5 0.75
e e
Residuals follow a
symmetrical and bell shape
Residuals are being right-
skewed
distribution
66
Summary
Description Response Predictor Correlation Error
Population Version
Sample Analogy
Variance of Estimator
(take square root to get
standard error)
67
Summary
is the breakdown of variance / variations
= a single number in that quantifies the model explained
variation / measures the goodness of fit
t-statistic t tests the significance of a single predictor, i.e.
whether
F-statistic F tests the significance of the entire model, i.e.
whether all , is the number of predictors
In this chapter with a single , and
Point prediction and confidence interval prediction
68