Part 5 - Multiple Linear Regression

Introduction

Last time we discussed simple linear regression, which is a model that predicts the value of a dependent variable based on the value of a single independent variable. But, often in the real world, we may want to model our dependant variable from multiple independent variables.

Or, mathematically represented as,

Yi=β0+β1x1i0β2x2i++βpxpi+eiY_i = \beta_0 + \beta_1 x_{1i} 0 \beta_2 x_{2i} + \ldots + \beta_p x_{pi} + e_i

To have a “good” fit, we try to minimize RSS, however, since we now have multiple variables, we need to minimize the RSS with respect to all the variables.

RSS=i=1n(yib0b1bpxpi)2=RSSb0=2i=1n(yib0b1x1ib2x2ibpxpi)=0=RSSb1=2i=1nx1i(yib0b1x1ib2x2ibpxpi)=0=RSSbp=2i=1nxpi(yib0b1x1ib2x2ibpxpi)=0\begin{align*} RSS &= \sum_{i=1}^{n} (y_i - b_0 - b_1 - \ldots - b_p x_{pi})^2 \newline &= \frac{\partial RSS}{\partial b_0} = -2 \sum_{i=1}^{n} (y_i - b_0 - b_1 x_{1i} - b_2 x_{2i} - \ldots - b_p x_{pi}) = 0 \newline &= \frac{\partial RSS}{\partial b_1} = -2 \sum_{i=1}^{n} x_{1i} (y_i - b_0 - b_1 x_{1i} - b_2 x_{2i} - \ldots - b_p x_{pi}) = 0 \newline & \vdots \newline &= \frac{\partial RSS}{\partial b_p} = -2 \sum_{i=1}^{n} x_{pi} (y_i - b_0 - b_1 x_{1i} - b_2 x_{2i} - \ldots - b_p x_{pi}) = 0 \newline \end{align*}

Matrix Form

Using matrix formulation is more convenient,

Y=[y1y2yn],X=[1x11x1p1x21x2p1xn1xnp],β=[β0β1βp],e=[e1e2en]\mathbf{Y} = \begin{bmatrix} y_1 \newline y_2 \newline \vdots \newline y_n \end{bmatrix}, \mathbf{X} = \begin{bmatrix} 1 & x_{11} & \ldots & x_{1p} \newline 1 & x_{21} & \ldots & x_{2p} \newline \vdots & \vdots & \ddots & \vdots \newline 1 & x_{n1} & \ldots & x_{np} \end{bmatrix}, \beta = \begin{bmatrix} \beta_0 \newline \beta_1 \newline \vdots \newline \beta_p \end{bmatrix}, \mathbf{e} = \begin{bmatrix} e_1 \newline e_2 \newline \vdots \newline e_n \end{bmatrix}

Thus, the matrix form of the equation is,

Y=Xβ+e\mathbf{Y} = \mathbf{X} \beta + \mathbf{e}

Now, we can write the RSS, denoted as RSS(β)RSS(\beta), in matrix form as,

RSS(β)=(YXβ)T(YXβ)RSS(\beta) = (\mathbf{Y} - \mathbf{X} \beta)^T (\mathbf{Y} - \mathbf{X} \beta)

We can also write our predictor for β^\hat{\beta} as,

β^=(XTX)1XTY\hat{\beta} = (\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{Y}

Since we are trying to generalize linear regression with this, it must mean that simple linear regression is a special case of multiple linear.

XTX=[111x1x2xn][1x11x21xn]=[ni=1nxii=1nxii=1nxi2]=n[1xˉxˉ1ni=1nxi2]\mathbf{X}^T \mathbf{X} = \begin{bmatrix} 1 & 1 & \ldots & 1 \newline x_1 & x_2 & \ldots & x_n \end{bmatrix} \begin{bmatrix} 1 & x_1 \newline 1 & x_2 \newline \vdots & \vdots \newline 1 & x_n \end{bmatrix} = \begin{bmatrix} n & \sum_{i=1}^{n} x_i \newline \sum_{i=1}^{n} x_i & \sum_{i=1}^{n} x_i^2 \end{bmatrix} = n \begin{bmatrix} 1 & \bar{x} \newline \bar{x} & \frac{1}{n} \sum_{i=1}^{n} x_i^2 \end{bmatrix}

The inverse of this matrix is,

(XTX)1=1n(1ni=1nxi2xˉ2)[1ni=1nxi2xˉxˉ1]=1Sxx[1ni=1nxi2xˉxˉ1](\mathbf{X}^T \mathbf{X})^{-1} = \frac{1}{n \left( \frac{1}{n} \sum_{i=1}^{n} x_i^2 - \bar{x}^2 \right)} \begin{bmatrix} \frac{1}{n} \sum_{i=1}^{n} x_i^2 & -\bar{x} \newline -\bar{x} & 1 \end{bmatrix} = \frac{1}{S_{xx}} \begin{bmatrix} \frac{1}{n} \sum_{i=1}^{n} x_i^2 & -\bar{x} \newline -\bar{x} & 1 \end{bmatrix} XTY=[111x1x2xn][y1y2yn]=[i=1nyii=1nxiyi]\mathbf{X}^T \mathbf{Y} = \begin{bmatrix} 1 & 1 & \ldots & 1 \newline x_1 & x_2 & \ldots & x_n \end{bmatrix} \begin{bmatrix} y_1 \newline y_2 \newline \vdots \newline y_n \end{bmatrix} = \begin{bmatrix} \sum_{i=1}^{n} y_i \newline \sum_{i=1}^{n} x_i y_i \end{bmatrix}

Which finally means that β^\hat{\beta} is,

β^=1Sxx[1ni=1nxi2xˉxˉ1][i=1nyii=1nxiyi]=1Sxx[1ni=1nyii=1nxi2xˉi=1nxiyii=1nxiyixˉi=1nyi]=[yˉ[i=1nxi2nxˉ2]xˉ[i=1nxiyinxˉyˉ]SxxSxySxx]=[yˉSxySxxxˉSxySxx]\begin{align*} \hat{\beta} &= \frac{1}{S_{xx}} \begin{bmatrix} \frac{1}{n} \sum_{i=1}^{n} x_i^2 & -\bar{x} \newline -\bar{x} & 1 \end{bmatrix} \begin{bmatrix} \sum_{i=1}^{n} y_i \newline \sum_{i=1}^{n} x_i y_i \end{bmatrix} \newline &= \frac{1}{S_{xx}} \begin{bmatrix} \frac{1}{n} \sum_{i=1}^{n} y_i \sum_{i=1}^{n} x_i^2 - \bar{x} \sum_{i=1}^{n} x_i y_i \newline \sum_{i=1}^{n} x_i y_i - \bar{x} \sum_{i=1}^{n} y_i \end{bmatrix} \newline &= \begin{bmatrix} \frac{\bar{y} \left[ \sum_{i=1}^n x_i^2 - n\bar{x}^2 \right] - \bar{x} \left[ \sum_{i=1}^n x_i y_i - n\bar{x}\bar{y} \right]}{S_{xx}} \newline \frac{S_{xy}}{S_{xx}} \end{bmatrix} \newline &= \begin{bmatrix} \bar{y} - \frac{S_{xy}}{S_{xx}} \bar{x} \newline \frac{S_{xy}}{S_{xx}} \end{bmatrix} \end{align*}

So we finally have our β0\beta_0 and β1\beta_1.

Let’s take a look at some properties of the multiple linear regression model.

E(β^X)=βVar(β^X)=σ2(XTX)1S2=RSSnp1=i=1ne^i2np1\begin{align*} \mathbb{E}(\hat{\beta} | \mathbf{X}) &= \beta \newline \text{Var}(\hat{\beta} | \mathbf{X}) &= \sigma^2 (\mathbf{X}^T \mathbf{X})^{-1} S^2 &= \frac{RSS}{n-p-1} = \frac{\sum_{i=1}^{n} \hat{e}_i^2}{n-p-1} \end{align*}

Inference

We can define inference for a single parameter as such,

Ti=βi^βiSE(βi^)tnp1T_i = \frac{\hat{\beta_i} - \beta_i}{SE(\hat{\beta_i})} \sim t_{n-p-1}

Analysis of variance approach to inference is also possible,

H0:β1==βp=0H1:At least one βj0H_0 : \beta_1 = \ldots = \beta_p = 0 \newline H_1 : \text{At least one } \beta_j \neq 0

Let’s recall some important definitions,

SST=Syy=i=1n(yiyˉ)2RSS==i=1n(yiyi^)2SSreg=i=1n(yi^yˉ)2SST=SSreg+RSS\begin{align*} SST = S_{yy} &= \sum_{i=1}^{n} (y_i - \bar{y})^2 \newline RSS = &= \sum_{i=1}^{n} (y_i - \hat{y_i})^2 \newline SS_{\text{reg}} &= \sum_{i=1}^{n} (\hat{y_i} - \bar{y})^2 SST = SS_{\text{reg}} + RSS \end{align*}

The test statistic is defined as,

F=SSreg/pRSS/(np1)Fp,np1F = \frac{SS_{reg}/p}{RSS/(n-p-1)} \sim F_{p, n-p-1}

where we reject H0H_0 if FF is too large.

Recall our analysis of variance table,

Table 1: Analysis of variance for multiple linear regression.
Source of Variation Degrees of Freedom (df) Sum of Squares (SS) Mean Square (MS) F-statistic
Regression p SSregSS_{\text{reg}} SSreg/pSS_{\text{reg}}/p F=SSreg/pRSS/(np1)F = \frac{SS_{\text{reg}}/p}{RSS/(n - p - 1)}
Residual np1n - p - 1 RSSRSS S2=RSSnp1S^2 = \frac{RSS}{n - p - 1}
Total n1n - 1 SST=SyySST = S_{yy}

The coefficient of determination and adjusted coefficient of determination are defined as,

R2=SSregSST=1RSSSSTRadj2=1RSS/(np1)SST/(n1)\begin{align*} R^2 &= \frac{SS_{\text{reg}}}{SST} = 1 - \frac{RSS}{SST} \newline R^2_{\text{adj}} &= 1 - \frac{RSS/(n-p-1)}{SST/(n-1)} \end{align*}

This new adjusted coefficient of determination is due to the fact of, if we add variables (even irrelevant variables) R2R^2 never decreases. Thus to compare the model with different number of variables, we use this.

We can also test a subset of coefficients using ANOVA.

H0:β1==βk=0H1:At least one βj0H_0 : \beta_1 = \ldots = \beta_k = 0 \newline H_1 : \text{At least one } \beta_j \neq 0

Then the test statistic is,

F=(RSS(reducedRSS(full))/(dfreduceddffull)RSS(full)/dffull=(RSS(reducedRSS(full))/kRSS(full)/(np1)\begin{align*} F &= \frac{(RSS(\text{reduced} - RSS(\text{full}))/(df_{\text{reduced}} - df_{\text{full}})}{RSS(\text{full})/df_{\text{full}}} \newline &= \frac{(RSS(\text{reduced} - RSS(\text{full}))/k}{RSS(\text{full})/(n-p-1)} \end{align*}

since dfreduceddffull=(n(p+1k)(n(p+1))=kdf_{\text{reduced}} - df_{\text{full}} = (n - (p + 1 - k) - (n - (p + 1)) = k.

Example

Let’s look at a simple example and the corresponding R code.

Data from surveys of customers of 168 Italian restaurants in the target area are available, with the following variables:

Y=Price=the price (in $US) of a dinner (including 1 drink & a tip)x1=Food=customer rating of the food (out of 30)x2=Deˊcor=customer rating of the decor (out of 30)x3=Service=customer rating of the service (out of 30)x4=East=dummy variable=1 if the restaurant is east of Fifth Avenue, 0 if it is west\begin{align*} Y &= \text{Price} = \text{the price (in \$US) of a dinner (including 1 drink \& a tip)} \newline x_1 &= \text{Food} = \text{customer rating of the food (out of 30)} \newline x_2 &= \text{Décor} = \text{customer rating of the decor (out of 30)} \newline x_3 &= \text{Service} = \text{customer rating of the service (out of 30)} \newline x_4 &= \text{East} = \text{dummy variable} = 1 \text{ if the restaurant is east of Fifth Avenue, 0 if it is west} \end{align*}

So, let’s first devlop a regression model that predicts the price of a dinner.

# Assume data is loaded into `nyc`.
attach(nyc)
m1<-lm(Price~Food+Decor+Service+East)
summary(m1)
> Coefficients:
> Estimate Std. Error t value Pr(>|t|)
> (Intercept) -24.244248 4.758009 -5.095 9.70e-07 ***
> Food 1.553922 0.372672 4.170 4.98e-05 ***
> Decor 1.897926 0.219100 8.662 4.79e-15 ***
> Service 0.005318 0.399493 0.013 0.9894
> East 2.003432 0.961794 2.083 0.0388 *
> ---
> Signif. codes: 0***0.001**0.01*0.05.0.1 ‘ ’ 1

So, our initial regression model is,

Price=24.244248+1.553922Food+1.897926Decor+0.005318Service+2.003432East\text{Price} = -24.244248 + 1.553922 \text{Food} + 1.897926 \text{Decor} + 0.005318 \text{Service} + 2.003432 \text{East}

We can see that Décor has the largest effect on the price (note that Food, Décor and Service are each measured on the same 0 to 30 scale so it is meaningful to compare the regression coefficients).

We can also observe that, if a new resturant wants to choose a location, it should be east of Fifth Avenue, since our dummy variable is statistically significantly larger than 0.

Interaction Effects

Consider we want to model YY based on a continuous predictor xx and a dummy variable dd. Compared to using a single variable xx, the additional effect of dd could be,

  1. dd ahs no effect on YY, that is, Y=β0+β1x+eY = \beta_0 + \beta_1 x + e.
  2. dd produces only an additive change in YY, that is,
Y=β0+β1x+β2d+e={β0+β1x+ed=0β0+β2+β1x+ed=1Y = \beta_0 + \beta_1 x + \beta_2 d + e = \begin{cases} \beta_0 + \beta_1 x + e & d = 0 \newline \beta_0 + \beta_2 + \beta_1 x + e & d = 1 \end{cases}
  1. dd produces an additive change in YY and also changes the size of the effect of xx on YY, that is,
Y=β0+β1x+β2d+β3xd+e={β0+β1x+ed=0β0+β2+(β1+β3)x+ed=1Y = \beta_0 + \beta_1 x + \beta_2 d + \beta_3 xd + e = \begin{cases} \beta_0 + \beta_1 x + e & d = 0 \newline \beta_0 + \beta_2 + (\beta_1 + \beta_3) x + e & d = 1 \end{cases}

Let’s go back to our example again.

After you present your model to the management team, the debate foucses on the statistical insignificane of the Service variable. The team feels like the model may be too simple to reflect the reality of a Italian restaurant in Manhattan. In particular, there is general consensus amongst the team that restaurants on the east of Fifth Avenue are very different from those on the west side. As such, you have been asked to consider different models for the East and West.

So, we can develop a mathematical model as such,

Y=β0+β1x1+β2x2+β3x3+β4East+β5x1East+β6x2East+β7x3East+eY = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \beta_3 x_3 + \beta_4 \text{East} + \beta_5 x_1 \text{East} + \beta_6 x_2 \text{East} + \beta_7 x_3 \text{East} + e

In our code now,

m2<-lm(Price~Food+Decor+Service+East+Food:East+Decor:East+Serivce:East)
> Coefficients:
> Estimate Std. Error t value Pr(>|t|)
> (Intercept) -26.9305 8.5195 -3.161 0.00189 **
> Food 0.9986 0.5742 1.739 0.08398 .
> Decor 1.8824 0.3005 6.264 3.45e-09 ***
> Service 0.7566 0.6489 1.166 0.24536
> East 5.5702 10.3285 0.539 0.59044
> Food:East 1.2679 0.7815 1.622 0.10673
> Decor:East -0.2791 0.4618 -0.604 0.54646
> Service:East -1.2842 0.8230 -1.560 0.12071
> Residual standard error: 5.747 on 157 degrees of freedom
> (3 observations deleted due to missingness)
> Multiple R-squared: 0.6381, Adjusted R-squared: 0.622
> F-statistic: 39.55 on 7 and 157 DF, p-value: < 2.2e-16

If we also do an ANOVA test,

anova(m1, m2)
> Analysis of Variance Table
> Model 1: Price ~ Food + Decor + East
> Model 2: Price ~ Food + Decor + Service + East + Food:East + Decor:East + Service:East
> Service:East
> Res.Df RSS Df Sum of Sq F Pr(>F)
> 1 161 5338.9
> 2 157 5186.1 4 152.82 1.1566 0.3322

This means we are happy to adopt the original reduced model.

Diagnostics Summary

The main tools we will use to validate regression assumptions are,

  1. Plots involving standardized residuals and/or *fitted values.
  2. Determine leverage points
  3. Determine which (if any) of the data points are outliers.
  4. Asses the effect of each predictor variable, having adjusted for the effect of other predictor variables using added variable plots.
  5. Asses the extent of collinearity among the predictor variables using variance inflation factors.
  6. Examine whether the assumption of normality of error and constant error variance is reasonable.

Leverage Points and Residuals

Remember from simple linear regression, leverage points are points that have extreme values of the predictor variable.

Y^=Xβ^=X(XTX)1XTY=HY,\mathbf{\hat{Y}} = \mathbf{X} \hat{\beta} = \mathbf{X}(\mathbf{X}^T \mathbf{X})^{-1} \mathbf{X}^T \mathbf{Y} = \mathbf{H} \mathbf{Y},

where H=X(XX)1XT\mathbf{H} = \mathbf{X}(\mathbf{X} \mathbf{X})^{-1} \mathbf{X}^T (also called the hat matrix).

Let hijh_{ij} be the (i,j)(i, j)-entry of H\mathbf{H}, then,

Yi^=hiiYi+jihijYj\hat{Y_i} = h_{ii} Y_i + \sum_{j \neq i} h_{ij} Y_j

The rule of thumb for leverage points are,

hii>2×average(hii)=2×p+1nh_{ii} > 2 \times \text{average}(h_{ii}) = 2 \times \frac{p+1}{n}

The residuals are defined as,

e^=YY^=(IH)Y\mathbf{\hat{e}} = \mathbf{Y} - \mathbf{\hat{Y}} = (\mathbf{I} - \mathbf{H}) \mathbf{Y}

We can show that,

Var(e^X)=σ2(IH),\text{Var}(\mathbf{\hat{e}} | \mathbf{X}) = \sigma^2(\mathbf{I} - \mathbf{H}),

and that the standardized residuals are,

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

where,

s=i=1nei2^np1s = \sqrt{\frac{\sum_{i=1}^n \hat{e_i^2}}{n - p - 1}}

Any pattern in a residual plot indicates that an incorrect model has been fit, but the pattern in general does not provide direct information on how the model is misspecified.

Added Variable Plots

Assume we originally have the model,

YXβ+e.\mathbf{Y} \mathbf{X} \beta + \mathbf{e}.

With an additional predictor, we now consider,

Y=Xβ+Zα+e,\mathbf{Y} = \mathbf{X} \beta + \mathbf{Z} \alpha + \mathbf{e},

where,

Z=[z1z2zn]\mathbf{Z} = \begin{bmatrix} z_1 \newline z_2 \newline \vdots \newline z_n \end{bmatrix}

The procedure is as follows,

  1. Perform Regression
Y=Xβ+e\mathbf{Y} = \mathbf{X} \beta + \mathbf{e}

to get the residual eY.X\mathbf{e}_{\mathbf{Y}.\mathbf{X}}.

  1. Perform Regression
Z=Xβ+e\mathbf{Z} = \mathbf{X} \beta + \mathbf{e}

to get residual eZ.X\mathbf{e}_{\mathbf{Z}.\mathbf{X}}.

  1. Plot eY.X\mathbf{e}_{\mathbf{Y}.\mathbf{X}} (on yy-axis)

against eZ.X\mathbf{e}_{\mathbf{Z}.\mathbf{X}} (on xx-axis).

Transformations

Transforming only YY using inverse response plot.

Assume the true model is actually,

Y=g(β0+β1x1++βpxp+e),Y = g(\beta_0 + \beta_1 x_1 + \ldots + \beta_p x_p + e),

the inverse model is thus,

g1(Y)=β0+β1x1++βpxp+eg^{-1}(Y) = \beta_0 + \beta_1 x_1 + \ldots + \beta_p x_p + e

To use inverse response plot, an important assumption is that the predictors are pairwise linearly related.

Example: Defective rates.

We want to develop a model for number of defectives based on x1x_1: temperature, x2x_2: density, and x3x_3: production rate.

To get the pairwise linear relationship, we can use the scatterplot matrix.

pairs(~Defective + Temperature + Density + Rate)

To get the inverse response plot, we can use the following code.

m1<-lm(Defective~Temperature+Density+Rate)
inverseResponsePlot(m1)

Collinearity of Predictors

When higly correlated predictor variables are included, they are effectively carrying very similar information about the response variable.

Thus, it is difficult for least squares to distinguish their separate effects on the response variable.

Some of the coefficients in the regression model are of the opposite sign than expected.

Consider the multiple regression model,

Y=β0+β1x1+βpxp+e.Y = \beta_0 + \beta_1 x_1 + \ldots \beta_p x_p + e.

Let Rj2R_j^2 be the coefficient of determination R2R^2 obtained when regressing xjx_j on other predictors.

Then it can be shown that,

Var(βj^)=11Rj2σ2(n1)Sxj2\text{Var}(\hat{\beta_j}) = \frac{1}{1 - R_j^2} \frac{\sigma^2}{(n - 1) S_{x_j}^2}

11Rj2\frac{1}{1 - R_j^2} is called the variance inflation factor.

A rough guide for identifying large VIF is to use the cut-off value 5.

What do you do when collinearity exists?

  1. Do nothing but be careful of interpretation.
  2. Remove highly correlated variables (keep only one of them).