Recap

  • Simple linear regression (using lm())
  • Multivariate linear regression (using lm())
  • Presenting estimation results (using modelsummary())
  • Hypothesis testing on regression coefficients (using summary() and qt())

Load Packages

Install/load the tidyverse and modelsummary packages.

library(pacman)
p_load(tidyverse, modelsummary)

Today we will use the mtcars dataset. We will focus on the mpg, wt and am variables.

  • mpg – mpg (miles per gallon) is a determinant of fuel efficiency
  • wt – weight of the vehicle in 1000s of lbs
  • am – a binary variable signaling whether a vehicle has automatic (am=0) or manual (am=1) transmission configuration.
head(mtcars %>% select(mpg, wt, am))
##                    mpg    wt am
## Mazda RX4         21.0 2.620  1
## Mazda RX4 Wag     21.0 2.875  1
## Datsun 710        22.8 2.320  1
## Hornet 4 Drive    21.4 3.215  0
## Hornet Sportabout 18.7 3.440  0
## Valiant           18.1 3.460  0

Log Transformations

At this point, we should be comfortable with interpreting the \(\beta_1\) coefficient in the simple linear regression model when \(Y\) and \(X\) are both in levels:

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

In such a model, \(\beta_1\) represents the unit change in \(Y\) associated with a unit increase in \(X\).

\[ \frac{\Delta Y}{\Delta X} = \beta_1 \]

reg = lm(formula = mpg ~ wt, data = mtcars)
modelsummary(reg)
Model 1
(Intercept) 37.285
(1.878)
wt -5.344
(0.559)
Num.Obs. 32
R2 0.753
R2 Adj. 0.745
AIC 166.0
BIC 170.4
Log.Lik. -80.015
F 91.375
RMSE 3.05

How do we interpret \(\beta_1\) in the following regression, in which the dependent variable is a log transformation of \(Y\)?

\[ \log(Y) = \beta_0 + \beta_1 X + \varepsilon \]

The following is important to remember:

\[ \Delta \log(Y) \approx \frac{\% \Delta Y}{100} \]

We may find that

\[ \frac{\Delta \log(Y)}{\Delta X} = \beta_1 \] and since

\[ \frac{\Delta \log(Y)}{\Delta X} = \frac{\%\Delta Y / 100}{\Delta X} \] then

\[ \beta_1 = \frac{\%\Delta Y / 100}{\Delta X} \]

this implies we may interpret \(100 * \beta_1\) as the percentage change in \(Y\) due to a unit increase in \(X\).

reg = lm(formula = log(mpg) ~ wt, data = mtcars)
modelsummary(reg)
Model 1
(Intercept) 3.832
(0.084)
wt -0.272
(0.025)
Num.Obs. 32
R2 0.798
R2 Adj. 0.791
AIC -32.8
BIC -28.5
Log.Lik. 19.424
F 118.191
RMSE 0.14

How do we interpret \(\beta_1\) in a model in which the dependent variable is in levels but the regressor of interest is log-transformed?

\[ Y = \beta_0 + \beta_1 \log(X) + \varepsilon \]

Recall that \(\frac{d \log(X)}{d X} = \frac{1}{X}\).

Therefore, we may find that

\[ \frac{\partial Y}{\partial X} = \beta_1 \frac{1}{X} \approx \frac{\Delta Y}{\Delta X}\]

Solving for \(\beta_1\) gives us

\[ \beta_1 = \frac{\Delta Y}{\Delta X} X = \frac{\Delta Y}{\Delta X / X } = \frac{\Delta Y}{\% \Delta X / 100} = 100 \frac{\Delta Y}{\%\Delta X}\] In words, this implies that we may interpret \(\beta_1/100\) as representing the change in units of \(Y\) due to a percentage increase in \(X\).

reg = lm(formula = mpg ~ log(wt), data = mtcars)
modelsummary(reg)
Model 1
(Intercept) 39.257
(1.758)
log(wt) -17.086
(1.510)
Num.Obs. 32
R2 0.810
R2 Adj. 0.804
AIC 157.6
BIC 162.0
Log.Lik. -75.794
F 128.016
RMSE 2.67

Lastly, let’s interpret the \(\beta_1\) coefficient in the following regression model, in which both the dependent variable and the regressor are log-transformed:

\[ \log(Y) = \beta_0 + \beta_1 \log(X) + \varepsilon \]

Notice that

\[ \frac{\partial \log(Y)}{\partial X} = \beta_1 \frac{1}{X} \approx \frac{\Delta \log(Y)}{\Delta X} \]

so that

\[ \beta_1 = \frac{\Delta \log(Y)}{\Delta X / X} = \frac{100 \cdot \Delta \log(Y)}{100 \cdot \Delta X / X} = \frac{\% \Delta Y}{\% \Delta X} \].

In words, \(\beta_1\) represents the elasticity of \(Y\) with respect to \(X\) – the percentage change in \(Y\) associated with a one percent increase in \(X\).

reg = lm(formula = log(mpg) ~ log(wt), data = mtcars)
modelsummary(reg)
Model 1
(Intercept) 3.902
(0.088)
log(wt) -0.842
(0.075)
Num.Obs. 32
R2 0.806
R2 Adj. 0.799
AIC -34.2
BIC -29.8
Log.Lik. 20.077
F 124.360
RMSE 0.13

Polynomial Terms

Suppose we have data on variables \(Y\) and \(X\), and want to estimate the effect of \(X\) on \(Y\).

In most cases we would just regress \(Y\) on \(X\) – but what if we suspect the relationship between \(Y\) and \(X\) to be nonlinear?

We may attempt to captures nonlinearities by adding polynomials of \(X\) in the regression model:

\[ Y = \beta_0 + \beta_1 X + \beta_2 X^2 + \varepsilon \]

Notice that in the given model, the effects of variation in \(X\) are captured by both \(\beta_1\) and \(\beta_2\):

\[ \frac{\partial Y}{\partial X} = \beta_1 + 2 \beta_2 X \]

Furthermore, notice that the effect of \(X\) on \(Y\) is a function of \(X\)!

So, for us to make a statement of the usual nature “a unit increase in \(X\) is associated with a change in \(Y\) by … units”, we have to specify an initial/benchmark level of \(X\).

reg = lm(formula = mpg ~ wt + wtsq, data = mtcars %>% mutate(wtsq = wt^2))
modelsummary(reg)
Model 1
(Intercept) 49.931
(4.211)
wt -13.380
(2.514)
wtsq 1.171
(0.359)
Num.Obs. 32
R2 0.819
R2 Adj. 0.807
AIC 158.0
BIC 163.9
Log.Lik. -75.024
F 65.638
RMSE 2.65

Dummies

A dummy is a categorical variable that takes on values of 1 or 0 to denote whether a given observation does or does not possess a certain characteristic.

Dummies are also referred to as binary variables.

Suppose we have data on a continuous variable \(Y\) and a dummy \(D\), and we want to estimate the effect of \(D\) on \(Y\). We estimate the following model:

\[ Y = \beta_0 + \beta_1 D + \varepsilon \]

Notice that when \(D = 0\), we have \(Y = \beta_0 + \varepsilon\), and when \(D = 1\), we have \(Y = \beta_0 + \beta_1 + \varepsilon\).

Therefore,

\[ E[Y \,|\, D = 1] = \beta_0 + \beta_1 \]

and

\[ E[Y \,|\, D = 0] = \beta_0 \]

so that

\[ E[Y \,|\, D = 1] - E[Y \,|\, D = 0] = \beta_1 \, .\]

In other words, \(\beta_1\) captures the difference in the expected value of \(Y\) in the case that \(D = 1\) vs. \(D = 0\) – this is the effect of possessing the characteristic implied by \(D = 1\) (you can think of it as some sort of treatment, if that helps).

reg1 = lm(formula = mpg ~ am, data = mtcars)
modelsummary(reg1)
Model 1
(Intercept) 17.147
(1.125)
am 7.245
(1.764)
Num.Obs. 32
R2 0.360
R2 Adj. 0.338
AIC 196.5
BIC 200.9
Log.Lik. -95.242
F 16.860
RMSE 4.90
reg = lm(formula = log(mpg) ~ am, data = mtcars)
modelsummary(reg) 
Model 1
(Intercept) 2.817
(0.057)
am 0.347
(0.089)
Num.Obs. 32
R2 0.337
R2 Adj. 0.315
AIC 5.1
BIC 9.5
Log.Lik. 0.453
F 15.278
RMSE 0.25
reg2 = lm(formula = mpg ~ am + wt, data = mtcars)
modelsummary(list(reg1, reg2))
Model 1 Model 2
(Intercept) 17.147 37.322
(1.125) (3.055)
am 7.245 -0.024
(1.764) (1.546)
wt -5.353
(0.788)
Num.Obs. 32 32
R2 0.360 0.753
R2 Adj. 0.338 0.736
AIC 196.5 168.0
BIC 200.9 173.9
Log.Lik. -95.242 -80.015
F 16.860 44.165
RMSE 4.90 3.10

Interaction Terms

Suppose we have data on variables \(X\), \(Y\), and \(Z\), and are interested in estimating the effects of \(X\) and \(Z\) on \(Y\).

The simplest model we could estimate in such a case is the following multivariate linear regression model:

\[ Y = \beta_0 + \beta_1 X + \beta_2 Z + \varepsilon \]

In this case \(\beta_1\) represents the effect of \(X\) on \(Y\), and \(\beta_2\) represents the effect of \(Z\) on \(Y\).

But what if we suspect that the level of \(Z\) affects the effect of \(X\) on \(Y\) and/or the level of \(X\) affects the effect of \(Z\) on \(Y\)?

In other words, what if we suspect that \(X\) and \(Z\) interact?

We may follow up on such suspicions by estimating the following model containing an interaction term:

\[ Y = \beta_0 + \beta_1 X + \beta_2 Z + \beta_3 X Z + \varepsilon \]

Notice that now we have

\[ \frac{\Delta Y}{\Delta X} = \beta_1 + \beta_3 Z \]

and

\[ \frac{\Delta Y}{\Delta Z} = \beta_2 + \beta_3 X \]

So, \(\beta_3\) may be interpreted as the effect of \(Z\) on the effect of \(X\) on \(Y\), or equivalently the effect of \(X\) on the effect of \(Z\) on \(Y\):

\[ \beta_3 = \frac{\partial}{\partial Z} \left( \frac{\Delta Y}{\Delta X} \right) = \frac{\partial}{\partial X} \left( \frac{\Delta Y}{\Delta Z} \right) \]

Note that \(X\) and \(Z\) do not necessarily have to be continuous variables – either one or both can be binary!

reg1 = lm(formula = mpg ~ wt + am, data = mtcars)
modelsummary(reg1)
Model 1
(Intercept) 37.322
(3.055)
wt -5.353
(0.788)
am -0.024
(1.546)
Num.Obs. 32
R2 0.753
R2 Adj. 0.736
AIC 168.0
BIC 173.9
Log.Lik. -80.015
F 44.165
RMSE 3.10
reg2 = lm(formula = mpg ~ wt + am + wt*am, data = mtcars)
modelsummary(list(reg1,reg2))
Model 1 Model 2
(Intercept) 37.322 31.416
(3.055) (3.020)
wt -5.353 -3.786
(0.788) (0.786)
am -0.024 14.878
(1.546) (4.264)
wt × am -5.298
(1.445)
Num.Obs. 32 32
R2 0.753 0.833
R2 Adj. 0.736 0.815
AIC 168.0 157.5
BIC 173.9 164.8
Log.Lik. -80.015 -73.738
F 44.165 46.567
RMSE 3.10 2.59