ISL

hn

2022-06-27

Chapter 3 of James, Witten, Hastie, & Tibshirani (2021). See https://www.statlearning.com/ for data and resources.

3 Linear Regression

Linear regression (LR) is a useful tool for predicting a quantitative response and a simple type of supervised learning. Understanding LR remains important, as newer methods of statistical learning can be seen as generalizations or extensions of LR.

Example

Reconsidering the Advertising data from Chapter 2:

library(ISLR2)

# Data: 
# https://www.statlearning.com/resources-second-edition
data_url  <- "https://www.statlearning.com/s/Advertising.csv"

if (wd == "/Users/hneth/Desktop/stuff/Dropbox/GitHub/predict/R"){ # in subdir R: 
  data_file <- "./../data/_JamesEtAl2021_ISL2/data/Advertising.csv"  # up 1 level
} else {
  data_file <- "./data/_JamesEtAl2021_ISL2/data/Advertising.csv" 
}

ad <- tibble::as_tibble(read.csv(data_file))
dim(ad)
## [1] 200   5

Figure 2.1 (p. 16) displayed sales (in thousands of units) for a particular product as a function of advertising budgets (in thousands of dollars) for TV, radio, and newspaper media:

Figure 2.1: Advertising data.

Recreating Figure 2.1:

par(mfrow = c(1, 3))

plot(ad$TV, ad$sales, main = "(a) sales by TV",
     pch = 20, cex = 2, col = adjustcolor("black", .25))
abline(lm(sales ~ TV, data = ad), lwd = 2, col = "deepskyblue")

plot(ad$radio, ad$sales, main = "(b) sales by radio",
     pch = 20, cex = 2, col = adjustcolor("black", .25))
abline(lm(sales ~ radio, data = ad), lwd = 2, col = "deeppink")

plot(ad$newspaper, ad$sales, main = "(c) sales by newspaper", 
     pch = 20, cex = 2, col = adjustcolor("black", .25))
abline(lm(sales ~ newspaper, data = ad), lwd = 2, col = "gold")

par(opar)  # restore original par
Figure 2.1

Figure 2.1

For an output variable \(Y\) and a set of \(p\) predictor variables \(X = (X_1,\ X_2,\ \ldots,\ X_p)\) we assume:

\[Y = f(X) + \epsilon\]

Here, \(f\) represents systematic information, whereas \(\epsilon\) represents random error information (aka. noise, independent of \(X\) and with a mean of zero).

In simple LR, we assume that the function \(f\) can be approximated by a line (with two parameters, i.e., \(\beta_0 + \beta_1 X\)). Thus,

\[Y = \beta_0 + \beta_1 X + \epsilon\]

The error or noise term \(\epsilon\) is assumed to be independent of the predictor \(X\) and contain everything that we miss in the linear model \(f(X)\). For instance,

  • the true relationship between \(X\) and \(Y\) may be non-linear,
  • other variables beyond \(X\) may contribute to the variation of \(Y\), and
  • our measurements may contain errors.

Questions

The LR paradigm allows addressing the following (types of) questions:

  1. Is there a relationship between predictors (advertising budgets) and outcome (sales)?

  2. How strong is the relationship?

  3. Which predictors matter? What are their individual contributions on the overall outcome?

  4. How strong is the relationship for each predictor (with or without considering the others)?

  5. Prediction: How accurately can we predict future outomes?

  6. Inference: What is the form of the relationship (e.g., linear or non-linear)?

  7. Interactions: Are there synergy effects (interactions)?

These questions will be addressed for the Advertising data in below (see Section 3.4).

3.1 Simple linear regression

Simple LR assumes a single predictor variable \(X\) and an approximately linear relationship between \(X\) and an outcome variable \(Y\):

\[Y \approx \beta_0 + \beta_1 X\]

When plotting outcome values \(Y\) as a function of predictor values \(X\), simple LR estimates two parameters (or coefficients) of a straight line: its intercept \(\beta_0\) (for \(X = 0\)) and its slope \(\beta_1\).

Estimating the coefficients

Varying the parameters for intercept and slope (i.e., \(\beta_0\) and \(\beta_1\)) yields different lines. Each line provides a specific model that predicts a particular outcome value \(\hat{Y}\) for any predictor value \(\hat{X}\). (The hat-symbols signal that we are usually estimating unknown population values from observed samples.) Determining how closely our predictions fit to the data requires that we quantify the deviation between observed and predicted data. The criterion for measuring closeness or quantifying the degree of fit used here is the least squared deviation: We select \(\beta_0\) and \(\beta_1\) so that the residual sum of squares (RSS) — commonly known as mean squared error (MSE) — of our predictions is minimized.

See Equation 3.4 (James et al., 2021, p. 62) for the least squares coefficient estimates for simple LR.

Figure 3.1 (p. 62) shows the least squares fit for the regression of sales onto TV for the Advertising data. The fit is found by minimizing the residual sum of squares.

Figure 3.1: Least square fit of a regression model.

Recreating a version of Figure 3.1:

# Scatterplot: ---- 
plot(x = ad$TV, y = ad$sales, 
     main = "Sales by TV advertising budget", xlab = "Budget (in units of $1,000)", ylab = "Sales (in units of 1,000)", 
     ylim = c(0, 30), 
     pch = 20, cex = 1.5, col = adjustcolor("black", .50))

# Simple linear regression model: ---- 
slm_1 <- lm(sales ~ TV, data = ad) 
abline(slm_1, lwd = 2, col = "deepskyblue")

# Summary: ---- 
summary(slm_1)
## 
## Call:
## lm(formula = sales ~ TV, data = ad)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.3860 -1.9545 -0.1913  2.0671  7.2124 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 7.032594   0.457843   15.36   <2e-16 ***
## TV          0.047537   0.002691   17.67   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.259 on 198 degrees of freedom
## Multiple R-squared:  0.6119, Adjusted R-squared:  0.6099 
## F-statistic: 312.1 on 1 and 198 DF,  p-value: < 2.2e-16

# Predictions: ---- 
y_hat <- predict(slm_1)
segments(x0 = ad$TV, y0 = ad$sales, y1 = y_hat,
         lwd = .8, col = "deeppink")
Figure 3.1: Sales by TV advertising budget (with linear regression model and residual deviations).

Figure 3.1: Sales by TV advertising budget (with linear regression model and residual deviations).

Assessing the accuracy of the coefficient estimates

In statistics, we usually are interested in population values (e.g., \(\mu\)), but have only access to samples (e.g., \(\hat{\mu}\)). Thus, we need to estimate the population parameters from samples.

In the context of simple LR, we are interested in the coefficients \(beta_0\) and \(beta_1\) of the population regression line. However, we need to estimate these unknown coefficients from our sample(s) using \(\hat{\beta_0}\) and \(\hat{\beta_1}\).

True relation vs. least-square regression line

Figure 3.3 (p. 64) compares the true relationship between \(X\) and \(Y\) with the estimated coefficients of simple LR for a simulated data set:

Figure 3.3: Estimated vs. true relationship between predictor and response.

  • Left: The red line represents the true relationship, \(Y = f(X) = 2 + 3X\), which is known as the population regression line. The blue line is the least squares line; it is the least squares estimate for \(f(X)\) based on the observed data, shown in black.

  • Right: The population regression line is again shown in red, and the least squares line in dark blue. In light blue, ten least squares lines are shown, each computed on the basis of a separate random set of observations. Each least squares line is different, but on average, the least squares lines are quite close to the population regression line.

Just like many sample means \(\hat{\mu}\) provide unbiased estimates of the true population mean \(\mu\)), the estimated coefficients \(\hat{\beta_0}\) and \(\hat{\beta_1}\) provide unbiased estimates of the true regression line in the population. When drawing many samples, the average of the estimates approximate the true population values.

Measures of deviation

To quantify the deviation between population values and sample-based estimates, we can compute standard errors (SE), residual standard errors (RSE), and confidence intervals (CI).

For simple LR, the 95%-confidence interval for the population (or true) coefficients (\(beta_0\) and \(beta_1\)) can be computed from the estimated (or sample-based) coefficients (\(\hat{\beta_0}\) and \(\hat{\beta_1}\)) and their corresponding standard errors as:

\[ \hat{\beta_0} \pm 2 \cdot SE(\hat{\beta_0}) \] \[ \hat{\beta_1} \pm 2 \cdot SE(\hat{\beta_1}) \]

Hypothesis testing

Endowed with estimated coefficients and standard errors, we can perform hypothesis tests on the coefficients:

  • \(H_a\): There is a relationship between \(X\) and \(Y\): \(\beta_1 \neq 0\)
  • \(H_0\): There is no relationship between \(X\) and \(Y\): \(\beta_1 = 0\)

Using a \(t\)-distribution (to assess the likelihood of an estimated coefficient’s deviation from 0) yields a \(p\)-value: The probability that the observed effect was caused exclusively by chance (i.e., \(P(data | H_0)\)) — which is not the probability that \(H_a\) is true!

For our model slm_1 (from above):

# Simple linear regression model: ---- 
slm_1 <- lm(sales ~ TV, data = ad) 

# Summary of the slm_1 model (above):
summary(slm_1)
## 
## Call:
## lm(formula = sales ~ TV, data = ad)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.3860 -1.9545 -0.1913  2.0671  7.2124 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 7.032594   0.457843   15.36   <2e-16 ***
## TV          0.047537   0.002691   17.67   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.259 on 198 degrees of freedom
## Multiple R-squared:  0.6119, Adjusted R-squared:  0.6099 
## F-statistic: 312.1 on 1 and 198 DF,  p-value: < 2.2e-16

As the SE-values for both coefficients are very small, the estimated (or sample-based) coefficients (\(\hat{\beta_0}\) and \(\hat{\beta_1}\)) deviate from 0.

This is tested by the \(t\)-statistic (and corresponding \(p\)-values).

Rejecting \(H_0\) for both \(\beta_0\) and \(\beta_1\) implies:

  • \(\beta_0 \neq 0\): There are sales in the absence of TV advertising.
  • \(\beta_1 \neq 0\): There is a relationship between TV advertisements and sales.

Assessing the accuracy of the model

We want to quantify the extent to which a model fits the data. The quality of fit for a LR model is typically assessed using two related quantities: the residual standard error (RSE) and the \(R^2\) statistic.

Residual standard error (RSE)

The residual standard error (RSE) estimates the standard deviation of the irreducible error \(\epsilon\) to quantify the extent to which our predictions would miss on average if we knew the true LR line (i.e., the population values \(\beta_0\) and \(\beta_1\)). The residual standard error (RSE) is based on the residual sums of squares (RSS, see the formulas on p. 69):

\[\text{RSS} = \sum_{i=1}^{n}{(y_i - \hat{y_i})^2}\]

and computed as:

\[\text{RSE} = \sqrt{\frac{1}{n-2} \cdot \text{RSS}}\]

As RSE quantifies the lack of fit of a model in units of \(Y\), lower values are better (ideally, \(RSE \rightarrow 0\)). In our above example model slm_1, \(RSE = 3.26\) and \(Y\) represents sales in units of 1,000. Thus, even if our model was perfectly correct, our predictions would deviate from the true values by approximately 3,260 units, on average.

Proportion of variance explained (\(R^2\))

Whereas RSE provides an absolute measure of model fit (in units of the criterion \(Y\)), the \(R^2\) statistic provides an alternative measure of fit as the proportion of non-erroreous squared deviations, or the proportion of variance explained:

\[R^2 = \frac{\text{TSS} - \text{RSS}}{\text{TSS}}\] with

\[\text{TSS} = \sum_{i=1}^{n}{(y_i - \bar{y_i})^2}\]

(Note that TSS is a special case of RSS, with only a single predicted value \(\bar{y_i}\).)

In contrast to error measures, \(R^2\) ranges from \(0\) to \(1\), with higher values indicating better model fits (ideally, \(R^2 \rightarrow 1\)).

Given a model, \(R^2\) measures the proportion of variability in \(Y\) that can be explained by \(X\). An \(R^2\) value close to 1 indicates that a large proportion of the variability in the response variable is explained by the regression model.

Note:

  • TSS is a special case of RSS (with only )

  • Although \(R^2\) is always between 0 and 1, it can still be challenging to determine what constitutes a “good” \(R^2\) value. For domains in which the true model is highly linear and \(\epsilon\) is small, we can obtain \(R^2 \approx 1\). In social sciences, relationships can be non-linear and subject to many other influences, even small \(R^2 > 0\) can be meaningful.

  • The \(R^2\) is related to the correlation \(r = \text{Cor}(X, Y)\). In simple LR (with only 1 predictor), \(R^2 = r^2\).

Accuracy measures in R

We can easily define R functions for the total sum of squares (TSS), residual sum of squares (RSS), the residual standard error (RSE), and the \(R^2\)-statistic, when providing both the true and predicted values as arguments:

# Measures of model accuracy: 
# (see @JamesEtAl2021, p. 69ff.): 


# Total sum of squares (TSS): ---- 
TSS <- function(true_vals, pred_vals = NA){
  
  # Special case: 
  if (is.na(pred_vals)){
    pred_vals <- mean(true_vals)
  }
  
  sum((true_vals - pred_vals)^2)
}


# Residual sum of squares (RSS): ---- 
RSS <- function(true_vals, pred_vals){
  
  sum((true_vals - pred_vals)^2)
}


# Residual standard error (RSE): ----
RSE <- function(true_vals, pred_vals){
  
  n <- length(true_vals)  
  
  rss <- RSS(true_vals, pred_vals)  # defined above
  
  sqrt(1/(n - 2) * rss)
}


# R^2 statistic: ----
Rsq <- function(true_vals, pred_vals){
  
  tss <- TSS(true_vals)  # defined above
  
  rss <- RSS(true_vals, pred_vals)  # defined above
  
  (tss - rss)/tss
}

3.2 Multiple linear regression

Simple LR assumes only one predictor variable, but we often have more than one predictor.

Multiple single LR

We could consider running separate models for each predictor.

Re-creating Table 3.3 (p. 72): Multiple simple/single LR models for the Advertising data:

# Simple linear regression models: ---- 
slm_1 <- lm(sales ~ TV, data = ad) 
slm_2 <- lm(sales ~ radio, data = ad) 
slm_3 <- lm(sales ~ newspaper, data = ad) 

# Summary of each model:
summary(slm_1)
## 
## Call:
## lm(formula = sales ~ TV, data = ad)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.3860 -1.9545 -0.1913  2.0671  7.2124 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 7.032594   0.457843   15.36   <2e-16 ***
## TV          0.047537   0.002691   17.67   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.259 on 198 degrees of freedom
## Multiple R-squared:  0.6119, Adjusted R-squared:  0.6099 
## F-statistic: 312.1 on 1 and 198 DF,  p-value: < 2.2e-16
summary(slm_2)
## 
## Call:
## lm(formula = sales ~ radio, data = ad)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15.7305  -2.1324   0.7707   2.7775   8.1810 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  9.31164    0.56290  16.542   <2e-16 ***
## radio        0.20250    0.02041   9.921   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.275 on 198 degrees of freedom
## Multiple R-squared:  0.332,  Adjusted R-squared:  0.3287 
## F-statistic: 98.42 on 1 and 198 DF,  p-value: < 2.2e-16
summary(slm_3)
## 
## Call:
## lm(formula = sales ~ newspaper, data = ad)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -11.2272  -3.3873  -0.8392   3.5059  12.7751 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 12.35141    0.62142   19.88  < 2e-16 ***
## newspaper    0.05469    0.01658    3.30  0.00115 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.092 on 198 degrees of freedom
## Multiple R-squared:  0.05212,    Adjusted R-squared:  0.04733 
## F-statistic: 10.89 on 1 and 198 DF,  p-value: 0.001148

Three distinct simple/single LR models are unsatisfactory:

  • It remains unclear how we can predict the outcome variable based on a combination of predictor values.

  • Also, each simple/single LR model ignores (or rather tacitly incorporates) the effects of the other predictor variables.

Multiple LR

A better approach is to extend the simple LR model by incorporating multiple predictors in a single model. The following multiple linear regression (multiple LR) model uses a separate slope coefficient for each predictor \(p\):

\[ Y = \beta_0 + \beta_1 X_1 + \beta_2 X_2 +\ \ldots\ + \beta_p X_p + \epsilon \]

where \(X_j\) represents the \(j\)th predictor and the coefficient \(\beta_j\) quantifies the association between that predictor and the response.

We interpret \(beta_j\) as the average effect on \(Y\) of a one unit increase in \(X_j\), when holding all other predictors fixed.

Estimating the regression coefficients

# Multiple linear regression model (3 predictors): ---- 
mlm_1 <- lm(sales ~ TV + radio + newspaper, data = ad)

# Summary of multiple LR model:
summary(mlm_1)
## 
## Call:
## lm(formula = sales ~ TV + radio + newspaper, data = ad)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.8277 -0.8908  0.2418  1.1893  2.8292 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.938889   0.311908   9.422   <2e-16 ***
## TV           0.045765   0.001395  32.809   <2e-16 ***
## radio        0.188530   0.008611  21.893   <2e-16 ***
## newspaper   -0.001037   0.005871  -0.177     0.86    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.686 on 196 degrees of freedom
## Multiple R-squared:  0.8972, Adjusted R-squared:  0.8956 
## F-statistic: 570.3 on 3 and 196 DF,  p-value: < 2.2e-16

Note that the simple and multiple regression coefficients can be quite different. Here, the coefficient for the newspaper predictor is no longer significant, when accounting for the effects of TV and radio.

How can the effect of newspaper be significant in a simple/single LR, but non-significant in a multiple LR? The correlation between the radio and newspaper predictors is 0.35:

Re-creating Table 3.5 (p. 75): Correlation matrix for TV, radio, newspaper, and sales, for the Advertising data:

# Correlation matrix:
df <- ad[ , -1]
round(cor(x = df), 2)
##             TV radio newspaper sales
## TV        1.00  0.05      0.06  0.78
## radio     0.05  1.00      0.35  0.58
## newspaper 0.06  0.35      1.00  0.23
## sales     0.78  0.58      0.23  1.00

Thus, the simple/single LR of sales by newspaper captures some of the variance due to the radio predictor. In other words, newspaper advertising is a surrogate for radio advertising, or the newspaper predictor gets some credit for the association between radio and sales. When incorporating both predictors in a single model, the effect of newspaper disappears:

# Multiple linear regression model (2 predictors): ---- 
mlm_2 <- lm(sales ~ radio + newspaper, data = ad)

# Summary mLR:
summary(mlm_2)
## 
## Call:
## lm(formula = sales ~ radio + newspaper, data = ad)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -15.5289  -2.1449   0.7315   2.7657   7.9751 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 9.188920   0.627672  14.640   <2e-16 ***
## radio       0.199045   0.021870   9.101   <2e-16 ***
## newspaper   0.006644   0.014909   0.446    0.656    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.284 on 197 degrees of freedom
## Multiple R-squared:  0.3327, Adjusted R-squared:  0.3259 
## F-statistic: 49.11 on 2 and 197 DF,  p-value: < 2.2e-16

Example involving common sense phenomenon (or a common cause variable): Assume an association between the predictor ice cream sales on the outcome shark attacks on beaches. Once we incorporate the (common cause) predictor temperature, the association disappears.

Some important questions

When we perform multiple linear regression, we usually are interested in answering the following questions:

  1. Is at least one of the predictors \(X_1,\ X_2,\ \ldots,\ X_p\) useful in predicting the response \(Y\)?

  2. Do all the predictors help to explain \(Y\) , or is only a subset of the predictors useful?

  3. How well does the model fit the data?

  4. Given a set of predictor values, what response value should we predict, and how accurate is our prediction?

ad 1. Overall effect vs. contributions of individual predictors

Is at least one of the predictors \(X_1,\ X_2,\ \ldots,\ X_p\) useful in predicting the response \(Y\)?

A large \(F\)-statistic (\(F > 1\), taking into account the \(df\) implied by \(n\) and \(p\)) implies that at least one of the predictors is successful in predicing the outcome/response variable.

In contrast to this overall effect, the \(t\)-statistic and the corresponding \(p\)-value of each coefficient informs about whether each predictor is related to the outcome/response when adjusting for the other predictors.

???: Note that \({t_{j}}^2 = F_j\) for the partial effect of each predictor \(X_j\) (see James et al., 2021, p. 77, Footnote 7).

Good question: Given individual \(p\)-values for each coefficient, why do we need the overall \(F\)-statistic?
Consider an example with many predictors (or multiple tests): For instance, assume a multiple LR model with \(p = 100\) predictors, but
\(H_0: \beta_1 = \beta_2 =\ \ldots\ = \beta_p = 0\) is true, so no variable is truly associated with the response. In this situation, about \(5\)% of the \(p\)-values associated with each coefficient will be below \(0.05\) by chance (and — simply by definition — “statistically significant”). In other words, we expect to see approximately five significant \(p\)-values in the absence of any true association between the predictors and the response. By contrast, the \(F\)-statistic does not suffer from the problem of multiple tests because it adjusts for the number of predictors \(p\).

ad 2. Deciding on important variables

Do all the predictors help to explain \(Y\) , or is only a subset of the predictors useful?

The task of determining which predictors are associated with the response, in order to fit a single model involving only those predictors, is referred to as variable selection (see Chapter 6). With \(p\) predictors, we can create \(2^p\) different models containing subsets of predictor variables (not yet considering interactions). Various statistics allow judging the quality of a model (including AIC, BIC).

Three classical approaches for solving the variable selection task are:

  1. Forward selection: Begin with the null model and add the predictor that results in a model with the lowest RSS. Proceed until some stopping criterion is reached.

  2. Backward selection: Begin with the full model and remove the predictor that has the highest \(p\) value. Proceed until some stopping criterion is reached.

  3. Mixed selection: Begin with forward selection, but also remove predictors when their \(p\)-values increases above some criterion. Continue until all variables remaining in the model have a sufficiently low \(p\)-value.

Note:

  • Backward selection cannot be used when \(p > n\).

  • Forward selection can always be used (even when \(p > n\)), but is a greedy approach (i.e., might include variables early that later become redundant). Mixed selection can remedy this.

ad 3. Model fit

How well does the model fit the data?

Two of the most common numerical measures of model fit are the residual standard error (RSE) and the fraction of variance explained (\(R^2\)).

Note:

  • Relation between \(R^2\) and correlation: In simple LR (with only 1 predictor), we had \(R^2 = r^2\). In multiple LR, we have \(R^2 = \text{Cor}(Y, \hat{Y})^2\) (i.e., multiple LR maximizes the correlation between response \(Y\) and the predictions of the fitted linear model \(\hat{Y}\)).

  • Number of predictors \(p\), \(R^2\), vs. model quality: \(R^2\) always increases when adding variables to a model, even if those variables are only weakly associated with the response. This is due to the fact that adding another variable always results in a decrease in the RSS on the training data, but not necessarily the testing data.

  • Graphical summaries can reveal problems with a model that are not visible from numerical statistics (e.g., non-linearity).

Example: Figure 3.5 (p. 81) shows a non-linear relationship in data for a multiple LR with 2 predictors. Such a non-linear pattern often suggests a synergy or interaction effect.

Figure 3.5: Non-linear relationship for multiple LR.

ad 4. Predictions

Given a set of predictor values, what response value should we predict, and how accurate is our prediction?

For a specific multiple LR model and a set of values for the predictors \(X_1, X_2,\ \ldots, X_p\), it is straightforward to predict the response \(\hat{Y}\). There are three sorts of uncertainty associated with this prediction:

  1. Reducible error: The coefficients \(\hat{\beta_0}, \hat{\beta_1}, \ldots, \hat{\beta_p}\) are only estimations for the true population values \(\beta_0, \beta_1,\ \ldots, \beta_p\).

  2. Model bias: Even the best linear model \(f(X)\) must not be the true relationship (which may be non-linear).

  3. Irreducible error: Even the true population model would be off by random error/noise \(\epsilon\).

Distinction between confidence interval (average \(Y\) over many observations) vs. prediction interval (uncertainty surrounding \(Y\) for a particular observation).

  • Confidence intervals express sampling uncertainty in quantities estimated from many data points. The more data, the less sampling uncertainty, and hence the thinner the interval.

  • Prediction intervals, on top of the sampling uncertainty, also express uncertainty around a single value, which makes them wider than the confidence intervals.

See blog post at Confidence intervals vs prediction intervals (2021-11-08).

3.3 Other considerations in the regression model

The Credit dataset of ISLR2 contains both quantitative and qualitative predictors:

library(ISLR2)
library(tidyverse)

cred <- ISLR2::Credit
dim(cred)  # 400 x 11
## [1] 400  11
summary(cred)
##      Income           Limit           Rating          Cards      
##  Min.   : 10.35   Min.   :  855   Min.   : 93.0   Min.   :1.000  
##  1st Qu.: 21.01   1st Qu.: 3088   1st Qu.:247.2   1st Qu.:2.000  
##  Median : 33.12   Median : 4622   Median :344.0   Median :3.000  
##  Mean   : 45.22   Mean   : 4736   Mean   :354.9   Mean   :2.958  
##  3rd Qu.: 57.47   3rd Qu.: 5873   3rd Qu.:437.2   3rd Qu.:4.000  
##  Max.   :186.63   Max.   :13913   Max.   :982.0   Max.   :9.000  
##       Age          Education      Own      Student   Married     Region   
##  Min.   :23.00   Min.   : 5.00   No :193   No :360   No :155   East : 99  
##  1st Qu.:41.75   1st Qu.:11.00   Yes:207   Yes: 40   Yes:245   South:199  
##  Median :56.00   Median :14.00                                 West :102  
##  Mean   :55.67   Mean   :13.45                                            
##  3rd Qu.:70.00   3rd Qu.:16.00                                            
##  Max.   :98.00   Max.   :20.00                                            
##     Balance       
##  Min.   :   0.00  
##  1st Qu.:  68.75  
##  Median : 459.50  
##  Mean   : 520.01  
##  3rd Qu.: 863.00  
##  Max.   :1999.00

sub_cred <- cred %>%
  select(Balance, Age, Cards, Education, Income, Limit, Rating, Own)

plot(sub_cred, pch = 20, cex = .8, col = adjustcolor("steelblue", .20))

Qualitative predictors

Given a quantitative outcome/response variable, binary predictor variable can be dummy coded as 0 vs. 1:

Example: Predicting Credit card balance based on demographic variables (quantitative and qualitative/categorical).

Re-creating Table 3.7 (p. 85): Least squares coefficient estimates associated with the regression of credit card Balance onto home ownership (binary predictor Own) in the Credit data set.

# Descriptive: 
# table(cred$Own)
tb_own <- cred %>%
  group_by(Own) %>%
  summarise(n = n(),
            mn_balance = mean(Balance))

knitr::kable(tb_own, caption = "N and mean balance by home ownership.")
N and mean balance by home ownership.
Own n mn_balance
No 193 509.8031
Yes 207 529.5362
# library(kableExtra)
# kbl(tb_own, booktabs = TRUE, caption = "N and mean balance by home ownership.") %>% 
#  kable_styling(latex_options = "striped", full_width = FALSE)

# Box plot: 
plot(x = cred$Own, y = cred$Balance, 
     main = "Balance by home ownership", 
     col = adjustcolor(c("deepskyblue", "deeppink"), .33))


# Linear model (with binary dummy variable):
mlm_2 <- lm(Balance ~ Own, data = cred)
summary(mlm_2)
## 
## Call:
## lm(formula = Balance ~ Own, data = cred)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -529.54 -455.35  -60.17  334.71 1489.20 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   509.80      33.13  15.389   <2e-16 ***
## OwnYes         19.73      46.05   0.429    0.669    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 460.2 on 398 degrees of freedom
## Multiple R-squared:  0.0004611,  Adjusted R-squared:  -0.00205 
## F-statistic: 0.1836 on 1 and 398 DF,  p-value: 0.6685

Interpretation:

  • intercept: Average credit card balance without home (i.e., Own == No baseline): $509.80

  • coefficient: Increase by $19.73 in case of home ownership (but effect is small and non-significant).

Note that the interpretation of the coefficients depends on the way the variables (here: the factor Own) is coded. By contrast, the interpreted predictions remain the same, regardless of coding:

table(cred$Own)  # is a factor with 2 levels (1: No, 2: Yes)
## 
##  No Yes 
## 193 207

# Recode binary predictor:
not_Own <- (as.numeric(cred$Own) * -1) + 2  # reverse 0/1 values of variable
table(not_Own)
## not_Own
##   0   1 
## 207 193

# Linear model (with binary dummy variable):
mlm_3 <- lm(Balance ~ not_Own, data = cred)
summary(mlm_3)
## 
## Call:
## lm(formula = Balance ~ not_Own, data = cred)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -529.54 -455.35  -60.17  334.71 1489.20 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   529.54      31.99  16.554   <2e-16 ***
## not_Own       -19.73      46.05  -0.429    0.669    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 460.2 on 398 degrees of freedom
## Multiple R-squared:  0.0004611,  Adjusted R-squared:  -0.00205 
## F-statistic: 0.1836 on 1 and 398 DF,  p-value: 0.6685

Interpretation:

The mlm_3 contains the same results as mlm_2, but from the opposite perspective:

  • intercept: Average credit card balance WITH home (i.e., not_Own == 0 baseline): $529.54

  • coefficient: DEcrease by $19.73 in ABSENCE of home ownership (but effect is small and non-significant).

Qualitative predictors with more than two levels

When a qualitative predictor has more than two levels, a single dummy variable cannot represent all possible values. In this situation, we can create additional dummy variables.

Example: For predicting Credit card balance by Region (i.e., a factor variable with 3 levels), we create and use two (binary) dummy variables:

table(cred$Region)  # is a factor with 3 levels (1: East, 2: South, 3: West)
## 
##  East South  West 
##    99   199   102

# Create 2 dummy variables:
cred$r_S <- rep(NA, nrow(cred))
cred$r_S <- ifelse(cred$Region == "South", 1, 0)
table(cred$r_S)
## 
##   0   1 
## 201 199

cred$r_W <- rep(NA, nrow(cred))
cred$r_W <- ifelse(cred$Region == "West", 1, 0)
table(cred$r_W)
## 
##   0   1 
## 298 102

Model mlm_4 (using 2 dummy variables as binary predictors) re-creates the results of Table 3.8 (p. 86): Least squares coefficient estimates associated with the regression of Balance onto region in the Credit data set. The categorical Region variable (with 3 levels) is encoded via two dummy variables.

# Linear model (with 2 binary dummy variables):
mlm_4 <- lm(Balance ~ r_S + r_W, data = cred)
summary(mlm_4)
## 
## Call:
## lm(formula = Balance ~ r_S + r_W, data = cred)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -531.00 -457.08  -63.25  339.25 1480.50 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   531.00      46.32  11.464   <2e-16 ***
## r_S           -12.50      56.68  -0.221    0.826    
## r_W           -18.69      65.02  -0.287    0.774    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 460.9 on 397 degrees of freedom
## Multiple R-squared:  0.0002188,  Adjusted R-squared:  -0.004818 
## F-statistic: 0.04344 on 2 and 397 DF,  p-value: 0.9575

Interpretation:

  • The level with no dummy variable (East in this example) is the baseline. Which level we choose as the baseline is arbitrary, but matters for interpreting the coefficients.

  • The coefficient estimates show that the differences between regions are small and non-significant.

  • Overall, the low \(F\)-value (and its \(p\)-value of 0.96) indicate that we cannot reject the null hypothesis \(H_0\): \(\beta_1 = \beta_2 = 0\) (i.e., Region has no effect on Balance) by this model.

Note that we can achieve the same result with the original categorical variable Region (i.e., a factor with 3 levels):

# Linear model with a factor variable (3-levels):
mlm_5 <- lm(Balance ~ Region, data = cred)
summary(mlm_5)
## 
## Call:
## lm(formula = Balance ~ Region, data = cred)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -531.00 -457.08  -63.25  339.25 1480.50 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)   531.00      46.32  11.464   <2e-16 ***
## RegionSouth   -12.50      56.68  -0.221    0.826    
## RegionWest    -18.69      65.02  -0.287    0.774    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 460.9 on 397 degrees of freedom
## Multiple R-squared:  0.0002188,  Adjusted R-squared:  -0.004818 
## F-statistic: 0.04344 on 2 and 397 DF,  p-value: 0.9575

Overall, using the dummy variable approach presents no difficulties when incorporating both quantitative and qualitative predictors.

Besides the dummy variable approach, there are alternative ways of coding qualitative variables. All of these approaches lead to equivalent model fits, but the coefficients have different values and interpretations, and are designed to measure particular contrasts.

Extensions of the linear model

The standard LR model provides interpretable results and works quite well on many real-world problems. However, its restrictive assumptions are often violated in practice. Two of the most important assumptions state that the effects of the predictors are additive and the relationship between the predictors and response is linear. Here are some classical approaches for extending the linear model:

1. Non-additive relationships

Removing the additive assumption of standard LR models: Including interaction effects.

Example: Two quantitative predictors

Figure 3.5 (p. 81) suggested a non-linear relationship in the Advertising data for a multiple LR with 2 predictors (TV and radio). Such a non-linear pattern often suggests a synergy or interaction effect between both predictors:

# Multiple linear regression model (2 predictors + interaction): ---- 
mlm_6 <- lm(sales ~ TV + radio + TV:radio, data = ad)
mlm_6 <- lm(sales ~ TV * radio, data = ad)  # same model (main effects + interaction)

# Summary mLR:
summary(mlm_6)
## 
## Call:
## lm(formula = sales ~ TV * radio, data = ad)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.3366 -0.4028  0.1831  0.5948  1.5246 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 6.750e+00  2.479e-01  27.233   <2e-16 ***
## TV          1.910e-02  1.504e-03  12.699   <2e-16 ***
## radio       2.886e-02  8.905e-03   3.241   0.0014 ** 
## TV:radio    1.086e-03  5.242e-05  20.727   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.9435 on 196 degrees of freedom
## Multiple R-squared:  0.9678, Adjusted R-squared:  0.9673 
## F-statistic:  1963 on 3 and 196 DF,  p-value: < 2.2e-16

# Without interaction: ---- 
mlm_x <- lm(sales ~ TV + radio, data = ad)
# summary(mlm_x)

Notes:

  • Testing for interactions relaxes the additivity assumption insofar as multiplicative relations are included (see explanation and examples on p. 88).

  • The new \(R^2 = 96.7\%\), whereas it was only \(R^2 = 89.6\%\) without the interaction.
    Thus,68.3% of the remaining variance in sales was explained by including the interaction term.

  • The hierarchical principle: If we include an interaction in a model, we should also include the main effects, even if the \(p\)-values associated with their coefficients are not significant. (In mlm_6, both the two main effects and their interaction are significant.)

Example: One qualitative and one quantitative predictor

An interaction between a qualitative variable and a quantitative variable has a particularly nice interpretation (p. 89 ). Consider the Credit data set from above, and suppose that we wish to predict balance using the income (quantitative) and student (qualitative) variables:

# head(cred)

# (a) Linear model with a quantitative and qualitative predictor (NO interaction):
mlm_7a <- lm(Balance ~ Income + Student, data = cred)
summary(mlm_7a)
## 
## Call:
## lm(formula = Balance ~ Income + Student, data = cred)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -762.37 -331.38  -45.04  323.60  818.28 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 211.1430    32.4572   6.505 2.34e-10 ***
## Income        5.9843     0.5566  10.751  < 2e-16 ***
## StudentYes  382.6705    65.3108   5.859 9.78e-09 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 391.8 on 397 degrees of freedom
## Multiple R-squared:  0.2775, Adjusted R-squared:  0.2738 
## F-statistic: 76.22 on 2 and 397 DF,  p-value: < 2.2e-16

# (b) Linear model with a quantitative and qualitative predictor (WITH interaction):
mlm_7b <- lm(Balance ~ Income + Student + Income:Student, data = cred)
summary(mlm_7b)
## 
## Call:
## lm(formula = Balance ~ Income + Student + Income:Student, data = cred)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -773.39 -325.70  -41.13  321.65  814.04 
## 
## Coefficients:
##                   Estimate Std. Error t value Pr(>|t|)    
## (Intercept)       200.6232    33.6984   5.953 5.79e-09 ***
## Income              6.2182     0.5921  10.502  < 2e-16 ***
## StudentYes        476.6758   104.3512   4.568 6.59e-06 ***
## Income:StudentYes  -1.9992     1.7313  -1.155    0.249    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 391.6 on 396 degrees of freedom
## Multiple R-squared:  0.2799, Adjusted R-squared:  0.2744 
## F-statistic:  51.3 on 3 and 396 DF,  p-value: < 2.2e-16

When expressed graphically, including the interaction results in a more flexible model that allows not only for different intercepts, but also different slopes for both qualitative groups:

Figure 3.7: Including interactions.

Notes:

  • Whereas including main effects allow only for parallel lines with different intercepts (for each level of the qualitative predictor), including interaction term additionally allows for different slopes (i.e., different impacts of the quantitative predictor on different qualitative groups).

  • See derivation of line equations (i.e., values of intercept and slope) from coefficients (James et al., 2021, p. 90)

# Scatterplot: 
plot(x = cred$Income, y = cred$Balance, 
     main = "Balance by income (including interaction with student/no student)", 
     pch = 20, col = ifelse(cred$Student == "Yes", adjustcolor("deepskyblue", .33), adjustcolor("deeppink", .33)),  
     xlab = "Income", ylab = "Balance", xlim = c(0, 200), ylim = c(0, 1600))

# From model:
cf <- coefficients(mlm_7b)

# Line equations (with interaction of categorical predictor): 
l_inc_no_stud <- function(x){ cf[1] + cf[2] * x }
l_inc_student <- function(x){ (cf[1] + cf[3]) + (cf[2] + cf[4]) * x }

curve(expr = l_inc_student, lwd = 2, col = "deepskyblue", add = TRUE)
curve(expr = l_inc_no_stud, lwd = 2, col = "deeppink", add = TRUE)
Recreating Figure 3.7b: 1 quantitative and 1 qualitative predictor with interaction.

Recreating Figure 3.7b: 1 quantitative and 1 qualitative predictor with interaction.

2. Non-linear relationships

Removing the linear assumption of standard LR models: Using polynomial regression.

The LR model assumes a linear relationship between the predictors and response. When the true relationship between the response and the predictors is non-linear, a simple way to allow for incorporating incorporating non-linear associations into a linear model is to include transformed versions of the predictors.

Extending the linear model to accommodate non-linear relationships is known as polynomial regression, as we include polynomial functions of the predictors in a LR model.

Example of polynomial regression: The points in Figure 3.8 (p. 91) seem to have a quadratic shape, suggesting that a model of the form

\(\text{mpg} = \beta_0 + (\beta_1 \cdot \text{hp}) + (\beta_2 \cdot \text{hp}^2) + \epsilon\)

may provide a better fit.

# Data: 
Auto <- ISLR2::Auto

# Variables:
hp  <- Auto$horsepower  # predictor
mpg <- Auto$mpg  # outcome

# Scatterplot:
plot(x = hp, y = mpg,
     main = "Auto data: mpg by horsepower", xlab = "horsepower", ylab = "mpg", 
     pch = 20, cex = 1, col = adjustcolor("black", .25))

# (a) Linear model with one predictor (hp):
mlm_8a <- lm(mpg ~ hp)
summary(mlm_8a)
## 
## Call:
## lm(formula = mpg ~ hp)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -13.5710  -3.2592  -0.3435   2.7630  16.9240 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 39.935861   0.717499   55.66   <2e-16 ***
## hp          -0.157845   0.006446  -24.49   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.906 on 390 degrees of freedom
## Multiple R-squared:  0.6059, Adjusted R-squared:  0.6049 
## F-statistic: 599.7 on 1 and 390 DF,  p-value: < 2.2e-16

abline(mlm_8a, lwd = 2, col = "deepskyblue")

# (b) Polynomial LR model with one predictor and its square (as 2nd predictor):

hp_sq <- hp^2  # squared predictor 
mlm_8b <- lm(mpg ~ hp + hp_sq)

mlm_8b <- lm(mpg ~ hp + I(hp^2))  # Note: I() inhibits the interpretation of ^ 
                                  # (which has special meaning in an R formula)
summary(mlm_8b)
## 
## Call:
## lm(formula = mpg ~ hp + I(hp^2))
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -14.7135  -2.5943  -0.0859   2.2868  15.8961 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 56.9000997  1.8004268   31.60   <2e-16 ***
## hp          -0.4661896  0.0311246  -14.98   <2e-16 ***
## I(hp^2)      0.0012305  0.0001221   10.08   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.374 on 389 degrees of freedom
## Multiple R-squared:  0.6876, Adjusted R-squared:  0.686 
## F-statistic:   428 on 2 and 389 DF,  p-value: < 2.2e-16

curve(expr = coef(mlm_8b)[1] + coef(mlm_8b)[2] * x + coef(mlm_8b)[3] * x^2, 
      lwd = 2, col = "deeppink", add = TRUE)
Figure 3.8

Figure 3.8

Notes:

  • The fit of the polynomial model (mlm_8b) is superior: \(R^2 = 0.686\), compared to \(0.605\) for the linear fit (mlm_08a). Also, the \(p\)-value for the coefficient of the quadratic term is highly significant.

  • In this equation, the value of mpg is a non-linear function of hp. However, the model is still a linear model — a multiple LR model with two predictors \(X_1 = \text{hp}\) and \(X_2 = \text{hp}^2\).

  • We could extend the polynomial LR approach by including terms for \(\text{hp}^3\), \(\text{hp}^4\), etc. Although the resulting curves would provide closer fits, they are also prone to overfitting the model training data.

See Chapter 7 for further non-linear extensions of the linear model (e.g., basis functions, splines, local regression, generalized additive models).

Potential problems for LR

When we fit a linear regression model to a particular data set, the following problems can occur:

  1. Non-linearity of the response-predictor relationships.
  2. Correlation of error terms.
  3. Non-constant variance of error terms.
  4. Outliers.
  5. High-leverage points.
  6. Collinearity.

Detecting and solving these problems in practice is as much an art as a science. A brief summary of some key points:

ad 1. Non-linearity of the response-predictor relationships

A LR model assumes a linear (i.e., straight-line) relationship between predictors and response. If the true relationship is not linear, all conclusions drawn from a linear model are suspect.

Diagnosis:

A useful graphical tool for identifying non-linearity are residual plots:

Figure 3.9: Residual plots for linear vs. quadratic fit.

Re-creating Figure 3.9 (p. 93):

  • Simple LR model: Plotting the residual errors \(e_i = y_i − \hat{y_i}\) as a function of the the predictor \(x_i\):
# Data: 
Auto <- ISLR2::Auto

# Variables:
hp  <- Auto$horsepower  # predictor
mpg <- Auto$mpg  # outcome

# (a) Linear model with one predictor (hp):
mlm_8a <- lm(mpg ~ hp)
# summary(mlm_8a)

# Scatterplot of residuals:
mpg_residuals <- mpg - predict(mlm_8a)
plot(x = hp, y = mpg_residuals,
     main = "Residuals: mpg by horsepower", xlab = "horsepower", ylab = "mpg", 
     pch = 20, cex = 1, col = adjustcolor("black", .25))
abline(0, 0, lty = 2, lwd = .5)

# identify(x = hp, y = mpg_residuals, plot = TRUE)

# Note: Default residual plot in 
# plot(mlm_8a)
  • Polynomial LR model: Plotting residual errors \(e_i = y_i − \hat{y_i}\) vs. the predictor \(x_i\):
# (b) Polynomial LR model with one predictor and its square (as 2nd predictor):
hp_sq <- hp^2  # squared predictor 
mlm_8b <- lm(mpg ~ hp + hp_sq)
# summary(mlm_8b)

# Scatterplot of residuals:
mpg_residuals_2 <- mpg - predict(mlm_8b)
plot(x = hp, y = mpg_residuals_2,
     main = "Residuals: mpg by horsepower + hp^2", xlab = "hp", ylab = "mpg", 
     pch = 20, cex = 1, col = adjustcolor("black", .25))
abline(0, 0, lty = 2, lwd = .5)

# identify(x = hp, y = mpg_residuals_2, plot = TRUE)

# Note: Default residual plot in 
# plot(mlm_8b)
  • Multiple LR model: Plotting the residual errors \(e_i = y_i − \hat{y_i}\) vs. the predicted (or fitted) values .

Interpretation:

  • The presence of a pattern (e.g., U-shape) may indicate a problem with some aspect of the linear model.

  • If the linearity assumption is violated, a simple approach for improving a model can be to use a non-linear transformation of the predictor(s) (e.g., log(\(X\)), \(\sqrt{X}\), or \(X^2\)).

ad 2. Correlation of error terms

A LR model assumes that the error terms \(e_i = y_i − \hat{y_i}\) are uncorrelated. Violating this assumption results in unjustified confidence (e.g., larger confidence intervals).

Examples:

  • If we accidentally used each of \(n\) data points twice (i.e., created a model that considered \(2n\) data points), our confidence intervals would overestimate the truly warranted confidence by a factor of \(\sqrt{2}\).

  • Correlated error terms frequently occur in the context of time series data, which often exhibit positively correlated errors of adjacent data points.

Diagnosis:

Plot the residual errors from a LR model as a function of time (see Figure 3.10, p. 95):

Figure 3.10: Residual plots by time (to diagnose correlated error terms).

Interpretation:

  • When viewing residual errors as a function of time, we should not see any time-related trends or patterns. By contrast, tracking in the residuals (i.e., adjacent residuals having similar signs or values) suggest correlated error terms.

Examples beyond time series:

  • Correlation between errors can occur when other relevant variables influence the relationship between predictor(s) and response variables. For instance, a study may aim to predict individuals’ height from their weight. If clusters in the data (e.g., families, communities, countries) consider individuals on similar diets, their error terms may be correlated. Avoiding such correlations is critical factor of (ensuring the internal validity of an) experimental design.

ad 3. Non-constant variance of error terms

A LR model assumes that error terms have a constant variance \(\text{Var}(\epsilon_i) = \sigma^2\) (aka. heteroscedasticity).

However, variances of the error terms may increase (or decrease) with the value of the response.

Diagnosis:

  • A funnel shape in the residual plot indicates non-constant variances in the errors (heteroscedasticity).

  • A concave transformation of the outcome/response variable \(Y\) (e.g., using \(\text{log}(Y)\) or \(\sqrt{Y}\)) can restore homoscedasticity (see Figure 3.11, p. 96):

Figure 3.11: Funnel shape in residual plot indicating non-constant variance of errors (heteroscedasticity).

  • When the variance of response values \(Y_i\) is known, we can fit a LR model with weighted least squares, with weights proportional to the inverse variances. LR software usually allows for observation weights.

ad 4. Outliers

Definition: An outlier is a data point for which \(y_i\) differs substantially from the value \(\hat{y_i}\) predicted by the model (i.e., the value’s residual error is large). This can — but does not have to — indicate errors (e.g., in measuring or coding the original data) and should be investigated carefully.

Even when the model coefficients (and thus the best-fitting regression line) may change very little, removing outliers may dramatically improve the model fit (by reducing \(RSE\) and increasing \(R^2\)).

Example of Figure 3.12 (p. 97):

Figure 3.12: Impact on outliers on LR model.

Interpretation:

  • Left: The least squares regression line is shown in red, and the regression line after removing the outlier is shown in blue.

  • Center: The residual plot clearly identifies the outlier.

  • Right: The outlier has a studentized residual of \(+6\); typically we expect values between \(−3\) and \(+3\).

Diagnosis:

  • Residual plots can be used to identify outliers. To standardize, we can plot studentized residuals that divide each residual error \(e_i\) by its estimated standard error. Observations whose studentized residuals are greater than \(|3|\) are possible outliers.

ad 5. High-leverage points

Definition: Whereas outliers are observations for which the response \(y_i\) is unusual given the predictor \(x_i\). By contrast, observations with high leverage have an unusual predictor value for \(x_i\).

Example: Observation 41 in the left-hand panel of Figure 3.13 (p. 98) has high leverage, in that the predictor value for this observation is large relative to the other observations:

Figure 3.13: Impact of high leverage values.

Interpretation:

  • Left: Observation 41 is a high leverage point, while 20 is (perhaps) an outlier. The red line is the fit to all the data, and the blue line is the fit with observation 41 removed.

  • Center: The red observation is not unusual in terms of its \(X_1\) value or its \(X_2\) value, but still falls outside the bulk of the data, and hence has high leverage.

  • Right: Observation 41 has a high leverage and a high residual.

Overall, a few high leverage observations can have a sizable impact on the estimated regression line.

Diagnosis:

  • In order to quantify an observation’s leverage, we compute the leverage statistic: If a given observation has a leverage statistic that greatly exceeds \((p + 1)/n\), then we may suspect that the corresponding point has high leverage.

  • In Figure 3.13 (p. 98), observation 41 stands out as having a very high leverage statistic as well as a high studentized residual. In other words, it is an outlier as well as a high leverage observation. By contrast, observation 20 has low leverage (i.e., not much influence on the model/line of best fit).

ad 6. Collinearity

Definition: Collinearity refers to the situation in which two or more predictor variables are closely related to one another.

When two or more predictors are collinear (i.e., related to each other), it is difficult or impossible to separate their individual effects on the response. In other words, when predictors tend to increase or decrease together, their \(\beta\)-weights can fluctuate widely, as we cannot separately measure their relation to the response.

Example: Two scatterplot for predictors in the Credit dataset (without and with collinearity between predictors):

Figure 3.14: Collinearity between predictors.

Interpretation:

  • Left: A plot of age versus limit. These two variables are not collinear.

  • Right: A plot of rating versus limit. There is high collinearity.

Example: Two contour plots of RSS (without and with collinearity between predictors):

Figure 3.15: Contour plots for RSS without and with collinearity between predictors.

Interpretation:

  • Left: A contour plot of RSS for the regression of balance onto age and limit. In the absence of collinearity between predictors, the minimum value is well defined.

  • Right: A contour plot of RSS for the regression of balance onto rating and limit. In the presence of collinearity between predictors, there are many pairs (\(\beta_\text{Limit}\), \(\beta_\text{Rating}\)) with a similar RSS value.

Consequences of collinearity:

  • Collinearity yields a great deal of uncertainty in the coefficient estimates.

  • The standard error of coefficient estimates \(\hat{\beta_j}\) grows, resulting in a declining \(t\)-statistic, and lower power (for rejecting \(H_0\)).

Example: See Table 3.11 (p. 102) for two models without vs. with collinearity.

Diagnosis:

  • To detect collinearity between pairs of predictors, inspect the correlation matrix of the predictors.

  • To detect multicollinearity (i.e., relationships between three or more predictors), compute the variance inflation factor (VIF) for each predictor. VIF values are \(\geq 1\): VIF\((\hat{\beta_j}) \geq 5\) suggest problematic amounts of collinearity.

Remedy:

  1. Drop one or more problematic predictors from the model.

  2. Combine collinear variables into a single predictor (e.g., the average of standardized versions of the individual predictors).

3.4 The marketing plan

This chapter started by introducing the Advertising data. After our overview of LR, we can not address the questions (asked on p. 59f.):

  1. Is there a relationship between predictors (advertising budgets) and outcome (sales)?

Fit a multiple LR model (without interactions):

# Data:
dim(ad)
## [1] 200   5
head(ad)
## # A tibble: 6 × 5
##       X    TV radio newspaper sales
##   <int> <dbl> <dbl>     <dbl> <dbl>
## 1     1 230.   37.8      69.2  22.1
## 2     2  44.5  39.3      45.1  10.4
## 3     3  17.2  45.9      69.3   9.3
## 4     4 152.   41.3      58.5  18.5
## 5     5 181.   10.8      58.4  12.9
## 6     6   8.7  48.9      75     7.2

m_1 <- lm(sales ~ TV + radio + newspaper, data = ad)
summary(m_1)
## 
## Call:
## lm(formula = sales ~ TV + radio + newspaper, data = ad)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -8.8277 -0.8908  0.2418  1.1893  2.8292 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.938889   0.311908   9.422   <2e-16 ***
## TV           0.045765   0.001395  32.809   <2e-16 ***
## radio        0.188530   0.008611  21.893   <2e-16 ***
## newspaper   -0.001037   0.005871  -0.177     0.86    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.686 on 196 degrees of freedom
## Multiple R-squared:  0.8972, Adjusted R-squared:  0.8956 
## F-statistic: 570.3 on 3 and 196 DF,  p-value: < 2.2e-16

Interpretation: Table 3.6 (p. 76): There clearly is a relation between advertising budget (mostly that of TV, radio) and sales.

  1. How strong is the relationship?

Summary of m_1 and Table 3.6 (p. 76): Quite strong (e.g., \(RSE = 1.69\), \(R^2 = 89.6\)%, \(F = 570\)).

  1. Which predictors matter? What are their individual contributions on the overall outcome?

Summary of m_1 and Table 3.4 (p. 74): The \(p\)-values suggest that This suggest only TV and radio are related to sales (see Chapter 6 for details).

  1. How strong is the relationship for each predictor (with or without considering the others)?

Summary of m_1 and Table 3.4 (p. 74) can be used to compute confidence intervals for the predictors: The coefficients for predictors TV and radio exclude zero, whereas that of newspaper is not different from zero.

Is there collinearity between predictors?

  • Re-creating the correlation matrix of Table 3.5 (p. 75):
# Correlation matrix (for predictors):
pr <- ad[ , c(-1, -5)]
round(cor(x = pr), 2)
##             TV radio newspaper
## TV        1.00  0.05      0.06
## radio     0.05  1.00      0.35
## newspaper 0.06  0.35      1.00

Only low to moderate correlation between predictors.

  • Check the variance inflation factor (VIF) for each predictor:
# install.packages(car)  # if car has not been installed yet
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
vif(m_1)
##        TV     radio newspaper 
##  1.004611  1.144952  1.145187

No evidence for collinearity (as VIF values are close to 1, not exceeding 5).

  1. Prediction: How accurately can we predict future outomes?

We can use the model m_1 to predict (or estimate) response values. The accuracy of such estimates depends on whether we wish to predict an individual response \(Y = f(X) + \epsilon\), or the average response \(f(X)\) (see Question 4 of Section 3.2.2, p. 81f.). If the former, we use a prediction interval, and if the latter, we use a confidence interval. Prediction intervals are always be wider than confidence intervals because they account for the uncertainty associated with the irreducible error \(\epsilon\).

  1. Inference: What is the form of the relationship (e.g., linear or non-linear)?

We can use some diagnostic plots:

par(mfrow = c(2, 2))
plot(m_1, pch = 20, col = adjustcolor("black", .25))

par(opar)  # restore original par

Alternatively, we can compute the residuals of m_1 by plotting the predicted values vs. the residuals() function:

plot(predict(m_1), residuals(m_1), 
     main = "Residual plot", pch = 20, col = adjustcolor("black", .25))

The function rstudent() will return the studentized residuals, and we can use this function to plot the residuals against the fitted values:

plot(predict(m_1), rstudent(m_1), 
     main = "Studentized residual plot", pch = 20, col = adjustcolor("black", .25))

Interpretation: On the basis of the residual plots, there is some evidence of non-linearity. This could be addressed by transforming the predictors in the LR model in order to accommodate non-linear relationships (see Section 3.3.2).

Leverage statistics can be computed for any number of predictors using the hatvalues() function:

# Leverage statistics:
plot(hatvalues(m_1), 
     main = "Leverage", pch = 20, col = adjustcolor("black", .25))