simple linear regression (slr)

35
Simple linear regression (SLR) Anna Helga Jonsdottir Gunnar Stefansson Sigrun Helga Lund University of Iceland (UI) SLR 1 / 35

Upload: others

Post on 04-May-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Simple linear regression (SLR)

Simple linear regression (SLR)

Anna Helga JonsdottirGunnar StefanssonSigrun Helga Lund

University of Iceland

(UI) SLR 1 / 35

Page 2: Simple linear regression (SLR)

Introduction

The following gives a review of:

Scatter plots

Correlation

Simple linear regression - SLR

Inference in SLR

(UI) SLR 2 / 35

Page 3: Simple linear regression (SLR)

Scatter plot

Scatter plotScatter plots are used to investigate the relationship between two numericalvariables.

The value of one variable is on the y-axis (vertical) and the other on thex-axis (horizontal).

When one of the variable is an explanatory variable and the other one is aresponse variable, the response variable is always on the y-axis and theexplanatory variable on the x-axis.

(UI) SLR 3 / 35

Page 4: Simple linear regression (SLR)

0 2 4 6 8 10

0.00

0.05

0.10

0.15

0.20

Number of beers

Alc

ohol

leve

l in

bloo

d

(5 , 0.10)

(UI) SLR 4 / 35

Page 5: Simple linear regression (SLR)

The equation of a straight line

The equation of a straight lineThe equation of a straight line describes a linear relationship between twovariables, x and y. The equation is written

y = β0 + β1x

where β0 is the intercept of the line on the y-axis and β1 is the slope ofthe line.

(UI) SLR 5 / 35

Page 6: Simple linear regression (SLR)

The straight line

x

y

0 2 4 6 8

04

812

1620

β1: slope

β0: intercept

(UI) SLR 6 / 35

Page 7: Simple linear regression (SLR)

Linear relationship

Linear relationshipWe say that the relationship between two variables is linear if the equationof a straight line can be used to predict which value the response variablewill take based on the value of the explanatory variable.

There can be all sorts of relationship between two variables. For example,the relationship can be described with a parabola, an exponential functionand so on. Those relationship are referred to as nonlinear.

(UI) SLR 7 / 35

Page 8: Simple linear regression (SLR)

Linear/nonlinear relationship

● ●

● ●

●●

●●●

●●

●●

●●

●●

Linear relationship

●●

●●

●●

●●●●

●●

●●

●●●

Linear relationship

●●●

●●●

●●

●●

●●●●

●●

●●

●●●

●●●●

●●

●●

Nonlinear relationship

●●●

●●

●●

●●

●●

●●●●

●●●

●●●

Nonlinear relationship

(UI) SLR 8 / 35

Page 9: Simple linear regression (SLR)

Sample correlation coefficient

Sample coefficient of correlationAssume that we have n measurements on two variables x and y.Denote the mean and the standard deviation of the variable x with x andsx and the mean and the standard deviation of the y variable with y and sy.

The sample coefficient of correlation is

r =1

n− 1

n∑i=1

(xi − xsx

)(yi − ysy

).

Warning: The correlation only estimates the strength of a linearrelationship!

(UI) SLR 9 / 35

Page 10: Simple linear regression (SLR)

Perfect positive relationship

Perfect negative relationship

● ● ● ● ● ● ● ● ● ●

No relationship

●●

●●

●●

●●

●●

●●●●

● ●●

●●

●●

●●

No relationship

● ●

●●

●●

●●

●●

●●

●●

● ●

●●

Strong positive relationship

● ●

●●

●●

Weaker positive relationship

●●

●●

●●

●●

●●

●●

●●

●●

●●

Strong negative relationship

●●

●●

●●

Weaker negative relationship

(UI) SLR 10 / 35

Page 11: Simple linear regression (SLR)

Correlation and causation

Causation is when changes in one variable cause changes in theother variable.There is often strong correlation between two variables although thereis no causal relationship.In many cases, the variables are both influenced by the third variablewhich is then a lurking variable.Therefore, high correlation on its own is never enough to claim thatthere is a causal relationship between two variables!

(UI) SLR 11 / 35

Page 12: Simple linear regression (SLR)

Informal regression

Input: Have data as (x, y)-pairs

Suppose a scatterplot indicates a linear relationship

Loosely: Want to "fit a line" through the data

Next: Evaluate the fit

(UI) SLR 12 / 35

Page 13: Simple linear regression (SLR)

Formal regression

Consider fixed numbers, xi

Random variables: Yi ∼ N(β0 + β1xi, σ2)

or: Yi = β0 + β1xi + εi

where εi ∼ N(0, σ2) is a random error term, independent and identicallydistributed (i.i.d.)

We collect some data on (yi, xi) and use the data to estimate β0 and β1.We can then predict Y using

yi = b0 + b1xi

Thenei = yi − yi

is the ith residual - the difference between the ith observed response andthe prediction.

(UI) SLR 13 / 35

Page 14: Simple linear regression (SLR)

0 1 2 3 4 5

05

1015

20

x

y

(UI) SLR 14 / 35

Page 15: Simple linear regression (SLR)

The linear regression model

The linear regression modelThe simple linear regression model is written

Y = β0 + β1x+ ε

where β0 and β1 are unknown parameters and ε is a normally distributedrandom variable with mean 0 and variance σ2.

The aim of the simple linear regression is first and foremost to estimate theparameters β0 and β1 with the measurements on the two variables,x and Y .

The most common estimation method is through least squares.

(UI) SLR 15 / 35

Page 16: Simple linear regression (SLR)

Which line?

0 2 4 6 8 10

0.00

0.10

0.20

Number of beers

Alc

ohol

leve

l

(UI) SLR 16 / 35

Page 17: Simple linear regression (SLR)

The least squares method

0 2 4 6 8 10

0.00

0.05

0.10

0.15

0.20

Number of beers

Alc

ohol

leve

l

(UI) SLR 17 / 35

Page 18: Simple linear regression (SLR)

The least squares method

Least squares estimation technique minimizes:

S =∑

(yi − (b0 + b1xi))2

Maximum likelihood estimation assumes a probability distribution forthe data and maximizes the corresponding likelihood function.

In the case of normal distributions the two methods results in the sameestimates - we will use least squares.

(UI) SLR 18 / 35

Page 19: Simple linear regression (SLR)

The least squares regression line

Let the mean and standard deviation of the x variable with x and sx andthe y variable with y and sy and their correlation coefficient with r.

Let b0 denote the estimate of β0 and b1 denote the estimate of β1. Thenb0 and b1 are given with the equation

b1 =

∑(x− x)(y − y)∑

(x− x)2= r

sysx

and

b0 = y − b1x.

These are the least squares estimates of the coefficient of a regression linethrough the data points (x, y).

(UI) SLR 19 / 35

Page 20: Simple linear regression (SLR)

Prediction

We often want to use our regression model to predict the outcome of ourresponse variable for some value(s) of the explanatory variable.

PredictionWe can predict the value of Y for some value of x using

yh = b0 + b1 · xh

(UI) SLR 20 / 35

Page 21: Simple linear regression (SLR)

Interpolation

InterpolationIf the regression model is used to predict a value of Y for some value of xwhich is similar to the x-values that were used to estimate the model isreferred to as interpolating.

(UI) SLR 21 / 35

Page 22: Simple linear regression (SLR)

Extrapolation

ExtrapolationExtrapolating is using the regression model to predict a value of Y forsome value of x which is far from the x-values that were used to estimatethe model.

It can be very questionable to extrapolate!

(UI) SLR 22 / 35

Page 23: Simple linear regression (SLR)

Estimating dispersion

A point estimate of σ2, the variance of the y-measurements, is obtainedwith

s2 =

∑i (yi − (b0 + b1xi))

2

n− 2

The predicted value of y at a given x is often denoted by y = b0 + b1x andtherefore

s2 =

∑i(yi − yi)2

n− 2

Commonly σ2 is used in place of s2.

(UI) SLR 23 / 35

Page 24: Simple linear regression (SLR)

Estimating dispersion

We will commonly use the notation

SSE =∑i

(yi − (b0 + b1xi))2

andMSE = SSE/(n− 2)

so s2 = MSE.

(UI) SLR 24 / 35

Page 25: Simple linear regression (SLR)

Correlation and explained variation

Recall the the correlation coeffficient r is always between −1 and 1.

Write SSE =∑

(y− y)2 (sum of squared errors, i.e. error after regression),and SSTOT =

∑(y − y)2 (total sum of squares, i.e. before regression)

The explained variationThe explained variation, often called the coefficient of determination, iscalculated with

R2 = 1− SSESSTOT

Note:

R2 = 1−∑

(y − y)2∑(y − y)2

= . . . = r2

(UI) SLR 25 / 35

Page 26: Simple linear regression (SLR)

SLR in R

It is easy to perform linear regression in R using the lm() function. Forsimple linear regression the syntax is

fit <- lm(x ~ y, data=nameofdataset)

The results can then be looked at using the summary() function

summary(fit)

(UI) SLR 26 / 35

Page 27: Simple linear regression (SLR)

Inference in the linear regression model

Recall that if we have n paired measurements (x1, y1), . . . , (xn, yn), theregression model can be written as

Yi = β0 + β1xi + εi.

β0 is the true intercept (population intercept) that we do not knowthe value of.β1 is the true slope (population slope)εi are the errors where ε ∼ N(0, σ2) and

σ2 =

∑(y − y)2

n− 2

β0 and β1 are therefore parameters, that we both want to estimate andmake inference on.

(UI) SLR 27 / 35

Page 28: Simple linear regression (SLR)

Estimating slope and intercept accuracy

The standard error of the slope is:

σ2b1 =σ2∑

(x− x)2

and the standard error of the intercept is:

σ2b0 =

(1

n+

x2∑ni=1 (xi − x)2

)σ2

where

σ2 =

∑(y − y)2

n− 2

(UI) SLR 28 / 35

Page 29: Simple linear regression (SLR)

Elements of inference in simple linear regression

Basic inference: Test hypotheses and generate confidence intervals for slopeand intercept.

Results on the estimators can be used to make inference on the true slopeand intercept.

The first question raised is whether there is any relationship between the xand y measurements, i.e. whether the slope is zero. This can be phrased asa general hypothesis test for the slope.

Although hypothesis tests are important, they give no information if thehypothesis can not be rejected and hence confidence intervals tend to bemore informative in general.

Both hypothesis tests and confidence intervals can be derived for theintercept as well as the slope, although inference for the intercept tends notto be as commonly used.

(UI) SLR 29 / 35

Page 30: Simple linear regression (SLR)

Testing hypotheses concerning the slope

Hypothesis test forβ1The null hypothesis is:

H0 : β1 = β10

The test statistic is:t =

b1 − β10σβ1

If the null hypothesis is true the test statistic follows the t distribution withn-2 degrees of freedom or t ∼ t(n− 2).

Alternative hypothesis Reject H0 if:H1 : β1 < β10 t < −t1−αH1 : β1 > β10 t > t1−αH1 : β1 6= β10 t < −t1−α/2 or t > tα/2

(UI) SLR 30 / 35

Page 31: Simple linear regression (SLR)

Confidence interval for β1

Confidence interval for β1The lower bound of1− α confidence interval for β1 is:

b1 − t1−α/2,(n−2) · σβ1

The upper bound of 1− α confidence interval is:

b1 + t1−α/2,(n−2) · σβ1

where b1 is calculated the same way as usual, n is the number of pairedmeasurements and t1−α/2,(n−2) is found in the t-distribution table.

(UI) SLR 31 / 35

Page 32: Simple linear regression (SLR)

Confidence interval for β0

Confidence interval for β0The lower bound of a 1− α confidence interval for β0 is:

b0 − t1−α/2,(n−2) · σβ0

The upper bound of 1− α confidence interval is:

b0 + t1−α/2,(n−2) · σβ0

where b0 is calculated the same way as usual, n is the number of pairedmeasurements and t1−α/2,(n−2) is in the table for the t-distribution.

(UI) SLR 32 / 35

Page 33: Simple linear regression (SLR)

Confidence interval for a point on the regression line

Confidence interval for a point on the regression line

The lower bound of 1− α confidence interval for Yh is:

(b0 + b1xh)− t1−α/2,(n−2) · syh

The upper bound of 1− α confidence interval is:

(b0 + b1xh) + t1−α/2,(n−2) · syh

where b0 and b1 are calculated the same way as usual, n is the number ofpaired measurements, t1−α/2,(n−2) is found in the t-distribution table and

syh =

√√√√σ2

(1

n+

(xh − x)2∑j(xj − x)2

)

(UI) SLR 33 / 35

Page 34: Simple linear regression (SLR)

Predicting a new observation

Notice, that as a prediction for a future point, includes two sources ofvariation or error, first due to the measurement errors in the original datathrough variation in the parameter estimates and secondly through thefuture measurement errors at this point.

(UI) SLR 34 / 35

Page 35: Simple linear regression (SLR)

Prediction interval for a new observation

Prediction interval for a new observation

The lower bound of 1− α prediction interval for Yh is:

(b0 + b1xh)− t1−α/2,(n−2) · spred

The upper bound of 1− α prediction interval is:

(b0 + b1xh) + t1−α/2,(n−2) · spred

where b0 and b1 are calculated the same way as usual, n is the number ofpaired measurements, t1−α/2,(n−2) is found in the t-distribution table and

spred =

√√√√σ2

(1 +

1

n+

(xh − x)2∑j(xj − x)2

).

(UI) SLR 35 / 35