Part 4 - Simple Linear Regression

Introduction

The main objective of many statistical investigations is to make predictions.

For instance, an engineer may wish to predict the amount of oxide that will form on the surface of a metal baked in an oven for one hour at 200^{\circ}C, or the amount of deformation of a ring subjected to a certain compressive force.

Usually, such predictions require a formula which relates the dependent variable whose value we want to predict (or called response) to one or more other variables, usually called predictors (or regressors).

The collection of statistical tools for this is called regression analysis.

Simple Linear Regression

The simplest form of regression analysis is simple linear regression, which involves a single predictor and a single response variable.

Y=β0+β1X+eY = \beta_0 + \beta_1 X + e

where:

  • YY is the response variable.
  • XX is the predictor variable.
  • β0\beta_0 is the intercept.
  • β1\beta_1 is the slope.

Assume that E(e)=0\mathbb{E}(e) = 0 and Var(e)=σ2\text{Var}(e) = \sigma^2.

Suppose we fix X=xX = x, meaning

Y=β0+β1x+e,Y = \beta_0 + \beta_1 x + e,

with mean E(Y)=β0+β1x\mathbb{E}(Y) = \beta_0 + \beta_1 x and variance Var(Y)=σ2\text{Var}(Y) = \sigma^2 (YY is normal if ee is normal).

The linear function β0+β1x\beta_0 + \beta_1 x is thus the function giving the mean value of YY for each possible value of XX.

It is called the regression function (or regression line) and will be denoted as,

μYX=x=E(YX=x)=β0+β1x.\mu_{Y | X = x} = \mathbb{E}(Y | X = x) = \beta_0 + \beta_1 x.

The slope β1\beta_1 is the change in mean of YY for one unit change in XX, the intercept β0\beta_0 is the mean value of YY when X=0X = 0.

Least Squares Estimator

Gauss proposed estimating β0\beta_0 and β1\beta_1 to minimize the sum of the squares of the vertical deviations between the observed responses and the fitted line.

These deviations are called the residuals.

The residuals are the vertical distances between the data points and the fitted line.
The residuals are the vertical distances between the data points and the fitted line.

For any “candidate” straight line Y=a+bXY = a + bX, we can write R(a,b)=i=1N(Yi(a+bXi))2R(a, b) = \sum_{i = 1}^N (Y_i - (a + bX_i))^2.

The minimizers are (β0^,β1^)(\hat{\beta_0}, \hat{\beta_1}), with

β1^=iXiYi(iXi)(iYi)niXi2(iXi)2n\hat{\beta_1}= \frac{\sum_i X_i Y_i - \frac{\left(\sum_i X_i\right) \left(\sum_i Y_i\right)}{n}}{\sum_i X_i^2 - \frac{\left(\sum_i X_i\right)^2}{n}}

and

β0^=Yˉβ^1Xˉ,\hat{\beta_0}= \bar{Y} - \hat{\beta}_1 \bar{X},

where Xˉ=1niXi\bar{X} = \frac{1}{n} \sum_i X_i and Yˉ=1niYi\bar{Y} = \frac{1}{n} \sum_i Y_i.

We can clean up this by defining the following quantities:

SXX=i(XiXˉ)2(=iXi2(iXi)2n)SXY=i(XiXˉ)(YiYˉ)(=iXiYi(iXi)(iYi)n)\begin{align*} S_{XX} = \sum_i (X_i - \bar{X})^2 \left(= \sum_i X_i^2 - \frac{\left(\sum_i X_i\right)^2}{n}\right) \newline S_{XY} = \sum_i (X_i - \bar{X})(Y_i - \bar{Y}) \left(= \sum_i X_i Y_i - \frac{\left(\sum_i X_i\right) \left(\sum_i Y_i\right)}{n}\right) \newline \end{align*}

Thus, we have,

β1^=SXYSXX\hat{\beta_1}= \frac{S_{XY}}{S_{XX}}

and

β0^=Yˉβ^1Xˉ.\hat{\beta_0}= \bar{Y} - \hat{\beta}_1 \bar{X}.

The variance σ2\sigma^2 of the error term e=Y(β0+β1X)e = Y - (\beta_0 + \beta_1 X) is another unknown parameter.

The residuals,

ei^=yi(β0^+β1^xi)=yiy^i,i=1,,n,\hat{e_i} = y_i - (\hat{\beta_0} + \hat{\beta_1} x_i) = y_i - \hat{y}_i, \quad i = 1, \ldots, n,

can be regarded as a ‘sample’

First, it can be checked that,

eˉ=1nie^i=yˉβ^0β^1xˉ=0.\bar{e} = \frac{1}{n} \sum_i \hat{e}_i = \bar{y} - \hat{\beta}_0 - \hat{\beta}_1 \bar{x} = 0.

(by definition of the estimated coefficient β^0\hat{\beta}_0 and β^1\hat{\beta}_1).

Therefore, an unbiased estimate of σ2\sigma^2 is,

s2=1n2ie^i2.s^2 = \frac{1}{n - 2} \sum_i \hat{e}_i^2.

Inferences

From now on we will assume that the value of the predictors are deterministic (or we condition on X).

We note that Yi(Xi=xi)N(β0+β1xi,σ2)Y_i | (X_i = x_i) \sim N(\beta_0 + \beta_1 x_i, \sigma^2).

Then, because i(xixˉ)=0\sum_i (x_i - \bar{x}) = 0, we can write,

β1^=SxYsxx=i(xixˉ)sxxYi.\hat{\beta_1} = \frac{S_{xY}}{s_{xx}} = \frac{\sum_i (x_i - \bar{x})}{s_{xx}} Y_i.

The sampling distribution of β1^\hat{\beta_1} is normal with mean β1\beta_1 and variance σ2/sxx\sigma^2 / s_{xx}.

Now we can write,

β0^=iYinβ1^xˉ,\hat{\beta_0} = \sum_i \frac{Y_i}{n} - \hat{\beta_1} \bar{x},

which is again a linear combination of normal random variables (the YiY_i’s and β^1\hat{\beta}_1).

Thus, the estimator of β0\beta_0 is also normally distributed.

The sampling distribution of β0^\hat{\beta_0} is normal with mean β0\beta_0 and variance σ2(1n+xˉ2sxx)\sigma^2 \left(\frac{1}{n} + \frac{\bar{x}^2}{s_{xx}}\right).

Confidence Interval

If we have the hypothesis,

H0:β1=0vs.H1:β10,H_0: \beta_1 = 0 \quad \text{vs.} \quad H_1: \beta_1 \neq 0,

We have sxxβ^1β1σN(0,1)\sqrt{s_{xx}} \frac{\hat{\beta}_1 - \beta_1}{\sigma} \sim N(0, 1).

Replacing σ\sigma with its estimator SS, we find,

β1^β1S/sxxtn2.\frac{\hat{\beta_1} - \beta_1}{S / \sqrt{s_{xx}}} \sim t_{n - 2}.

The rejection region is then,

reject H0 if β1^[tn2,1α/2ssxx,tn2,1α/2ssxx].\text{reject } H_0 \text{ if } \hat{\beta_1} \notin \left[-t_{n - 2, 1 - \alpha / 2} \frac{s}{\sqrt{s_{xx}}}, t_{n - 2, 1 - \alpha / 2} \frac{s}{\sqrt{s_{xx}}}\right].

The pp-value is,

p=1P(T[t0,t0])=2P(T>t0).p = 1 - P(T \in [-|t_0|, |t_0|]) = 2P(T > |t_0|).

where t0=β1^s/sxxt_0 = \frac{\hat{\beta_1}}{s / \sqrt{s_{xx}}} and TT is a random variable with distribution tn2t_{n - 2}.

So, the CI for β1\beta_1 is,

[β1^tn2,1α/2ssxx,β1^+tn2,1α/2ssxx].\left[\hat{\beta_1} - t_{n - 2, 1 - \alpha / 2} \frac{s}{\sqrt{s_{xx}}}, \hat{\beta_1} + t_{n - 2, 1 - \alpha / 2} \frac{s}{\sqrt{s_{xx}}}\right].
Confidence Interval on the Mean Response

For a specified value of XX, say, xx, we want a confidence interval for,

y(x)=μYX=x=β0+β1x.y(x) = \mu_{Y | X = x} = \beta_0 + \beta_1 x.

we have an estimator,

y^(x)=μ^YX=x=β^0+β^1x.\hat{y}(x) = \hat{\mu}_{Y | X = x} = \hat{\beta}_0 + \hat{\beta}_1 x.

and

y^(x)N(μYX=x,σ2(1n+(xxˉ)2sxx)).\hat{y}(x) \sim N\left(\mu_{Y | X = x}, \sigma^2 \left(\frac{1}{n} + \frac{(x - \bar{x})^2}{s_{xx}}\right)\right).

Replacing the unkown σ\sigma by its estimator SS, we get,

μYX=x^μYX=xS1n+(xxˉ)2sxxtn2.\frac{\hat{\mu_{Y | X = x}} - \mu_{Y | X = x}}{S \sqrt{\frac{1}{n} + \frac{(x - \bar{x})^2}{s_{xx}}} } \sim t_{n - 2}.

and the CI is,

[μYX=x^tn2,1α/2S1n+(xxˉ)2sxx,μYX=x^+tn2,1α/2S1n+(xxˉ)2sxx].\left[\hat{\mu_{Y | X = x}} - t_{n - 2, 1 - \alpha / 2} S \sqrt{\frac{1}{n} + \frac{(x - \bar{x})^2}{s_{xx}}}, \hat{\mu_{Y | X = x}} + t_{n - 2, 1 - \alpha / 2} S \sqrt{\frac{1}{n} + \frac{(x - \bar{x})^2}{s_{xx}}}\right].

Prediction of new observations

An important application is predicting new or future observations YY corresponding to a specified level X=xX = x.

This is different to estimating the mean response.

μYX=x\mu_{Y | X = x} at X=xX = x, a predicted value is y^(x)=β0+β1x\hat{y}(x) = \beta_0 + \beta_1 x.

We can derive,

Y(X=x)y(x)^N(0,σ2(1+1n+(xxˉ)2sxx)),Y | (X = x) - \hat{y(x)} \sim N(0, \sigma^2 \left(1 + \frac{1}{n} + \frac{(x - \bar{x})^2}{s_{xx}}\right)),

which leads to the following prediction interval,

[y^(x)tn2,1α/2S1+1n+(xxˉ)2sxx,y^(x)+tn2,1α/2S1+1n+(xxˉ)2sxx].\left[\hat{y}(x) - t_{n - 2, 1 - \alpha / 2} S \sqrt{1 + \frac{1}{n} + \frac{(x - \bar{x})^2}{s_{xx}}}, \hat{y}(x) + t_{n - 2, 1 - \alpha / 2} S \sqrt{1 + \frac{1}{n} + \frac{(x - \bar{x})^2}{s_{xx}}}\right].

Remarks

  1. A prediction interval for YY at X=xX = x will always be longer than the CI for μYX=x\mu_{Y | X = x} because there is much more variability in one observation than in an average.

  2. As nn gets larger (nn \to \infty), the width of CI for μYX=x\mu_{Y | X = x} decreases to 0, buy this is not the case for the prediction interval.

Analysis of Variance (ANOVA)

The most interesting test for simple linear regression is probably,

H0:β1=0vs.H1:β10.H_0 : \beta_1 = 0 \quad \text{vs.} \quad H_1 : \beta_1 \neq 0.

We previously used tt-test,

β1^s/sxxtn2.\frac{\hat{\beta_1}}{s / \sqrt{s_{xx}}} \sim t_{n - 2}.

There is an alternative (and equivalent) test, the FF-test based on analysis of variance.

Firstly, let’s define some quantities,

SST=i(yiyˉ)2SSreg=i(y^iyˉ)2RSS=i(yiy^i)2=ie^i2\begin{align*} SST = \sum_i (y_i - \bar{y})^2 \newline SS_{\text{reg}} = \sum_i (\hat{y}_i - \bar{y})^2 \newline RSS = \sum_i (y_i - \hat{y}_i)^2 = \sum_i \hat{e}_i^2 \end{align*}

where SSTSST is the total sum of squares, SSregSS_{\text{reg}} is the regression sum of squares, and RSSRSS is the residual sum of squares.

For the FF-test, we use,

F=SSreg/1RSS/(n2)F(1,n2) under H0.F = \frac{SS_{\text{reg}} / 1}{RSS / (n - 2)} \sim F(1, n - 2) \text{ under } H_0.

We reject H0H_0 if F>F1α,1,n2F > F_{1 - \alpha, 1, n - 2}.

Analysis of Variance Table

The ANOVA table is a table that shows the sources of variation in an experiment.

Table 1: Analysis of variance for simple linear regression.
Source of Variation Degrees of Freedom (df) Sum of Squares (SS) Mean Square (MS) F-statistic
Regression 1 SSregSS_{\text{reg}} SSreg/1SS_{\text{reg}}/1 F=MSreg/1RSS/(n2)F = \frac{MS_{\text{reg}}/1}{RSS/(n - 2)}
Residual n2n - 2 RSSRSS RSS/(n2)RSS/(n - 2)
Total n1n - 1 SSTSST

Coefficient of Determination

Suppose SSTSSregSST \simeq SS_{\text{reg}} and RSS0RSS \simeq 0.

Almost the whole variation is due to error, and the regression is not significant.

The coefficient of determination is defined as,

R2=SSregSST,R^2 = \frac{SS_{\text{reg}}}{SST},

which represents the proportion of the variability in the responses that is explained by the predictor.

A value of R2R^2 near 1 indicates a good fit to the data.

A value of R2R^2 near 0 indicates a poor fit to the data.

Connection to correlation coefficient

We can derive the following relationship,

R2=SSregSST=SSTRSSsyy=sxx(SSTRSS)sxxsyy=sxy2sxxsyy=(i(xixˉ)(yiyˉ))2i(xixˉ)2i(yiyˉ)2.\begin{align*} R^2 & = \frac{SS_{\text{reg}}}{SST} \newline & = \frac{SST - RSS}{s_{yy}} \newline & = \frac{s_{xx}(SST - RSS)}{s_{xx} s_{yy}} \newline & = \frac{s^2_{xy}}{s_{xx} s_{yy}} \newline & = \frac{\left(\sum_i (x_i - \bar{x})(y_i - \bar{y})\right)^2}{\sum_i (x_i - \bar{x})^2 \sum_i (y_i - \bar{y})^2}. \end{align*}

Except for its sign (positive or negative linear relationship), the sample correlation is the square root of the coefficient of determination.

Example

A food processing center needs to be able to switch from one type of package to another quickly to react to changes in order patterns. Consultants have developed a new method for changing the production line. We have a sample of 48 change-over times using the new method, and an independent sample of 72 change-over times for the existing method.

We model the relationship between YY, the change-over time and XX, the dummy variable for the new method.

We consider the simple linear regression model,

Y=β0+β1X+e,Y = \beta_0 + \beta_1 X + e,

where Y=Y = change over time and xx is the dummy variable (i.e., x = 1 for the new change-over method and 0 for the existing method).

changeover_times<-read.table("changeover_times.txt", header=TRUE)
attach(changeover_times)
m1<-lm(Changeover~New)
summary(m1)
par(mfrow=c(2,2))
plot(New,Changeover,xlab="Dummy variable, New",ylab="Change Over Time")
abline(lsfit(New,Changeover))
boxplot(Changeover~New,xlab="Dummy variable, New",ylab="Change Over Time")
boxplot(Changeover~Method,ylab="Change Over Time",xlab="Method")
# p-value
pval<-2*pt(-2.254,df=118)
pval
# equivalently, using two sample test
t.test(Changeover[New==0],Changeover[New==1],var.equal = TRUE)
t.test(Changeover[New==0],Changeover[New==1],var.equal = FALSE)
detach(changeover_times)

Diagnostic Tools for Model Checking

So far we have assumed independence, linearity, constant variance and normal error. But, we will now look at some techniques and tools to check these assumptions.

We will discuss the following,

  • Leverage points
    • If they exist, determine if they are bad leverage points.
  • Standardized residuals
  • Outliers
  • Examine assumption of constant variance
  • Examine whether the errors are normally distributed

Leverage Points

A leverage point is a point whose xx-value is distant from the other xx-values.

A point is a bad leverage point if its yy-value does not follow the pattern set by the other data points. In other words, a bad leverage point is a leverage point which is also an outlier.

The mathematical definition of leverage is,

yi^=j=1nhijyj,\hat{y_i} = \sum_{j = 1}^n h_{ij} y_j,

where hij=[1n+(xixˉ)(xjxˉ)Sxx]h_{ij} = \left[\frac{1}{n} + \frac{(x_i - \bar{x})(x_j - \bar{x})}{S_{xx}}\right].

We can see that, if we sum up hijh_{ij} for all jj,

j=1nhij=1.\sum_{j = 1}^n h_{ij} = 1.

Thus,

hii=1n+(xixˉ)2j=1n(xjxˉ)2,h_{ii} = \frac{1}{n} + \frac{(x_i - \bar{x})^2}{\sum_{j = 1}^n (x_j - \bar{x})^2},

is called the leverage of the ii-th data point. If hii1,yi^yih_{ii} \approx 1, \hat{y_i} \approx y_i.

A popular rule of thumb for detecting leverage points is,

hii>2×average(hii).=2×2n=4n.h_{ii} > 2 \times \text{average}(h_{ii}). = 2 \times \frac{2}{n} = \frac{4}{n}.

When dealing with bad leverage points, after finding them we will remove them and then fit a different model.

Standardized Residuals

It can be shown that,

Var(ei^)=σ2(1hii).\text{Var}(\hat{e_i}) = \sigma^2 (1 - h_{ii}).

This is reasonable since hii1h_{ii} \approx 1 implies that yi^yi\hat{y_i} \approx y_i and thus ei^\hat{e_i} is small.

The standardized residuals are defined as,

ri=ei^s1hii.r_i = \frac{\hat{e_i}}{s \sqrt{1 - h_{ii}}}.

where s=1n2j=1nej^2s = \sqrt{\frac{1}{n - 2} \sum_{j = 1}^n \hat{e_j}^2}.

Outliers

The common practice of labelling points as outliers is,

If the standardized residual for the point falls outside the interval from -2 to 2, it is considered an outlier. In very large data sets, we change this interval to -4 to 4.

In R, we can use the following code to find the leverage and standardized residuals,

leverage<-hatvalues(m1)
# or influence(m1)$hat
stdres<-rstandard(m1)
res<-m1$residuals
# or residuals(m1)
Cook’s Distance

Cook’s distance is a measure of the influence of each observation on the fitted values.

It is defined as,

Di=j=1n(yj(i)^yj^)22S2=ri2hii2(1hii)2.D_i = \frac{\sum_{j = 1}^n (\hat{y_{j(i)}} - \hat{y_j})^2}{2S^2} = \frac{r_i^2 h_{ii}}{2(1 - h_{ii})^2}.

where yj(i)^\hat{y_{j(i)}} denotes the jj-th fitted value when the ii-th observation is deleted. A recommended cutoff is 4/(n2)4/(n - 2).

Normality of Errors

Usually the approximation ei^ei\hat{e_i} \approx e_i is accurate enough to justify use of a QQ-plot.

Non-constant Variance

What do we do if the variance is not constant?

We can use a transformation, the transformation is used to overcome problems due to non-constant variance but also due to nonlinearity.

The methods that we will discuss deal with positive variables only.

There is no general rule of transformation, if for example we have count data, this is often modelled by the Poisson distribution. Thus, the appropriate transformation is square root.

For overcoming nonlinearity, we need to think a bit more.

Suppose the true model is Y=g(β0+β1x+e)Y = g(\beta_0 + \beta_1x + e) for an unknown monotone function gg.

We can rewrite this as,

g1(Y)=β0+β1x+e.g^{-1}(Y) = \beta_0 + \beta_1 x + e.

For possible gg, we can consider the family of scaled power transformations,

ψs(Y,λ)={(Yλ1)/λ,if λ0,log(Y),if λ=0.\psi_s(Y, \lambda) = \begin{cases} (Y^{\lambda} - 1) / \lambda, & \text{if } \lambda \neq 0, \newline \log(Y), & \text{if } \lambda = 0. \end{cases}

So, the procedure will be,

  1. Fit a linear regression model yi=β0+β1xi+eiy_i = \beta_0 + \beta_1 x_i + e_i.
  2. Estimate g1g^{-1} by plotting yi^=β^0+β^1xi\hat{y_i} = \hat{\beta}_0 + \hat{\beta}_1 x_i against xix_i.
  3. Mathematically, we minimize i(yi^α0α1ψ(yi,λ))2\sum_i (\hat{y_i} - \alpha_0 - \alpha_1 \psi(y_i, \lambda))^2 over (α0,α1,λ)(\alpha_0, \alpha_1, \lambda).