Docsity

Bivariate Data Analysis: Linear Regression and Model Diagnostics, Lecture notes of Statistics

Partial preview of the text

Download Bivariate Data Analysis: Linear Regression and Model Diagnostics and more Lecture notes Statistics in PDF only on Docsity!

Lecture 8: Bivariate Data

Outline

Lecture 8: Bivariate Data Example – Male Olympic 100m sprints Fitting a Simple Linear Model Theory – Least Squares Regression Line for Bivariate Data Model Diagnostic Theory – Pearson’s sample correlation coefficient Theory – Coefficient of Determination Avoiding mistakes in Regression Theory – Inference for Linear Regression Theory – Polynomial Regression

Example – Scatter Plot

olympics = read.csv("../datasets/Olympics100m.csv", header=T) plot(Time ~ Year, data=olympics, xlab="year", ylab="time", main="Olympics 100m 1968-2012")

l

l

l l l

l l l

l l

l l

1970 1980 1990 2000 2010

Olympics 100m 1968−

year

time

Fitting a Model to Bivariate Data

I (^) Bivariate data are of the form: {(xi, yi)} for i = 1, 2 ,... , n. I (^) x = (x 1 , ..., xn)>^ is called the explanatory variable (or independent variable, predictor or regressor) and y = (y 1 , ..., yn)>^ is called the response variable (or dependent variable). These are determined by the context of the data. I (^) We are interested in fitting a model Yi = f (xi) + i, where i is the error, independent of the function f (x). Examples include I (^) Yi = α + (xi + β)^2 + γ + i (quadratic); I (^) Yi = αeβxi^ (exponential); or I (^) Yi = αxβi (allometric). I (^) We will just consider the simple linear model for now: Yi = β 0 + β 1 xi + i. I (^) Note that both the exponential and allometric models can be expressed as a simple linear model by taking logs of each side.

Step 2 – Fit the Least Square’s Regression Line

I (^) If the scatter plot looks linear, then we fit the least square’s regression line. I (^) By ‘eye’, there are many possible lines, which could be drawn on the scatter plot – but which one is optimal?

l

l

l l l

l l l

l l

l l

1970 1980 1990 2000 2010

Olympics 100m 1968−

year

time

Theory – Residuals

I (^) For each candidate line f (x) = βˆ 0 + βˆ 1 x for all βˆ 0 and βˆ 1 , we focus on the resultant set of residuals: ˆi( βˆ 0 , βˆ 1 ) = yi − ( βˆ 0 + βˆ 1 xi). I (^) This is the vertical distance between the line and the observed points. I (^) For example, in the plot below the green residual is − 0. 1581 and the purple residual is 0. 22603.

l

l

l l l

l l l

l l

l l

1970 1980 1990 2000 2010

Olympics 100m 1968−

time

Theory – Least Squares Regression Line for Bivariate Data

I (^) As long as the xi are not all equal, there is a unique solution for the intercept and the slope,

β^ ˆ 0 = ¯y − βˆ 1 ¯x and βˆ 1 = Sxy Sxx where Sxx =

∑^ n i=

x^2 i − (^1) n (

∑^ n i=

xi)^2 =

∑^ n i=

x^2 i − nx¯^2 = (n − 1)s^2 x

Syy =

∑^ n i=

y^2 i − (^1) n (

∑^ n i=

yi)^2 =

∑^ n i=

y i^2 − n¯y^2 = (n − 1)s^2 y

Sxy =

∑^ n i=

xiyi − 1 n (

∑^ n i=

xi)(

∑^ n i=

yi) =

∑^ n i=

xiyi − nx¯y¯

and s^2 x and s^2 y are the (sample) variance of x and y respectively.

Code – Least Squares Regression Line for Bivariate Data

You can fit the least squares regression line in R using the lm() function.

model = lm(Time ~ Year, data=olympics) model$coeff plot(Time ~ Year, data=olympics, xlab="year", ylab="time") abline(model$coeff, col="blue")

l

l

l l l

l l l

l l

l l

1970 1980 1990 2000 2010

year

time

Code – Least Squares Regression Line for Bivariate Data

If you don’t want the intercept, you need to add -1 to the code as below.

model = lm(Time ~ -1 + Year, data=olympics) model$coeff

Year

0.

This gives the fitted model

E(Time) = 0. 005 · Year.

Note: now the slope is positive!

Step 3 – Model Diagnostics

I (^) We can fit a model to the data but is it a good model? I (^) We can use the residuals ˆi to make some inference in our assumption. I (^) Is there some quantitative measure to suggest that there is a strong linear relationship between y and x? I (^) A fit is considered good when the set of data points is close to the fitted line, i.e. the residuals are small.

Example – Residual Plot

residuals = model$res plot(olympics$Year, residuals, main="Residual Plot") abline(h=0, col="red")

l l

l l l

l l l

l l

l l

1970 1980 1990 2000 2010

−0.

Residual Plot

olympics$Year

residuals

Theory – Pearson’s Sample Correlation Coefficient

The Pearson’s sample correlation coefficient is

r =

Sxy √ SxxSyy

In this course we will simply refer this to as correlation coefficient.

Some properties include: I (^) r is symmetric in x and y; I (^) − 1 ≤ r ≤ 1 ; I (^) r is a numerical summary which indicates the strength of the linear association between y and x;

Theory – Pearson’s Sample Correlation Coefficient

I (^) r = 0 indicates no linear correlation, for example a line with zero slope, or a random scatter, or a non linear relationship.

l l l l l l l l l l l l l l l l l l l l

5 10 15 20

−1.

r=

y

l l

l

l l

l

l

l

l

l l

l

l

l l

l

l

l

l

l

5 10 15 20

−1.

r=

y

Examples of Correlation Coefficients

Guess the correlation game