Download Simple Linear Regression and Correlation

Document related concepts

Data assimilation wikipedia , lookup

Instrumental variables estimation wikipedia , lookup

Forecasting wikipedia , lookup

Choice modelling wikipedia , lookup

Regression toward the mean wikipedia , lookup

Regression analysis wikipedia , lookup

Linear regression wikipedia , lookup

Coefficient of determination wikipedia , lookup

Transcript
Simple Linear Regression Review
1. review of scatterplots and
correlation
2. review of least squares
procedure
3. inference for least squares lines
1
Simple Linear Regression Review
1. review of scatterplots and
correlation
2. review of least squares
procedure
3. inference for least squares lines
2
Basic Terminology
• Univariate data: 1 variable is measured on each
sample unit or population unit
e.g. height of each student in a sample
• Bivariate data: 2 variables are measured on
each sample unit or population unit
e.g. height and GPA of each student in a
sample; (caution: data from 2 separate samples
is not bivariate data)
Basic Terminology (cont.)
• Multivariate data: several variables are
measured on each unit in a sample or
population. (later in course)
For each student in a sample of NCSU students,
measure height, GPA, and distance between
NCSU and hometown.
• Focus on bivariate data for now.
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 (x1, x2,…xk.).
– Analyze the specific relationships between the
independent variables and the dependent variable.
5
Scatterplot: Fuel Consumption vs Car
Weight. x=car weight, y=fuel cons.
• (xi, yi): (3.4, 5.5) (3.8, 5.9) (4.1, 6.5) (2.2, 3.3)
(2.6, 3.6) (2.9, 4.6) (2, 2.9) (2.7, 3.6) (1.9, 3.1) (3.4, 4.9)
FUEL CONSUMP. (gal/100
miles)
FUEL CONSUMPTION vs CAR WEIGHT
7
6.5
6
5.5
5
4.5
4
3.5
3
2.5
2
1.5
2.5
3.5
WEIGHT (1000 lbs)
4.5
The correlation coefficient "r"
The correlation coefficient is a measure of the direction and
strength of the linear relationship between 2 quantitative
variables. It is calculated using the mean and the standard
deviation of both the x and y variables.
Correlation can only be used to
describe quantitative variables.
Categorical variables don’t have
means and standard deviations.
Properties (cont.)
r ranges from
-1 to+1
"r" quantifies the strength and
direction of a linear
relationship between 2
quantitative variables.
Strength: how closely the points
follow a straight line.
Direction: is positive when
individuals with higher X values
tend to have higher values of Y.
Properties (cont.) High correlation
does not imply cause and effect
CARROTS: Hidden terror in the produce department at your
neighborhood grocery
• Everyone who ate carrots in 1920, if they are still alive, has
severely wrinkled skin!!!
• Everyone who ate carrots in 1865 is now dead!!!
• 45 of 50 17 yr olds arrested in Raleigh for juvenile
delinquency had eaten carrots in the 2 weeks prior to their
arrest !!!
Properties (cont.) Cause and Effect
• There is a strong positive correlation between the monetary
damage caused by structural fires and the number of firemen
present at the fire. (More firemen-more damage)
• Improper training? Will no firemen present result in the least
amount of damage?
Properties (cont.) Cause and Effect
(1,2) (24,75) (1,0) (18,59) (9,9) (3,7) (5,35) (20,46) (1,0)
(3,2) (22,57)
x = fouls committed by player;
y = points scored by same player
The correlation is due to a third “lurking”
variable – playing time
(x, y) = (fouls, points)
Points
• r measures the strength of
the linear relationship
between x and y; it does
not indicate cause and
effect
• correlation
r = .935
80
70
60
50
40
30
20
10
0
0
5
10
15
Fouls
20
25
30
Simple Linear Regression Review
1. review of scatterplots and
correlation
2. review of least squares
procedure
3. inference for least squares lines
12
The Model
The model has a deterministic and a probabilistic components
House
Cost
Most lots sell
for $25,000
House size
13
The Model
However, house cost vary even among same size
houses!
Since cost behave unpredictably,
House
Cost
we add a random component.
Most lots sell
for $25,000
House size
14
The Model
• The first order linear model
y  b0  b1x  e
y = dependent variable
x = independent variable
b0 = y-intercept
b1 = slope of the line
e = error variable
y
b0 and b1 are unknown population
parameters, therefore are estimated
from the data.
Rise
b0
b1 = Rise/Run
Run
x
15
Estimating the Slope b1 and Intercept b0
• 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.
y
w
Question: What should be
considered a good line?
w
w
w
w
w
w
w
w
w
w
w
w
w
w
x
16
The Least Squares (Regression) Line
A good line is one that minimizes
the sum of squared differences between the
points and the line.
17
The Least Squares (Regression) Line
Sum of squared differences = (2 - 1)2 + (4 - 2)2 +(1.5 - 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
4
3
2.5
2
Let us compare two lines
The second line is horizontal
(2,4)
w
w (4,3.2)
(1,2) w
w (3,1.5)
1
1
2
3
4
The smaller the sum of
squared differences
the better the fit of the
line to the data.
18
The Estimated Coefficients
To calculate the estimates of the slope and
intercept of the least squares line , use the
formulas:
b1  r
sy
sx
b0  y  b1 x
r  correlation coefficient
n
sy 
The least squares prediction equation
that estimates the mean value of y for a
particular value of x is:
(y
i 1
i
ŷ  b0  b1 x
 y )2
n 1
n
sx 
 (x  x )
i 1
2
i
n 1
19
The Simple Linear Regression Line
• 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 37400
14600
2 44800
14100
3 45800
14000
4 30900
15600
5 31700
15600
6 34000
14700
.
.
.
Independent
Dependent
.
.
.
variable
x variable
y
.
.
.
20
The Simple Linear Regression Line
• Solution
– Solving by hand: Calculate a number of statistics
x  36, 011;
sx  6596.125
r  0.80517
y  14,841;
s y  547.74
where n = 100.
b1  r
sy
sx
 0.81517
547.74
 .06769
6596.125
b0  y  b1 x  14,841  (.06769)(36, 011)  17, 286.15
yˆ  b0  b1 x  17, 286.15  .06769 x
21
The Simple Linear Regression Line
• Solution – continued
– Using the computer
1. Scatterplot
2. Trend function
3. Tools > Data Analysis > Regression
22
The Simple Linear Regression Line
Regression Statistics
Multiple R
0.805167979
R Square
0.648295475
Adjusted R
Square
0.644706653
Standard Error
326.4886258
Observations
100
yˆ  17, 248.73  .06686 x
ANOVA
df
Regression
Residual
Total
Intercept
Odometer
SS
19255607.37
10446292.63
29701900
MS
19255607.37
106594.8228
F
180.643
Coefficients
Standard Error
17248.72734
182.0925742
-0.06686089
0.004974639
t Stat
94.72504534
-13.44034928
P-value
3.57E-98
5.75E-24
1
98
99
Significance F
5.75078E-24
Lower 95%
Upper 95%
16887.37056
17610.084
-0.076732895 -0.0569889
23
Interpreting the Linear Regression Equation
17248.73
Odometer Line Fit Plot
Price
16000
15000
14000
0
No data
13000
Odometer
yˆ  17, 248.73  .06686 x
The intercept is b0 = $17248.73.
Do not interpret the intercept as the
“Price of cars that have not been driven”
This is the slope of the line.
For each additional mile on the odometer,
the price decreases by an average of $0.0669
24
Simple Linear Regression Review
1. review of scatterplots and
correlation
2. review of least squares
procedure
3. inference for least squares lines
25
The Model
• The first order linear model
y  b0  b1x  e
y = dependent variable
x = independent variable
b0 = y-intercept
b1 = slope of the line
e = error variable
y
b0 and b1 are unknown population
parameters, therefore are estimated
from the data.
Rise
b0
b1 = Rise/Run
Run
x
26
Error Variable: Required Conditions
• The error e is a critical part of the regression model.
• Four requirements involving the distribution of e must
be satisfied.
–
–
–
–
The probability distribution of e is normal.
The mean of e is zero: E(e) = 0.
The standard deviation of e is se for all values of x.
The set of errors associated with different values of y are
all independent.
27
The Normality of e
E(y|x3)
The standard deviation remains constant,
m3
b0 + b1x3
E(y|x2)
b0 + b1x2
m2
but the mean value changes with x
b0 + b1x1
E(y|x1)
m1
From the first three assumptions we have:
x1
y is normally distributed with mean
E(y) = b0 + b1x, and a constant standard
deviation se
x2
x3
28
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.
29
Sum of Squares for Errors
– 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
n
SSE 

( y i  ŷ i ) 2 .
i 1
– A shortcut formula
SSE   yi2 b0  yi  b1  xi yi
30
Estimate of se, the Standard Deviation
of the Error Term e
– The mean error is equal to zero (recall: the mean of
e is zero: E(e) = 0).
– If se 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 se as a measure of the
suitability of using a linear model.
– An estimator of se is given by se
Estimator of s e
SSE
se 
n2
se is called the standard
error since it is an
estimate of the standard
deviation se
31
Estimate of se, an example
• Example:
– Calculate the standard error se for the previous example and
describe what it tells you about the model fit.
• Solution
SSE  10, 446, 293
SSE
10, 446, 293
se 

 326.49
n2
98
It is hard to assess the model based
on se even when compared with the
mean value of y.
32
se  326.49 y  14,841
Testing the slope
– When no linear relationship exists between two
variables, the regression line should be horizontal.











Linear relationship.
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
33
Testing the Slope
• We can draw inference about b1 from b1 by testing
H0: b1 = 0
H1: b1 = 0 (or < 0,or > 0)
– The test statistic is
b1  b1
t
s b1
The standard error of b1.
where
sb1 
se
n  1 sx
– If the error variable is normally distributed, the statistic
is Student t distribution with d.f. = n-2.
34
Testing the Slope,
Example
• 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 a = 5%.
35
Testing the Slope,
Example
• Solving by hand
– To compute “t” we need the values of b1 and sb1.
b1  .06686
se
326.49
sb1 

 .004975
n  1 sx
99 6596.125
b1  b1 .06686  0
t

 13.44
.
004975
sb1
– The rejection region is t > t.025 or t < -t.025 with df = n-2 = 98,
t.025 = 1.9845
36
Testing the Slope (Example)
• Using the computer
Odometer Price
37400
44800
45800
30900
45900
19100
40100
40200
14600
14100
Regression Statistics
14000 Multiple R
0.805167979
15600 R Square
0.648295475
Adjusted R
15600 Square
0.644706653
Standard
14700 Error
326.4886258
Observation
14500 s
100
15700
15100 ANOVA
14800
df
32400
43500
32700
34500
15200 Regression
14700 Residual
15600 Total
15600
37700
41400
24500
35800
48600
24200
14600
14600 Intercept
15700 Odometer
15000
14700
15400
31700
34000
1
98
99
There is overwhelming evidence to infer
that the odometer reading affects the
auction selling price.
SS
MS
F
19255607.37
10446292.63
29701900
19255607.4
106594.823
180.643
Coefficients
Standard Error
17248.72734
182.0925742
-0.066860885
0.004974639
t Stat
94.7250453
-13.4403493
P-value
3.57E-98
5.75E-24
Significance F
5.75078E-24
Lower 95%
Upper 95%
16887.37056 17610.08
-0.076732895 -0.05699
37
Coefficient of determination
Case I:
Case II:
ignore x: use y to predict y
n
errors:  (obs.  pred.) 2
i 1
n
  ( yi  y )
i 1
 TSS
use x: use yˆ  b0  b1 x
n
errors:  (obs.  pred.) 2
i 1
n
2
2
ˆ
=  ( yi  yi )
i 1
 SSE
Reduction in prediction error when use x:
TSS-SSE = SSR
38
Coefficient of determination
Reduction in prediction error when use x:
TSS-SSE = SSR or TSS = SSR + SSE
The regression model
SSR
Overall variability in y
TSS
The error
SSE
Proportional reduction in prediction error when use x:
TSS  SSE
SSE
 1

TSS
TSS
2


 ( xi  x )( yi  y ) 
2
i 1


algebra =
 r 
2 2
sx s y
n
39
Coefficient of determination: graphically
y2
Two data points (x1,y1) and (x2,y2)
of a certain sample are shown.
y
y1
x1
Total variation in y =
( y1  y ) 2  ( y 2  y ) 2 
Variation in y = SSR + SSE
(TSS)
x2
Variation explained by the + Unexplained variation (error)
regression line
(ŷ1  y) 2  (ŷ 2  y) 2
 (y1  ŷ1 ) 2  (y 2  ŷ 2 ) 2
40
Coefficient of determination
• R2 (=r2 ) measures the proportion of the variation
in y that is explained by the variation in x.
SSE TSS  SSE SSR
R  1


TSS
TSS
TSS
2
• r2 takes on any value between zero and one (-1r 1).
r2 = 1: Perfect match between the line and the data points.
r2 = 0: There is no linear relationship between x and y.
41
Coefficient of determination,
Example
• 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;
r 2  (.80517)2  .6483
42
Coefficient of determination
– Using the computer
From the regression output we have
64.8% of the variation in the auction
selling price is explained by the
variation in odometer reading. The
rest (35.2%) remains unexplained by
this model.
Regression Statistics
Multiple R
0.805167979
R Square
0.648295475
Adjusted R
Square
0.644706653
Standard Error 326.4886258
Observations
100
ANOVA
df
Regression
Residual
Total
Intercept
Odometer
1
98
99
SS
19255607.37
10446292.63
29701900
MS
19255607.37
106594.8228
F
Significance F
180.643
5.75078E-24
Coefficients Standard Error
t Stat
P-value
17248.72734
182.0925742 94.72504534 3.57E-98
-0.06686089
0.004974639 -13.44034928 5.75E-24
43
Using the Regression Equation
• Before using the regression model, we need to
assess how well it fits the data.
• 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
44
Point Prediction
• Example
– Predict the selling price of a three-year-old Taurus
with 40,000 miles on the odometer.
A point prediction
yˆ  17248.73  .06686 x  17248.73  .066686(40, 000)  14,574
– It is predicted that a 40,000 miles car would sell for
$14,574.
– How close is this prediction to the real price?
45
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 prediction interval
yˆ  ta
2
s
SE 2 (b1 )( x  x ) 2  e  se2
n
2
– The confidence interval
yˆ  ta
2
2
s
SE 2 (b1 )( x  x ) 2  e
n
46
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
47
Interval Estimates,
Example
• Solution
– A prediction interval provides the price estimate for a
single car:
yˆ  ta
2
s
SE 2 (b1 )( x  x ) 2  e  se2
n
2
t.025,98
326.492
14,574  1.9845 (.004975)  (40, 000  36, 011) 
 326.492  14,574  652
100
2
2
48
Interval Estimates,
Example
• 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%) =
yˆ  ta
2
2
s
SE 2 (b1 )( x  x ) 2  e
n
326.492
14,574  1.9845 (.004975)  (40, 000  36, 011) 
 14,574  76
100
2
2
49
The effect of the given x on the
length of the interval
– As x moves away from x the interval becomes
longer. That is, the shortest interval is found at x.
se2
ŷ  b0  b1 x yˆ  t
2
2
a 2 SE (b1 )  ( x  x ) 
n
yˆ  ta
2
se2
se2
2
 ( x  x ) 
2
(n  1) sx
n
x
50
The effect of the given x on the
length of the interval
– As x moves away from x the interval becomes longer.
That is, the shortest interval is found at x = x.
ŷ  b0  b1 x
yˆ ( x  x  1)
yˆ ( x  x  1)
yˆ  ta 2 se
1 ( x  x ) 2

n (n  1) sx2
ŷ  t a 2 s e
1
12

n (n  1)s 2x
x 1 x 1
x
( x  1)  x  1 ( x  1)  x  1
51
The effect of the given x on the
length of the interval
– As x moves away from x the interval becomes longer. That
is, the shortest interval is found at x = x.
ŷ  b0  b1 x
x 2
x
x2
( x  2)  x  2 ( x  2)  x  2
yˆ  ta 2 se
1 ( x  x ) 2

n (n  1) sx2
ŷ  t a 2 s e
1
12

n (n  1)s 2x
ŷ  t a 2 s e
1
22

n (n  1)s 2x
52
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?
53
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.
54
Residual Analysis
ObservationPredicted Price Residuals 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
For each residual we calculate
the standard deviation as follows:
A Partial list of
Standard residuals
s ri  s e 1  hi where Standardized residual ‘i’ =
Residual ‘i’
1 ( x i  x)2
hi  
Standard deviation
2
n (n  1)s x
55
Residual Analysis
Standardized residuals
35
30
25
20
15
10
5
0
-2
-1
0
1
2
More
It seems the residual are normally distributed with mean zero
56
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.
+
^y
++
Residual
+ + +
+
+
+
+
+
+
+
+
+
++ +
+
+ +
+
+ +
+ +
+
+ +
+
+
+
The spread increases with ^y
y^
++
+ ++
++
++
+
+
++
+
+
57
Homoscedasticity
Residuals
• When the requirement of a constant variance is not
violated we have a condition of homoscedasticity.
• Example - continued
800
600
400
200
0
13500
-200
-400
-600
-800
-1000
14000
14500
15000
15500
16000
Predicted Price
58
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.
59
Non Independence of Error Variables
Patterns in the appearance of the residuals over time indicates
that autocorrelation exists.
Residual
Residual
+ ++
+
0
+
+
+
+
+
+ +
+
+
+
++
+
+
+
Time
Note the runs of positive residuals,
replaced by runs of negative residuals
+
+
+
0 +
+
+
+
Time
+
+
Note the oscillating behavior of the
residuals around zero.
60
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
61
An outlier
+ +
+
+ +
+ +
+ +
An influential observation
+++++++++++
… but, some outliers
may be very influential
+
+
+
+
+
+
+
The outlier causes a shift
in the regression line
62
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.
63