联系我们: 手动添加方式: 微信>添加朋友>企业微信联系人>13262280223 或者 QQ: 1483266981
Noam Yuchtman
July 20, 2023
MG107: Management and Analytics in
the Age of Big Data
Lecture 9: Linear patterns and
simple regression
Course overview
Part 5: Building regression models
Lectures 9-11
You want to build a model to predict and
explain outcomes from data you have
What’s the best model you can build
Today: constructing and interpreting the OLS
regression line and inference in regression
Outline
Constructing the OLS regression line
Interpreting and using the OLS model
The importance of the residuals
Inference in regression models
Outline
Constructing the OLS regression line
Interpreting and using the OLS model
The importance of the residuals
Inference in regression models
A bit more on the big picture . . .
Why regression models
Average effects of x on y often important for
decision-making in a range of organizations
We usually don’t have data that allow for a
specific comparison or a specific estimate of a
parameter
A general model can provide information about
these comparisons and estimates in the absence
of data we’d need to run the tests from earlier in
the course
Pick a job, I’ll give you a regression
Consultants: reg price competition
Pick a job, I’ll give you a regression
HR: reg performance interview_score
Pick a job, I’ll give you a regression
Finance: reg share_price quarter_sales
Pick a job, I’ll give you a regression
Education: reg test_scores spending
Fitting a line to data
Diamonds example
You have a sample of several hundred diamonds
Based on this evidence, you’d like to know:
1. What is the general relationship between price and
weight
2. What is the predicted price of a diamond that weighs
0.4 carat
3. How much more do diamonds that weigh 0.5 carat cost
Fitting a line to data
Diamonds example
To answer those questions, you want to build a linear
model that links the weight of a diamond to price
Does a linear model make sense
How do you build the model
Scatterplot of price against weight
Linear association is clear (correlation, r = 0.66)
What makes a good model
Many different lines can be drawn through the cloud of
data
You want to estimate a relationship between weight and
price that predicts price in the following way:
1. The prediction isn’t systematically wrong in one direction
2. Distance between prediction and actual price is small
The Ordinary Least Squares regression
The most important data analysis tool there is!
Any linear model will look like the following:
Estimated Price = b0 + b1 Weight
Why Because this is just the equation for a line . . .
The question is how to choose b0 and b1 to make the model work well
Model will make mistakes: residuals
What’s going on here
The Ordinary Least Squares regression
Again, the linear model has to be:
Estimated Price = b0 + b1 Weight
We want our models to have a “good fit” to the data; what choice of b0
and b1 will minimize the squared residuals (“least squares”)
Ordinary least squares estimates
Deriving these takes a bit of work . . . but
here they are for you to use:
x
y
s
r
s b =1
x y b b 1 0 =
So, you need to know the correlation between the variables
of interest and their means and standard deviations
Let’s look at some data
Price ($) Weight (carats)
Mean 1146.7 Mean 0.41
Standard Error 12.58 Standard Error 0.003
Median 1111.5 Median 0.41
Mode 1071 Mode 0.45
Standard
Deviation 225.04
Standard
Deviation 0.055
Sample Variance 50644.42 Sample Variance 0.003
Kurtosis -0.38 Kurtosis -1.026
Skewness 0.50 Skewness -0.18
Range 1133 Range 0.2
Minimum 714 Minimum 0.3
Maximum 1847 Maximum 0.5
Sum 366937 Sum 132.23
Count 320 Count 320
Price ($) Weight (carats)
Price ($) 1
Weight (carats) 0.655518324 1
Correlation table:
Summary stats:
The mechanics
So, the least squares regression equation relating diamond prices to
weight is:
Estimated Price = 43 + 2670 Weight
2670
0.055256
0.6555 225.043
1 = = =
x
y
s
r
s b
1146.7 -(2670)(0.41) 43 1 0 = = = x y b b
Outline
Constructing the OLS regression line
Interpreting and using the OLS model
The importance of the residuals
Inference in regression models
What does the OLS line tell you
Interpreting the y-intercept
What’s the price of a diamond weighing 0 carats In this case, b0 is a major
extrapolation
What does the OLS line tell you
Interpreting the slope coefficient
– The slope is the predicted change in the outcome
when the explanatory variable changes by 1 unit
(“change in y over change in x”)
In this case, a one carat change in diamond weight is
predicted to increase price by $2,670
– To find the effects of changes other than 1 unit, just
multiply b1 by the proper fraction/multiple
In this case, a .2 carat change in diamond weight is
predicted to increase price by 0.2*2670 = $534
For those who learn better
graphically
Using the model to estimate price
What is the estimated price of a diamond that weighs 0.4 carat
Using the model to estimate price
What is the estimated price of diamonds that weigh 0.4 carat
Estimated Price = 43 + 2670 Weight
Now just plug in 0.4 for the weight:
Estimated Price{.4 carats} = 43 + 2670 (0.4) = $1,111
Using the slope to estimate
differences
What is the estimated price difference between .4 carat diamonds
and .5 carat diamonds
Using the slope to estimate
differences
What is the estimated price difference between .4 carat diamonds
and .5 carat diamonds
Estimated Price{.4 carats} = 43 + 2670 (0.4) = $1,111
Estimated Price{.5 carats} = 43 + 2670 (0.5) = $1,378
$267 more
(also equals b1 times the weight difference of 0.1)
Outline
Constructing the OLS regression line
Interpreting and using the OLS model
The importance of the residuals
Inference in regression models
Is the linear model appropriate
Checking residuals
If the least squares model is appropriate then a plot of
residuals against x should stretch out horizontally with
“consistent vertical scatter”
Can use a visual test for association to check for the
absence of a pattern
Residual plot for diamonds example
Standard error of residuals
Also known as the standard deviation of the
residuals (se):
Measures how much the residuals tend to vary around the fitted
line
For the diamond example, se = $169
Tells you how much your estimates are likely to miss by
You can see in the previous graph that it is common to miss by a
couple hundred dollars
Explaining variation: R-squared (r2)
The residuals are what your model fails to explain
What your model does explain is measured by the r2 – the square of the
correlation between x and y
The fraction of the variation in y accounted for by the least squares
regression line
For the diamond example, r2 = 0.434
Thus, the fitted line explains 43.4% of the variation in price
NOTE: r2 alone does not tell you whether you have a good model
Causality
Is the linear fit appropriate
Outline
Constructing the OLS regression line
Interpreting and using the OLS model
The importance of the residuals
Inference in regression models
What’s new when we are making
inferences with regression
§ Instead of just describing what we see in our data, we
now consider the data (and the estimated OLS line) to be
a sample from a population
§ Had we drawn a different sample, how different would the
results be
§ Apply our tools to make inference about the
population relationships
The population and sample models
Parameters and estimates for SRM:
key terms and symbols
The simple regression model
Population relationship is linear on average
The equation of the SRM describes how the conditional mean
of y depends on x in the population
The SRM shows that these means lie on a line with intercept
β0 and slope β1:
μy|x = E(Y | X = x) = β0 + β1 x
The simple regression model
Deviations from the Mean
The model recognizes that individual responses will differ from
their conditional means (because other variables matter, too!):
y = β0 + β1 x + ε
The deviations of responses around μy|x are called errors
Errors are denoted ε, and E(ε) = 0
Assumptions about the error term
The SRM makes three assumptions about ε:
1. The errors are independent of each other
2. The errors are normally distributed
3. The errors all have the same variance, Var(ε) = σε
2
The point of all of this is to be able to say something
about the population relationship from one sample
Conditions for the SRM
§ Is the association between y and x linear
§ Are the errors evidently independent
§ Are the variances of the residuals similar
§ Are the residuals nearly normal
§ NOTE: we check these using our sample
§ For causal inference: have omitted variables been ruled
out
Locating a BP station
It is 2027 and you are working for BP (corporate!)
You are put in charge of determining the location for a
new petrol station
You are considering a variety of different possible
locations with different traffic levels
What is the relationship between traffic volume and
sales
Using the regression model for
inference: consulting for BP
You regress petrol sales on traffic levels
The slope b1 describes the average increase in sales for a given increase in traffic levels
But how strong is this relationship
You would like, in addition, to report a 95% confidence
interval for the estimated slope
Finally, you would like to report a 95% prediction interval for
the level of sales for a given station with 50,000 daily drive_xfffe_bys
Using the regression model for
inference
You have sales data from a recent month obtained from
80 BP stations
For each location you also have a measure of
average traffic level
What is the regression model of interest What
association do you care about
Look at your data first
Scatterplot
Look at your data first
Residual plot
Are errors “nearly normal”
Histogram of residuals
Residuals
Mean 2.94209E-15
Standard
Error 0.167240954
Median 0.014368073
Standard
Deviation 1.495848563
Sample
Variance 2.237562924
Kurtosis 0.0384488
Skewness 0.09670843
Range 7.151048252
Minimum -3.332929184
Maximum 3.818119068
Sum 2.35367E-13
Count 80
-4 -2 0 2 4
Residuals
0 .1 .2 .3 .4
Density
Fit the OLS line to the data
Inference in regression
We fit the OLS line to the data; now what
Describe the sample-to-sample variability of b1
The estimated standard error of b1 is:
Let’s visualise …
D
x
e
s n
se b s 1
1 ( 1) × =
Regression output
Inference in regression: hypothesis
tests
Hypothesis tests
To test H0: β1 = 0, use
The t distribution in this case has n-2 degrees of freedom
What exactly are we doing here Why are we doing this
t =
b1 0
se(b1)
Inference in regression: the BP data
§ The t-statistic for the slope is,
§ With p-value of < 0.0001 indicates that the slope is significantly
different from zero
§ NOTE 1: Make sure you can find the p-value in the t-table!
§ NOTE 2: These are two-sided tests!
0.0243
9.74 0.2367
( 1)
1
=
se b
= =
b
t
Inference in regression: confidence
intervals
Confidence intervals
The 95% confidence interval for β1 is:
b1 ± t0.05/2,n-2 se(b1)
Inference in regression: confidence
intervals
The 95% confidence interval for β1 is b1 ± t0.05/2,n-2 se(b1),
.2367 ± (1.992)(.0243)
[.188, .285]
Inference in regression: prediction
intervals
§ A prediction interval is an interval designed to hold a
fraction (usually 95%) of the values of the response
(outcome) for a given value of x
§ Different from a confidence interval!
§ It makes a statement about the location of a new
observation, rather than a parameter of a population
Inference in regression: prediction
intervals
§ A simple approximation for a 95% prediction interval is,
y
± 2se
§ Prediction intervals are reliable within the range of
observed data – don’t extrapolate too much!
§ They are also sensitive to the assumptions of constant
variance and normality
Prediction intervals: what they look
like, and why assumptions matter
Back to diamonds for a slide
Prediction interval for BP
What is the 95% prediction interval for the level of sales
for a station with 50,000 daily drive-bys
The predicted sales for x=50,000 is,
-1.338 + (0.2367) (50) = 10.5, or 10,500 gallons
A simple approximation for a 95% prediction interval is, y ± 2se
(From the regression output we know that se = 1.5)
Thus the 95% prediction interval is 10.5 ± (2)(1.5) or approximately
7,500 to 13,500 gallons
Take-aways
Linear regression among the most widely-used tools to model
relationships between variables
The OLS model can be used to describe a sample of data
It can be used for prediction about outcomes for observations we
have not yet observed
It can be used to make statistical inferences about a population
that we do not observe
Importantly, regression by itself cannot make any guarantees about
causal inference – this is up to us to think about carefully


发表回复