We now consider regression models with two numerical explanatory variables. To illustrate this situation we explore the ISLR2 R package for the first time in this book using its Credit dataset. This dataset contains simulated information for 400 customers. For the regression model we use the credit card balance (Balance) as the response variable; and the credit limit (Limit), and the income (Income) as the numerical explanatory variables.
We load the Credit data frame and to ensure the type of behavior we have become accustomed to in using the tidyverse, we also convert this data frame to be a tibble using as_tibble(). We construct a new data frame credit_ch6 with only the variables needed. We do this by using the select() verb as we did in SubsectionΒ 3.8.1 and, in addition, we save the selecting variables with different names: Balance becomes debt, Limit becomes credit_limit, and Income becomes income:
Furthermore, we present a random sample of five out of the 400 credit card holders in TableΒ 6.2.1. As observed before, each time you run this code, a different subset of five rows is given.
Note that income is in thousands of dollars while debt and credit limit are in dollars. We can also compute summary statistics using the tidy_summary() function. We only select() the columns of interest for our model, and the results are shown in TableΒ 6.2.2:
The mean and median credit card debt are $520.0 and $459.5, respectively. The first quartile for debt is 68.8; this means that 25% of card holders had debts of $68.80 or less. Correspondingly, the mean and median credit card limit, credit_limit, are around $4,736 and $4,622, respectively. Note also that the third quartile of income is 57.5; so 75% of card holders had incomes below $57,500.
We visualize the relationship of the response variable with each of the two explanatory variables using this R code. These plots are shown in FigureΒ 6.2.3.
ggplot(credit_ch6, aes(x = credit_limit, y = debt)) +
geom_point() +
labs(x = "Credit limit (in $)", y = "Credit card debt (in $)",
title = "Debt and credit limit") +
geom_smooth(method = "lm", se = FALSE)
ggplot(credit_ch6, aes(x = income, y = debt)) +
geom_point() +
labs(x = "Income (in $1000)", y = "Credit card debt (in $)",
title = "Debt and income") +
geom_smooth(method = "lm", se = FALSE)
Figure6.2.3.Relationship between credit card debt and credit limit/income.
The left plot in FigureΒ 6.2.3 shows a positive and linear association between credit limit and credit card debt: as credit limit increases so does credit card debt. Observe also that many customers have no credit card debt and there is a cluster of points at the credit card debt value of zero. The right plot in FigureΒ 6.2.3 shows also positive and somewhat linear association between income and credit card debt, but this association seems weaker and actually appears positive only for incomes larger than $50,000. For lower income values it is not clear there is any association at all.
Since variables debt, credit_limit, and income are numerical, and more importantly, the associations between the response and explanatory variables appear to be linear or close to linear, we can also calculate the correlation coefficient between any two of these variables. Recall that the correlation coefficient is appropriate if the association between the variables is linear. One way to do this is using the get_correlation() command as seen in SubsectionΒ 5.1.1, once for each explanatory variable with the response debt:
# A tibble: 1 Γ 1
cor
<dbl>
1 0.862
# A tibble: 1 Γ 1
cor
<dbl>
1 0.464
Alternatively, using the select() verb and command cor() we can find all correlations simultaneously by returning the correlation matrix in TableΒ 6.2.4. This matrix shows the correlation coefficient for any pair of variables in the appropriate row/column combination.
The correlation between debt and credit_limit is 0.862. This indicates a strong and positive linear relationship: the greater the credit limit is, the larger is the credit card debt, on average.
The correlation between debt and income is 0.464. The linear relationship is positive albeit somewhat weak. In other words, higher income is only weakly associated with higher debt.
A useful property of the correlation coefficient is that it is invariant to linear transformations; this means that the correlation between two variables, \(x\) and \(y\text{,}\) will be the same as the correlation between \((a\cdot x + b)\) and \(y\) for any constants \(a\) and \(b\text{.}\) To illustrate this, observe that the correlation coefficient between income in thousands of dollars and credit card debt was 0.464. If we now find the correlation of income in dollars, by multiplying income by 1000, and credit card debt we get:
We return to our exploratory data analysis of the multiple regression. The plots in FigureΒ 6.2.3 correspond to the response and each of the explanatory variables separately. In FigureΒ 6.2.5 we show a 3-dimensional (3D) scatterplot representing the joint relationship of all three variables simultaneously. Each of the 400 observations in the credit_ch6 data frame are marked with a blue point where
In addition, FigureΒ 6.2.5 includes a regression plane. Recall from SubsectionΒ 5.3.2 that the linear regression with one numerical explanatory variable selects the βbest-fittingβ line: the line that minimizes the sum of squared residuals. When linear regression is performed with two numerical explanatory variables, the solution is a βbest-fittingβ plane: the plane that minimizes the sum of squared residuals. Visit this website to open an interactive version of this plot in your browser.
Conduct a new exploratory data analysis with the same outcome variable \(y\)debt but with credit_rating and age as the new explanatory variables \(x_1\) and \(x_2\text{.}\) What can you say about the relationship between a credit card holderβs debt and their credit rating and age?
Patterns to look for: higher credit ratings often associate with higher debt; age can show a weak or nonlinear association. Your plots and correlations should guide the final statement.
Subsection6.2.2Multiple regression with two numerical regressors
As shown in FigureΒ 6.2.5, the linear regression with two numerical regressors produces the βbest-fittingβ plane. We start with a model with no interactions for the two numerical explanatory variables income and credit_limit. In R we consider a model fit with a formula of the form y ~ x1 + x2. We retrieve the regression coefficients using the lm() function and the command coef() to get the coefficients of the linear regression. The regression coefficients are shown in what follows.
Letβs interpret the coefficients. The intercept value is $-385.179. If the range of values that the regressors could take include a credit_limit of $0 and an income of $0, the intercept would represent the average credit card debt for an individual with those levels of credit_limit and income. This is not the case in our data and the intercept has no practical interpretation; it is mainly used to determine where the plane should cut the \(y\)-intercept to produce the smallest sum of squared residuals.
Each slope in a multiple linear regression is considered a partial slope and represents the marginal or additional contribution of a regressor when it is added to a model that already contains other regressors. This partial slope is typically different than the slope we may find in a simple linear regression for the same regressor. The reason is that, typically, regressors are correlated, so when one regressor is part of a model, indirectly itβs also explaining part of the other regressor. When the second regressor is added to the model, it helps explain only changes in the response that were not already accounted for by the first regressor. For example, the slope for credit_limit is $0.264. Keeping income fixed to some value, for an additional increase of credit limit by one dollar the credit debt increases, on average, by $0.264. Similarly, the slope of income is $-7.663. Keeping credit_limit fixed to some level, for a one unit increase of income ($1000 in actual income), there is an associated decrease of $7.66 in credit card debt, on average.
To illustrate the role of partial slopes further, observe that the right plot in FigureΒ 6.2.3 shows the relationship between debt and income in isolation, a positive relationship, so the slope of income is positive. We can determine the value of this slope by constructing a simple linear regression using income as the only regressor:
The regression line is given by the following with the coefficients denoted using the prime (\('\)) designation since they are different values than what we saw previously:
where \(x_2\) is income. By contrast, when credit_limit and income are considered jointly to explain changes in debt, the equation for the multiple linear regression was:
So the slope for income in a simple linear regression is 6.048, and the slope for income in a multiple linear regression is \(-7.663\text{.}\) As surprising as these results may appear at first, they are perfectly valid and consistent as the slope of a simple linear regression has a different role than the partial slope of a multiple linear regression. The latter is the additional effect of income on debt when credit_limit has already been taken into account.
Fit a new simple linear regression using lm(debt ~ credit_rating + age, data = credit_ch6) where credit_rating and age are the new numerical explanatory variables \(x_1\) and \(x_2\text{.}\) Get information about the βbest-fittingβ regression plane from the regression table by finding the coefficient of the model. How do the regression results match up with the results from your previous exploratory data analysis?
Interpretation: the coefficient on credit_rating is the partial effect holding age fixed; same for age. If EDA showed debt increasing as rating increases, expect a positive coefficient on credit_rating. If age showed a weak relationship, expect a small coefficient.
Subsection6.2.3Observed/fitted values and residuals
As shown in SubsectionΒ 6.1.4 for the UN member states example, we find the fitted values and residuals for our credit card debt regression model. The fitted values for the credit card debt (\(\widehat{\text{debt}}\)) are computed using the equation for the regression plane:
where \(x_1\) is credit_limit and \(x_2\) is income. The residuals are the difference between the observed credit card debt and the fitted credit card debt, \(y - \widehat y\text{,}\) for each observation in the data set. In R, we find the fitted values, debt_hat, and residuals, residual, using the get_regression_points() function. In TableΒ 6.2.8, we present the first 10 rows. Remember that the coordinates of each of the points in our 3D scatterplot in FigureΒ 6.2.5 can be found in the income, credit_limit, and debt columns.