Skip to main content

Section 6.1 One numerical and one categorical explanatory variable

We continue using the UN member states dataset introduced in SectionΒ 5.1. Recall that we studied the relationship between the outcome variable fertility rate, \(y\text{,}\) and the regressor life expectancy, \(x\text{.}\)
In this section, we introduce one additional regressor to this model: the categorical variable income group with four categories: Low income, Lower middle income, Upper middle income, and High income. We now want to study how fertility rate changes due to changes in life expectancy and different income levels. To do this, we use multiple regression. Observe that we now have:
  1. A numerical outcome variable \(y\text{,}\) the fertility rate in a given country or state, and
  2. Two explanatory variables:
    1. A numerical explanatory variable \(x_1\text{,}\) the life expectancy.
    2. A categorical explanatory variable \(x_2\text{,}\) the income group.

Subsection 6.1.1 Exploratory data analysis

The UN member states data frame is included in the moderndive package. To keep things simple, we select() only the subset of the variables needed here, and save this data in a new data frame called UN_data_ch6. Note that the variables used are different than the ones chosen in ChapterΒ 5. We also set the income variable to be a factor so that its levels show up in the expected order.
UN_data_ch6 <- un_member_states_2024 |>
  select(country,
         life_expectancy_2022,
         fertility_rate_2022,
         income_group_2024) |>
  na.omit() |>
  rename(life_exp = life_expectancy_2022,
         fert_rate = fertility_rate_2022,
         income = income_group_2024) |>
  mutate(income = factor(income,
                         levels = c("Low income", "Lower middle income",
                                    "Upper middle income", "High income")))
Recall the three common steps in an exploratory data analysis we saw in SubsectionΒ 5.1.1:
  1. Inspecting a sample of raw values.
  2. Computing summary statistics.
  3. Creating data visualizations.
We first look at the raw data values by either looking at UN_data_ch6 using RStudio’s spreadsheet viewer or by using the glimpse() function from the dplyr package:
glimpse(UN_data_ch6)
Rows: 182
Columns: 4
$ country   <chr> "Afghanistan", "Albania", "Algeria", "Angola", "Antigua and …
$ life_exp  <dbl> 53.65, 79.47, 78.03, 62.11, 77.80, 78.31, 76.13, 83.09, 82.2…
$ fert_rate <dbl> 4.3, 1.4, 2.7, 5.0, 1.6, 1.9, 1.6, 1.6, 1.5, 1.6, 1.4, 1.8, …
$ income    <fct> Low income, Upper middle income, Lower middle income, Lower …
The variable country contains all the UN member states. R reads this variable as character, <chr>, and beyond the country identification it will not be needed for the analysis. The variables life expectancy, life_exp, and fertility rate, fert_rate, are numerical, and the variable income, income, is categorical. In R, categorical variables are called factors and the categories are factor levels.
We also display a random sample of 10 rows of the 182 rows corresponding to different countries. Remember, due to the random nature of the sampling, you will likely end up with a different subset of 10 rows.
UN_data_ch6 |> sample_n(size = 10)
# A tibble: 10 Γ— 4
   country      life_exp fert_rate income             
   <chr>           <dbl>     <dbl> <fct>              
 1 Guinea           63.9       4.1 Lower middle income
 2 Costa Rica       79.6       1.5 Upper middle income
 3 Dominica         78.2       1.6 Upper middle income
 4 Slovenia         81.8       1.6 High income        
 5 Mongolia         71.4       2.7 Lower middle income
 6 Tajikistan       69.4       3.1 Lower middle income
 7 Mauritius        74.9       1.4 Upper middle income
 8 Oman             76.9       2.5 High income        
 9 Cambodia         70.6       2.3 Lower middle income
10 Bahamas, The     76.1       1.4 High income
Life expectancy, life_exp, is an estimate of how many years, on average, a person in a given country is expected to live. Fertility rate, fert_rate, is the average number of live births per woman of childbearing age in a country. As we did in our exploratory data analyses in SubsectionΒ 5.1.1 and SubsectionΒ 5.2.1 from ChapterΒ 5, we find summary statistics:
UN_data_ch6 |>
  select(life_exp, fert_rate, income) |>
  tidy_summary()
# A tibble: 6 Γ— 11
  column        n group         type    min    Q1  mean median    Q3   max    sd
  <chr>     <int> <chr>         <chr> <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl>
1 life_exp    182 <NA>          nume…  53.6  69.4 73.7    75.2  78.4  86.4  6.86
2 fert_rate   182 <NA>          nume…   0.9   1.6  2.49    2     3.2   6.6  1.16
3 income       25 Low income    fact…  NA    NA   NA      NA    NA    NA   NA   
4 income       52 Lower middle… fact…  NA    NA   NA      NA    NA    NA   NA   
5 income       49 Upper middle… fact…  NA    NA   NA      NA    NA    NA   NA   
6 income       56 High income   fact…  NA    NA   NA      NA    NA    NA   NA
Recall that each row in UN_data_ch6 represents a particular country or UN member state. The tidy_summary() function shows a summary for the numerical variables life expectancy (life_exp), fertility rate (fert_rate), and the categorical variable income group (income). When the variable is numerical, the tidy_summary() function provides the total number of observations in the data frame, the five-number summary, the mean, and the standard deviation.
For example, the first row of our summary refers to life expectancy as life_exp. There are 182 observations for this variable, it is a numerical variable, and the first quartile, Q1, is 69.4; this means that the life expectancy of 25% of the UN member states is less than 69.4 years. When a variable in the dataset is categorical, also called a factor, the summary shows all the categories or factor levels and the number of observations for each level. For example, income group (income) is a factor with four levels: Low income, Lower middle income, Upper middle income, and High income. The summary also provides the number of states for each factor level; observe, for example, that the dataset has 56 UN member states that are considered High income states.
Furthermore, we can compute the correlation coefficient between our two numerical variables: life_exp and fert_rate. Recall from SubsectionΒ 5.1.1 that correlation coefficients only exist between numerical variables. We observe that they are β€œstrongly negatively” correlated.
UN_data_ch6 |>
  get_correlation(formula = fert_rate ~ life_exp)
# A tibble: 1 Γ— 1
     cor
   <dbl>
1 -0.815
We are ready to create data visualizations, the last of our exploratory data analysis. Given that the outcome variable fert_rate and explanatory variable life_exp are both numerical, we can create a scatterplot to display their relationship, as we did in FigureΒ 5.1.2. But this time, we incorporate the categorical variable income by mapping this variable to the color aesthetic, thereby creating a colored scatterplot.
ggplot(UN_data_ch6, aes(x = life_exp, y = fert_rate, color = income)) +
  geom_point() +
  labs(x = "Life Expectancy", y = "Fertility Rate", color = "Income group") +
  geom_smooth(method = "lm", se = FALSE)
Scatterplot of life expectancy vs fertility rate with points and regression lines colored by income group (Low income, Lower middle income, Upper middle income, High income). All four lines have negative slopes, with the High income group having a less steep slope than the others.
Figure 6.1.1. Colored scatterplot of life expectancy and fertility rate by income group.
In FigureΒ 6.1.1, observe that ggplot() assigns a default color scheme to the points and to the lines associated with the four levels of income: Low income, Lower middle income, Upper middle income, and High income. Furthermore, the geom_smooth(method = "lm", se = FALSE) layer automatically fits a different regression line for each group.
We can see some interesting trends. First, observe that we get a different line for each income group. Second, the slopes for all the income groups are negative. Third, the slope for the High income group is clearly less steep than the slopes for all other three groups. So, the changes in fertility rate due to changes in life expectancy are dependent on the level of income of a given country. Fourth, observe that high-income countries have, in general, high life expectancy and low fertility rates.

Subsection 6.1.2 Model with interactions

We can represent the four regression lines in FigureΒ 6.1.1 as a multiple regression model with interactions.
Before we do this, however, we review a linear regression with only one categorical explanatory variable. Recall in SubsectionΒ 5.2.2 we fit a regression model for each country life expectancy as a function of the corresponding continent. We produce the corresponding analysis here, now using the fertility rate as the response variable and the income group as the categorical explanatory variable. We’ll use slightly different notation to what was done previously to make the model more general.
A linear model with a categorical explanatory variable is called a one-factor model where factor refers to the categorical explanatory variable and the categories are also called factor levels. We represent the categories using indicator functions or dummy variables. In our UN data example, the variable income has four categories or levels: Low income, Lower middle income, Upper middle income, and High income. The corresponding dummy variables needed are:
\begin{equation*} D_1 = \left\{ \begin{array}{ll} 1 \amp \text{if the UN member state has low income} \\ 0 \amp \text{otherwise} \end{array} \right. \end{equation*}
\begin{equation*} D_2 = \left\{ \begin{array}{ll} 1 \amp \text{if the UN member state has lower middle income} \\ 0 \amp \text{otherwise} \end{array} \right. \end{equation*}
\begin{equation*} D_3 = \left\{ \begin{array}{ll} 1 \amp \text{if the UN member state has upper middle income} \\ 0 \amp \text{otherwise} \end{array} \right. \end{equation*}
\begin{equation*} D_4 = \left\{ \begin{array}{ll} 1 \amp \text{if the UN member state has high income} \\ 0 \amp \text{otherwise} \end{array} \right. \end{equation*}
So, for example, if a given UN member state has Low income, its dummy variables are \(D_1 = 1\) and \(D_2 = D_3 = D_4 = 0\text{.}\) Similarly, if another UN member state has Upper middle income, then its dummy variables would be \(D_1 = D_2 = D_4 = 0\) and \(D_3 = 1\text{.}\) Using dummy variables, the mathematical formulation of the linear regression for our example is:
\begin{equation*} \hat y = \widehat{\text{fert rate}} = b_0 + b_2 D_2 + b_3 D_3 + b_4 D_4 \end{equation*}
or if we want to express it in terms of the \(i\)th observation in our dataset, we can include the \(i\)th subscript:
\begin{equation*} \hat y_i = \widehat{\text{fert rate}} = b_0 + b_2 D_{2i} + b_3 D_{3i} + b_4 D_{4i} \end{equation*}
Recall that the coefficient \(b_0\) represents the intercept and the coefficients \(b_2\text{,}\) \(b_3\text{,}\) and \(b_4\) are the offsets based on the appropriate category. The dummy variables, \(D_2\text{,}\) \(D_3\text{,}\) and \(D_4\text{,}\) take the values of zero or one depending on the corresponding category of any given country. Observe also that \(D_1\) does not appear in the model. The reason for this is entirely mathematical: if the model would contain an intercept and all the dummy variables, the model would be over-specified, that is, it would contain one redundant explanatory variable. The solution is to drop one of the variables. We keep the intercept because it provides flexibility when interpreting more complicated models, and we drop one of the dummy variables which, by default in R, is the first dummy variable, \(D_1\text{.}\) This does not mean that we are losing information of the first level \(D_1\text{.}\) If a country is part of the Low income level, \(D_1 = 1\text{,}\) \(D_2 = D_3 = D_4 = 0\text{,}\) so most of the terms in the regression are zero and the linear regression becomes:
\begin{equation*} \hat y = \widehat{\text{fert rate}} = b_0 \end{equation*}
So the intercept represents the average fertility rate when the country is a Low income country. Similarly, if another country is part of the Upper middle income level, then \(D_1 = D_2 = D_4 = 0\) and \(D_3 = 1\) so the linear regression becomes:
\begin{equation*} \hat y = \widehat{\text{fert rate}} = b_0 + b_3 \end{equation*}
The average fertility rate for a Upper middle income country is \(b_0 + b_3\text{.}\) Observe that \(b_3\) is an offset for fertility rate between the baseline level and the Upper middle income level. The same logic applies to the model for each possible income category.
We calculate the regression coefficients using the lm() function and the command coef() to retrieve the coefficients of the linear regression:
one_factor_model <- lm(fert_rate ~ income, data = UN_data_ch6)
coef(one_factor_model)
(Intercept) incomeLower middle income incomeUpper middle income 
                 4.280000                 -1.299231                 -2.247347 
        incomeHigh income 
                -2.653214
We present these results in a table with the mathematical notation used above:
Table 6.1.2. Regression coefficients for the one-factor model
Coefficients Values
\(b_0\) 4.280
\(b_2\) -1.299
\(b_3\) -2.247
\(b_4\) -2.653
The first level, Low income, is the β€œbaseline” group. The average fertility rate for Low income UN member states is \(b_0 = 4.280\text{.}\) Similarly, the average fertility rate for Upper middle income member states is \(b_0 + b_3 = 4.280 + (-2.247) = 2.033\text{.}\)
We are now ready to study the multiple linear regression model with interactions shown in FigureΒ 6.1.1. In this figure we can identify three different effects. First, for any fixed level of life expectancy, observe that there are four different fertility rates. They represent the effect of the categorical explanatory variable, income. Second, for any given regression line, the slope represents the change in average fertility rate due to changes on life expectancy. This is the effect of the numerical explanatory variable life_exp. Third, observe that the slope of the line depends on the income level; as an illustration, observe that for High income member states the slope is less steep than for Low income member states. When the slope changes due to changes in the explanatory variable, we call this an interaction effect.
The mathematical formulation of the linear regression model with two explanatory variables, one numerical and one categorical, and interactions is:
\begin{align*} \widehat{y} = \widehat{\text{fert rate}} = b_0 \amp+ b_{02}D_2 + b_{03}D_3 + b_{04}D_4\\ \amp+ b_1x\\ \amp+ b_{12}xD_2 + b_{13}xD_3 + b_{14}xD_4 \end{align*}
The linear regression shows how the average fertility rate is affected by the categorical variable, the numerical variable, and the interaction effects. There are eight coefficients in our model and we have separated their coefficients into three lines to highlight their different roles. The first line shows the intercept and the effects of the categorical explanatory variables. Recall that \(D_2\text{,}\) \(D_3\text{,}\) and \(D_4\) are the dummy variables in the model and each is equal to one or zero depending on the category of the country at hand; correspondingly, the coefficients \(b_{02}\text{,}\) \(b_{03}\text{,}\) and \(b_{04}\) are the offsets with respect to the baseline level of the intercept, \(b_0\text{.}\) Recall that the first dummy variable has been dropped and the intercept captures this effect. The second line in the equation represents the effect of the numerical variable, \(x\text{.}\) In our example \(x\) is the value of life expectancy. The coefficient \(b_1\) is the slope of the line and represents the change in fertility rate due to one unit change in life expectancy. The third line in the equation represents the interaction effects on the slopes. Observe that they are a combination of life expectancy, \(x\text{,}\) and income level, \(D_2\text{,}\) \(D_3\text{,}\) and \(D_4\text{.}\) What these interaction effects do is to modify the slope for different levels of income. For a Low income member state, the dummy variables are \(D_1 = 1\text{,}\) \(D_2 = D_3 = D_4 = 0\) and our linear regression is:
\begin{align*} \widehat{y} = \widehat{\text{fert rate}} \amp= b_0 + b_{02}\cdot 0 + b_{03}\cdot 0 + b_{04}\cdot 0 + b_1x + b_{12}x\cdot 0 + b_{13}x\cdot 0 + b_{14}x\cdot 0\\ \amp= b_0 + b_1x \end{align*}
Similarly, for a High income member state, the dummy variables are \(D_1 = D_2 = D_3 = 0\text{,}\) and \(D_4 = 1\text{.}\) We take into account the offsets for the intercept, \(b_{04}\text{,}\) and the slope, \(b_{14}\text{,}\) and the linear regression becomes:
\begin{align*} \widehat{y} = \widehat{\text{fert rate}} \amp= b_0 + b_{02}\cdot 0 + b_{03}\cdot 0 + b_{04}\cdot 1 + b_1 x + b_{12}x\cdot 0 + b_{13}x\cdot 0 + b_{14}x\cdot 1\\ \amp= b_0 + b_{04} + b_1x + b_{14}x\\ \amp= (b_0 + b_{04}) + (b_1 + b_{14})\cdot x \end{align*}
Observe how the intercept and the slope are different for a High income member state when compared to the baseline Low income member state. As an illustration, we construct this multiple linear regression for the UN member state dataset in R. We first β€œfit” the model using the lm() β€œlinear model” function and then find the coefficients using the function coef(). In R, the formula used is y ~ x1 + x2 + x1:x2 where x1 and x2 are the variable names in the dataset and represent the main effects while x1:x2 is the interaction term. For simplicity, we can also write y ~ x1 * x2 as the * sign accounts for both main effects and interaction effects. R would let both x1 and x2 be either explanatory or numerical, and we need to make sure the dataset format is appropriate for the regression we want to run. Here is the code for our example:
# Fit regression model and get the coefficients of the model
model_int <- lm(fert_rate ~ life_exp * income, data = UN_data_ch6)
coef(model_int)
(Intercept)                           life_exp 
                       11.91826460                        -0.11848075 
         incomeLower middle income          incomeUpper middle income 
                       -1.50420617                        -1.89291757 
                 incomeHigh income life_exp:incomeLower middle income 
                       -6.57953685                         0.01265027 
life_exp:incomeUpper middle income         life_exp:incomeHigh income 
                        0.01117481                         0.07221696
We present these results in a table with the mathematical notation:
Table 6.1.3. Regression coefficients for the interaction model
Coefficients Values
\(b_0\) 11.918
\(b_{02}\) -1.504
\(b_{03}\) -1.893
\(b_{04}\) -6.580
\(b_1\) -0.118
\(b_{12}\) 0.013
\(b_{13}\) 0.011
\(b_{14}\) 0.072
We can match the coefficients with the values computed in TableΒ 6.1.3: the fitted fertility rate \(\widehat{y} = \widehat{\text{fert rate}}\) for Low income countries is
\begin{equation*} \widehat{\text{fert rate}} = b_0 + b_1\cdot x = 11.92 + (-0.12)\cdot x, \end{equation*}
which is the equation of the regression line in FigureΒ 6.1.1 for low income countries. The regression has an intercept of \(11.92\) and a slope of \(-0.12\text{.}\) Since life expectancy is greater than zero for all countries, the intercept has no practical interpretation and we only need it to produce the most appropriate line. The interpretation of the slope is: for Low income countries, every additional year of life expectancy reduces the average fertility rate by \(0.12\) units.
As discussed earlier, the intercept and slope for all the other income groups are determined by taking into account the appropriate offsets. For example, for High income countries \(D_4 = 1\) and all other dummy variables are equal to zero. The regression line becomes
\begin{equation*} \widehat{y} = \widehat{\text{fert rate}} = b_0 + b_1x + b_{04} + b_{14}x = (b_0 + b_{04}) + (b_1 + b_{14})x \end{equation*}
where \(x\) is life expectancy, life_exp. The intercept is (Intercept) + incomeHigh income:
\begin{equation*} b_0 + b_{04} = 11.92 + (-6.58) = 5.34, \end{equation*}
and the slope for these High income countries is life_exp + life_exp:incomeHigh income corresponding to
\begin{equation*} b_1 + b_{14} = -0.12 + 0.07 = -0.05 \end{equation*}
For High income countries, every additional year of life expectancy reduces the average fertility rate by \(0.05\) units. The intercepts and slopes for other income levels are calculated similarly.
Since the life expectancy for Low income countries has a steeper slope than High income countries, one additional year of life expectancy will decrease fertility rates more for the low-income group than for the high-income group. This is consistent with our observation from FigureΒ 6.1.1. When the associated effect of one variable depends on the value of another variable we say that there is an interaction effect. This is the reason why the regression slopes are different for different income groups.

Exercises Exercises

1.
What is the goal of including an interaction term in a multiple regression model?
  1. To create more variables for analysis.
  2. To account for the effect of one explanatory variable on the response while considering the influence of another explanatory variable.
  3. To make the model more complex without any real benefit.
  4. To automatically improve the fit of the regression line.
Answer.
B. It lets the effect of one explanatory variable depend on the level of another.
2.
How does the inclusion of both main effects and interaction terms in a regression model affect the interpretation of individual coefficients?
  1. They represent simple marginal effects.
  2. They become meaningless.
  3. They are conditional effects, depending on the level of the interacting variables.
  4. They are interpreted in the same way as in models without interactions.
Answer.
C. Coefficients become conditional effects that depend on the interacting variable(s).
3.
Which statement about the use of dummy variables in regression models is correct?
  1. Dummy variables are used to represent numerical variables.
  2. Dummy variables are used to represent categorical variables with at least two levels.
  3. Dummy variables always decrease the R-squared value.
  4. Dummy variables are unnecessary in regression models.
Answer.
B. They encode categorical variables with at least two levels.

Subsection 6.1.3 Model without interactions

We can simplify the previous model by removing the interaction effects. The model still represents different income groups with different regression lines by allowing different intercepts but all the lines have the same slope: they are parallel as shown in FigureΒ 6.1.4.
To plot parallel slopes we use the function geom_parallel_slopes() that is included in the moderndive package. To use this function you need to load both the ggplot2 and moderndive packages. Observe how the code is identical to the one used for the model with interactions in FigureΒ 6.1.1, but now the geom_smooth(method = "lm", se = FALSE) layer is replaced with geom_parallel_slopes(se = FALSE).
ggplot(UN_data_ch6, aes(x = life_exp, y = fert_rate, color = income)) +
  geom_point() +
  labs(x = "Life expectancy", y = "Fertility rate", color = "Income group") +
  geom_parallel_slopes(se = FALSE)
Scatterplot of life expectancy vs fertility rate with parallel (equal-slope) regression lines for each income group. All four lines have the same negative slope but different intercepts, with Low income countries having the highest intercept.
Figure 6.1.4. Parallel slopes model of fertility rate with life expectancy and income group.
The regression lines for each income group are shown in FigureΒ 6.1.4. Observe that the lines are now parallel: they all have the same negative slope. The interpretation of this result is that the change in fertility rate due to changes in life expectancy in a given country are the same regardless of the income group of this country.
On the other hand, any two regression lines in FigureΒ 6.1.4 have different intercepts representing the income group; in particular, observe that for any fixed level of life expectancy the fertility rate is greater for Low income and Lower middle income countries than for Upper middle income and High income countries.
The mathematical formulation of the linear regression model with two explanatory variables, one numerical and one categorical, and without interactions is:
\begin{equation*} \widehat{y} = b_0 + b_{02}D_2 + b_{03}D_3 + b_{04}D_4 + b_1x. \end{equation*}
Observe that the dummy variables only affect the intercept now, and the slope is fully described by \(b_1\) for any income group. In the UN data example, a High income country, with \(D_4 = 1\) and the other dummy variables equal to zero, will be represented by
\begin{equation*} \widehat{y} = (b_0 + b_{04}) + b_1x. \end{equation*}
To find the coefficients for this regression in R, the formula used is y ~ x1 + x2 where x1 and x2 are the variable names in the dataset and represent the main effects. Observe that the term x1:x2 representing the interaction is no longer included. R would let both x1 and x2 to be either explanatory or numerical; therefore, we should always check that the variable format is appropriate for the regression we want to run. Here is the code for the UN data example:
# Fit regression model:
model_no_int <- lm(fert_rate ~ life_exp + income, data = UN_data_ch6)

# Get the coefficients of the model
coef(model_no_int)
We present these results in a table with the mathematical notation:
Table 6.1.5. Regression table for a model without interactions
Coefficients Values
\(b_0\) 10.768
\(b_{02}\) -0.719
\(b_{03}\) -1.239
\(b_{04}\) -1.067
\(b_1\) -0.101
In this model without interactions presented in TableΒ 6.1.5, the slope is the same for all the regression lines, \(b_1 = -0.101\text{.}\) Assuming that this model is correct, for any UN member state, every additional year of life expectancy reduces the average fertility rate by \(0.101\) units, regardless of the income level of the member state. The intercept of the regression line for Low income member states is \(10.768\) while for High income member states is \(10.768 + (-1.067) = 9.701\text{.}\) The intercepts for other income levels can be determined similarly. We compare the visualizations for both models side-by-side in FigureΒ 6.1.6.
Two side-by-side scatterplots comparing the interaction model (with different slopes for each income group) and the parallel slopes model (with equal slopes for all income groups).
Figure 6.1.6. Comparison of interaction model (left) and parallel slopes model (right) for fertility rate with life expectancy and income group.
Which one is the preferred model? Looking at the scatterplot and the clusters of points in FigureΒ 6.1.6, it does appear that lines with different slopes capture better the behavior of different groups of points. The lines do not appear to be parallel and the interaction model seems more appropriate.

Exercises Exercises

1.
How should a model with one categorical regressor and one numerical regressor, but no interactions, be interpreted?
  1. The slope of the model for each category is different.
  2. The slope of the model for each category is the same.
  3. There is no relationship between the categorical regressor and the response.
  4. There is no relationship between the numerical regressor and the response.
Answer.
B. All categories share the same slope; only intercepts differ.

Subsection 6.1.4 Observed responses, fitted values, and residuals

In this subsection, we work with the regression model with interactions. The coefficients for this model were found earlier, saved in model_int, and are displayed in TableΒ 6.1.7:
Table 6.1.7. Regression table for interaction model
Coefficients Values
(Intercept) \(b_0\) 11.918
incomeLower middle income \(b_{02}\) -1.504
incomeUpper middle income \(b_{03}\) -1.893
incomeHigh income \(b_{04}\) -6.580
life_exp \(b_1\) -0.118
incomeLower middle income:life_exp \(b_{12}\) 0.013
incomeUpper middle income:life_exp \(b_{13}\) 0.011
incomeHigh income:life_exp \(b_{14}\) 0.072
We can use these coefficients to find the fitted values and residuals for any given observation. As an illustration, we chose two observations from the UN member states dataset, provided the values for the explanatory variables and response, as well as the fitted values and residuals:
Table 6.1.8. Fitted values and residuals for two example countries
ID fert_rate income life_exp fert_rate_hat residual
1 1.3 High income 79.7 1.65 -0.35
2 5.7 Low income 62.4 4.52 1.18
The first observation is a High income country with a life expectancy of 79.74 years and an observed fertility rate equal to 1.3. The second observation is a Low income country with a life expectancy of 62.41 years and an observed fertility rate equal to 5.7. The fitted value, fert_rate_hat, is the estimated value of the response determined by the regression line. This value is computed by using the values of the explanatory variables and the coefficients of the linear regression. In addition, recall the difference between the observed response value and the fitted value, \(y - \hat y\text{,}\) is called the residual.
We illustrate this in FigureΒ 6.1.9. The vertical line on the left represents the life expectancy value for the Low income country. The y-value for the large dot on the regression line that intersects the vertical line is the fitted value for fertility rate, \(\widehat y\text{,}\) and the y-value for the large dot above the line is the observed fertility rate, \(y\text{.}\) The difference between these values, \(y - \widehat y\text{,}\) is called the residual and in this case is positive. Similarly, the vertical line on the right represents the life expectancy value for the High income country; the y-value for the large dot on the regression line is the fitted fertility rate. The observed y-value for fertility rate is below the regression line making the residual negative.
Scatterplot showing the interaction model regression lines with two specific countries highlighted. Dashed vertical lines connect each country’s life expectancy to the regression line; the observed points show residuals as the difference between observed and fitted fertility rates.
Figure 6.1.9. Fitted values for two countries: one High income and one Low income, showing the interaction model regression lines.
We can generalize the study of fitted values and residuals for all the countries in the UN_data_ch6 dataset, as shown in TableΒ 6.1.10:
regression_points <- get_regression_points(model_int)
regression_points
Table 6.1.10. Regression points (First 10 out of 182 countries)
ID fert_rate life_exp income fert_rate_hat residual
1 4.3 53.6 Low income 5.56 -1.26
2 1.4 79.5 Upper middle income 1.50 -0.098
3 2.7 78.0 Lower middle income 2.16 0.544
4 5.0 62.1 Lower middle income 3.84 1.16
5 1.6 77.8 High income 1.74 -0.139
6 1.9 78.3 Upper middle income 1.62 0.278
7 1.6 76.1 Upper middle income 1.86 -0.256
8 1.6 83.1 High income 1.50 0.105
9 1.5 82.3 High income 1.53 -0.033
10 1.6 74.2 Upper middle income 2.07 -0.469

Exercises Exercises

1.
Compute the observed response values, fitted values, and residuals for the model without interactions.
Answer.
Use the fitted no-interaction model and generate regression points:
model_no_int <- lm(fert_rate ~ life_exp + income, data = UN_data_ch6)
pts_no_int <- get_regression_points(model_no_int)
# Columns include fert_rate (observed y), fert_rate_hat (fitted y-hat), and residual = y - y-hat
head(pts_no_int)
# A tibble: 6 Γ— 6
     ID fert_rate life_exp income              fert_rate_hat residual
  <int>     <dbl>    <dbl> <fct>                       <dbl>    <dbl>
1     1       4.3     53.6 Low income                   5.37   -1.07 
2     2       1.4     79.5 Upper middle income          1.53   -0.131
3     3       2.7     78.0 Lower middle income          2.20    0.503
4     4       5       62.1 Lower middle income          3.80    1.20 
5     5       1.6     77.8 High income                  1.87   -0.272
6     6       1.9     78.3 Upper middle income          1.65    0.252
Interpretation: for each row, compare fert_rate to fert_rate_hat; the residual is their difference.
2.
What is the main benefit of visualizing the fitted values and residuals of a multiple regression model?
  1. To find errors in the dataset.
  2. To check the assumptions of the regression model, such as linearity and homoscedasticity.
  3. To always improve the model’s accuracy.
  4. To increase the complexity of the model.
Answer.
B. To assess model assumptions like linearity and constant variance.