Sei sulla pagina 1di 51

1

Simple Linear Regression


1. review of least squares
procedure
2. inference for least squares lines
2
Introduction
We will examine the relationship between
quantitative variables x and y via a mathematical
equation.
The motivation for using the technique:
Forecast the value of a dependent variable (y) from
the value of independent variables (x
1
, x
2
,x
k
.).
Analyze the specific relationships between the
independent variables and the dependent variable.
3
House size
House
Cost
Most lots sell
for $25,000
The Model
The model has a deterministic and a probabilistic components
4
House size
House
Cost
Most lots sell
for $25,000
However, house cost vary even among same size
houses!
The Model
Since cost behave unpredictably,
we add a random component.
5
The Model
The first order linear model


y = dependent variable
x = independent variable
|
0
= y-intercept
|
1
= slope of the line
c = error variable
c + | + | = x y
1 0
x
y
|
0

Run
Rise
|
1
= Rise/Run
|
0
and |
1
are unknown population
parameters, therefore are estimated
from the data.
6
Estimating the Coefficients
The estimates are determined by
drawing a sample from the population of interest,
calculating sample statistics.
producing a straight line that cuts into the data.
w
w
w
w
w w w w
w
w w
w
w w
w
Question: What should be
considered a good line?
x
y
7
The Least Squares (Regression) Line
A good line is one that minimizes
the sum of squared differences between the
points and the line.
8
The Least Squares (Regression) Line
3
3
w
w
w
w
4 1
1
4
(1,2)
2
2
(2,4)
(3,1.5)
Sum of squared differences = (2 - 1)
2
+ (4 - 2)
2
+ (1.5 - 3)
2
+
(4,3.2)
(3.2 - 4)
2
= 6.89
Sum of squared differences = (2 -2.5)
2
+ (4 - 2.5)
2
+ (1.5 - 2.5)
2
+ (3.2 - 2.5)
2
= 3.99
2.5
Let us compare two lines
The second line is horizontal
The smaller the sum of
squared differences
the better the fit of the
line to the data.
9
The Estimated Coefficients
To calculate the estimates of the slope and
intercept of the least squares line , use the
formulas:
( )( )
( )
1
0 1
2
2 2
( 1)
xy
xx
i i
xy i i
i
xx i x
SS
b
SS
b y b x
x y
SS x y
n
x
SS x n s
n
=
=
=
= =

The regression equation that estimates


the equation of the first order linear model
is:
0 1

y b b x = +
1
y
x
s
b r
s
=
Alternate formula for the slope b
1

10
Example:
A car dealer wants to find
the relationship between
the odometer reading and
the selling price of used cars.
A random sample of 100
cars is selected, and the data
recorded.
Find the regression line.
Car Odometer Price
1 37388 14636
2 44758 14122
3 45833 14016
4 30862 15590
5 31705 15568
6 34010 14718
. . .
. . .
. . .
Independent
variable x
Dependent
variable y
The Simple Linear Regression Line
11
The Simple Linear Regression Line
Solution
Solving by hand: Calculate a number of statistics
; 823 . 822 , 14 y
; 45 . 009 , 36 x
=
=
( )
2
2
43, 528, 690
( ) 2, 712, 511
i
xx i
i i
xy i i
x
SS x
n
x y
SS x y
n
= =
= =

where n = 100.
1
2
0 1
2, 712, 511
.06232
( 1) 43, 528, 690
14,822.82 ( .06232)(36, 009.45) 17, 067
xy
x
SS
b
n s
b y b x

= = =

= = =
x 0623 . 067 , 17 x b b y

1 0
= + =
12
Solution continued
Using the computer
1. Scatterplot
2. Trend function
3. Tools > Data Analysis > Regression
The Simple Linear Regression Line
13
SUMMARY OUTPUT
Regression Statistics
Multiple R 0.8063
R Square 0.6501
Adjusted R Square 0.6466
Standard Error 303.1
Observations 100
ANOVA
df SS MS F Significance F
Regression 1 16734111 16734111 182.11 0.0000
Residual 98 9005450 91892
Total 99 25739561
Coefficients Standard Error t Stat P-value
Intercept 17067 169 100.97 0.0000
Odometer -0.0623 0.0046 -13.49 0.0000
x y 0623 . 067 , 17

=
The Simple Linear Regression Line
14
This is the slope of the line.
For each additional mile on the odometer,
the price decreases by an average of $0.0623
Odometer Line Fit Plot
13000
14000
15000
16000
Odometer
P
r
i
c
e
x y 0623 . 067 , 17

=
Interpreting the Linear Regression -
Equation
The intercept is b
0
= $17067.
0
No data
Do not interpret the intercept as the
Price of cars that have not been driven
17067
15
Error Variable: Required Conditions
The error c is a critical part of the regression model.
Four requirements involving the distribution of c must
be satisfied.
The probability distribution of c is normal.
The mean of c is zero: E(c) = 0.
The standard deviation of c is o
c
for all values of x.
The set of errors associated with different values of y are
all independent.
16
The Normality of c
From the first three assumptions we have:
y is normally distributed with mean
E(y) = |
0
+ |
1
x, and a constant standard
deviation o
c

3

|
0
+ |
1
x
1

|
0
+ |
1
x
2

|
0
+ |
1
x
3

E(y|x
2
)
E(y|x
3
)
x
1
x
2
x
3

1

E(y|x
1
)

2

The standard deviation remains constant,
but the mean value changes with x
17
Assessing the Model
The least squares method will produces a
regression line whether or not there is a linear
relationship between x and y.
Consequently, it is important to assess how well
the linear model fits the data.
Several methods are used to assess the model.
All are based on the sum of squares for errors,
SSE.
18
This is the sum of differences between the points
and the regression line.
It can serve as a measure of how well the line fits the
data. SSE is defined by
. ) y

y ( SSE
n
1 i
2
i i

=
=
Sum of Squares for Errors
2
0 1 i i i i
SSE y b y b x y =

A shortcut formula
19
The mean error is equal to zero.
If o
c
is small the errors tend to be close to zero
(close to the mean error). Then, the model fits the
data well.
Therefore, we can, use o
c
as a measure of the
suitability of using a linear model.
An estimator of o
c
is given by s
c

2
tan

=
n
SSE
s
Estimate of Error dard S
c
Standard Error of Estimate
20
Example:
Calculate the standard error of estimate for the previous
example and describe what it tells you about the model fit.
Solution
9, 005, 450
9, 005, 450
303.13
2 98
SSE
SSE
s
n
c
=
= = =

It is hard to assess the model based


on s
c
even when compared with the
mean value of y.

823 , 14 y 1 . 303 s = =
c
Standard Error of Estimate,
Example
21
Testing the slope
When no linear relationship exists between two
variables, the regression line should be horizontal.
q
q
q
q
q
q
q
q
q
q
q
q
q
Different inputs (x) yield
different outputs (y).
No linear relationship.
Different inputs (x) yield
the same output (y).
The slope is not equal to zero The slope is equal to zero
Linear relationship. Linear relationship. Linear relationship. Linear relationship.
22
We can draw inference about |
1
from b
1
by testing
H
0
: |
1
= 0
H
1
: |
1
= 0 (or < 0,or > 0)
The test statistic is




If the error variable is normally distributed, the statistic
is Student t distribution with d.f. = n-2.
1
b
1 1
s
b
t
|
=
The standard error of b
1
.
1
b
xx
s
s
SS
c
=
where
Testing the Slope
23
Example
Test to determine whether there is enough evidence
to infer that there is a linear relationship between the
car auction price and the odometer reading for all
three-year-old Tauruses in the previous example .
Use o = 5%.
Testing the Slope,
Example
24
Solving by hand
To compute t we need the values of b
1
and s
b1
.









The rejection region is t > t
.025
or t < -t
.025
with v = n-2 = 98.
Approximately, t
.025
= 1.984
49 . 13
00462
0 0623
00462 .
) 690 , 528 , 43 )( 99 (
1 . 303
) 1 (
0623 .
1
1
1 1
2
1
=

=

=
= =

=
=
.
.
s
b
t
s n
s
s
b
b
x
b
|
c
Testing the Slope,
Example
25
Price Odometer SUMMARY OUTPUT
14636 37388
14122 44758 Regression Statistics
14016 45833 Multiple R 0.8063
15590 30862 R Square 0.6501
15568 31705 Adjusted R Square 0.6466
14718 34010 Standard Error 303.1
14470 45854 Observations 100
15690 19057
15072 40149 ANOVA
14802 40237 df SS MS F Significance F
15190 32359 Regression 1 16734111 16734111 182.11 0.0000
14660 43533 Residual 98 9005450 91892
15612 32744 Total 99 25739561
15610 34470
14634 37720 Coefficients Standard Error t Stat P-value
14632 41350 Intercept 17067 169 100.97 0.0000
15740 24469 Odometer -0.0623 0.0046 -13.49 0.0000
Using the computer
There is overwhelming evidence to infer
that the odometer reading affects the
auction selling price.
Testing the Slope,
Example
26
To measure the strength of the linear relationship we
use the coefficient of determination.
2
2
2 2
2
2
( )(
1
( )
i i
x y
i
x x y y
R
s s
SSE
or R
y y
(


=
=

Coefficient of determination
Note that the coefficient of determination is r
2

27
Coefficient of determination
To understand the significance of this coefficient
note:
Overall variability in y
The regression model
The error
28
Coefficient of determination
x
1
x
2

y
1

y
2

y
Two data points (x
1
,y
1
) and (x
2
,y
2
)
of a certain sample are shown.
= +
2
2
2
1
) y y ( ) y y (
2
2
2
1
) y y

( ) y y

( +
2
2 2
2
1 1
) y

y ( ) y

y ( + +
Total variation in y = Variation explained by the
regression line
+ Unexplained variation (error)
Variation in y = SSR + SSE
29
Coefficient of determination
R
2
measures the proportion of the variation in y
that is explained by the variation in x.

=
2
i
2
i
2
i
2
i
2
) y y (
SSR
) y y (
SSE ) y y (
) y y (
SSE
1 R
R
2
takes on any value between zero and one.
R
2
= 1: Perfect match between the line and the data points.
R
2
= 0: There are no linear relationship between x and y.
30
Example
Find the coefficient of determination for the used car
price odometer example.what does this statistic tell you
about the model?
Solution
Solving by hand;
2
2
[ 2,712,511] 2
(43,528,688)(259,996)
2 2
( )(
.6501
i i
x y
x x y y
R
s s


(

= = =

Coefficient of determination,
Example
31
SUMMARY OUTPUT
Regression Statistics
Multiple R 0.8063
R Square 0.6501
Adjusted R Square 0.6466
Standard Error 303.1
Observations 100
ANOVA
df SS MS F Significance F
Regression 1 16734111 16734111 182.11 0.0000
Residual 98 9005450 91892
Total 99 25739561
CoefficientsStandard Error t Stat P-value
Intercept 17067 169 100.97 0.0000
Odometer -0.0623 0.0046 -13.49 0.0000
Using the computer
From the regression output we have

65% of the variation in the auction
selling price is explained by the
variation in odometer reading. The
rest (35%) remains unexplained by
this model.
Coefficient of determination
32
If we are satisfied with how well the model fits
the data, we can use it to predict the values of y.
To make a prediction we use
Point prediction, and
Interval prediction
Using the Regression Equation
Before using the regression model, we need to
assess how well it fits the data.
33
Point Prediction
Example
Predict the selling price of a three-year-old Taurus
with 40,000 miles on the odometer.
It is predicted that a 40,000 miles car would sell for
$14,575.
How close is this prediction to the real price?
575 , 14 ) 000 , 40 ( 0623 . 17067 x 0623 . 17067 y

= = =
A point prediction
34
Interval Estimates
Two intervals can be used to discover how closely the
predicted value will match the true value of y.
Prediction interval predicts y for a given value of x,
Confidence interval estimates the average y for a given x.
The confidence interval
2
2
2
( )
1

( )
g
i
x x
y t s
n
x x
o c

+

The prediction interval


2
2
2
( )
1
1
( )
g
i
x x
y t s
n
x x
o c

+ +

35
Interval Estimates,
Example
Example - continued
Provide an interval estimate for the bidding price on
a Ford Taurus with 40,000 miles on the odometer.
Two types of predictions are required:
A prediction for a specific car
An estimate for the average price per car
36
Interval Estimates,
Example

Solution
A prediction interval provides the price estimate for a
single car:
2
2
2
( )
1

1
( )
g
i
x x
y t s
n
x x
o c

+ +

2
1 (40, 000 36, 009)
[17, 067 .0623(40000)] 1.984(303.1) 1 14, 575 605
100 4, 309, 340, 310

+ + =
t
.025,98
Approximately

37
Solution continued
A confidence interval provides the estimate of the
mean price per car for a Ford Taurus with 40,000
miles reading on the odometer.
The confidence interval (95%) =

+
c o
2
i
2
g
2
) x x (
) x x (
n
1
s t y

2
1 (40, 000 36, 009)
[17, 067 .0623(40000)] 1.984(303.1) 14, 575 70
100 4, 309, 340, 310

+ =
Interval Estimates,
Example
38
As x
g
moves away from x the interval becomes
longer. That is, the shortest interval is found at x.
2
x
2
g
2
s ) 1 n (
) x x (
n
1
s t y

+
c o
x
g 1 0
x b b y

+ =
The effect of the given x
g
on the
length of the interval
39
x
1 x ) 1 x ( = 1 x ) 1 x ( = +
g 1 0
x b b y

+ =
) 1 x x ( y

g
=
) 1 x x ( y

g
+ =
1 x + 1 x
As x
g
moves away from x the interval becomes
longer. That is, the shortest interval is found at x.
The effect of the given x
g
on the
length of the interval
2
x
2
g
2
s ) 1 n (
) x x (
n
1
s t y

+
c o
2
x
2
2
s ) 1 n (
1
n
1
s t y

+
c o
40
x
As x
g
moves away from x the interval becomes longer. That
is, the shortest interval is found at x.

g 1 0
x b b y

+ =
2 x ) 2 x ( = 2 x ) 2 x ( = +
2 x 2 x +
2
x
2
g
2
s ) 1 n (
) x x (
n
1
s t y

+
c o
2
x
2
2
s ) 1 n (
1
n
1
s t y

+
c o
2
x
2
2
s ) 1 n (
2
n
1
s t y

+
c o
The effect of the given x
g
on the
length of the interval
41
Regression Diagnostics - I
The three conditions required for the validity of
the regression analysis are:
the error variable is normally distributed.
the error variance is constant for all values of x.
The errors are independent of each other.
How can we diagnose violations of these
conditions?
42
Residual Analysis
Examining the residuals (or standardized
residuals), help detect violations of the required
conditions.
Example continued:
Nonnormality.
Use Excel to obtain the standardized residual histogram.
Examine the histogram and look for a bell shaped.
diagram with a mean close to zero.
43
For each residual we calculate
the standard deviation as follows:

2
x
2
i
i
i r
s ) 1 n (
) x x (
n
1
h
where h 1 s s
i

+ =
=
c
A Partial list of
Standard residuals
ObservationPredicted PriceResiduals Standard Residuals
1 14736.91 -100.91 -0.33
2 14277.65 -155.65 -0.52
3 14210.66 -194.66 -0.65
4 15143.59 446.41 1.48
5 15091.05 476.95 1.58
Standardized residual i =
Residual i
Standard deviation
Residual Analysis
44
Standardized residuals
0
10
20
30
40
-2 -1 0 1 2 More
It seems the residual are normally distributed with mean zero
Residual Analysis
45
Heteroscedasticity
When the requirement of a constant variance is violated we have
a condition of heteroscedasticity.
Diagnose heteroscedasticity by plotting the residual against the
predicted y.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
The spread increases with y
^
y
^
Residual
^
y
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
46
Homoscedasticity
When the requirement of a constant variance is not
violated we have a condition of homoscedasticity.
Example - continued
-1000
-500
0
500
1000
13500 14000 14500 15000 15500 16000
Predicted Price
R
e
s
i
d
u
a
l
s
47
Non Independence of Error Variables
A time series is constituted if data were collected
over time.
Examining the residuals over time, no pattern should
be observed if the errors are independent.
When a pattern is detected, the errors are said to be
autocorrelated.
Autocorrelation can be detected by graphing the
residuals against time.
48
Patterns in the appearance of the residuals over time indicates
that autocorrelation exists.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Time
Residual
Residual
Time
+
+
+
Note the runs of positive residuals,
replaced by runs of negative residuals
Note the oscillating behavior of the
residuals around zero.
0 0
Non Independence of Error Variables
49
Outliers
An outlier is an observation that is unusually small or large.
Several possibilities need to be investigated when an
outlier is observed:
There was an error in recording the value.
The point does not belong in the sample.
The observation is valid.
Identify outliers from the scatter diagram.
It is customary to suspect an observation is an outlier if its
|standard residual| > 2
50
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
The outlier causes a shift
in the regression line
but, some outliers
may be very influential
+ + + + + + + + + +
An outlier
An influential observation
51
Procedure for Regression Diagnostics
Develop a model that has a theoretical basis.
Gather data for the two variables in the model.
Draw the scatter diagram to determine whether a linear model
appears to be appropriate.
Determine the regression equation.
Check the required conditions for the errors.
Check the existence of outliers and influential observations
Assess the model fit.
If the model fits the data, use the regression equation.