library(marginaleffects)
mod <- lm(mpg ~ hp + wt, data = mtcars)
avg_slopes(mod)
#>
#> Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
#> hp -0.0318 0.00903 -3.52 <0.001 11.2 -0.0495 -0.0141
#> wt -3.8778 0.63273 -6.13 <0.001 30.1 -5.1180 -2.6377
#>
#> Type: response
#> Comparison: dY/dX
#> Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
avg_slopes(mod, slope = "eyex")
#>
#> Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
#> hp -0.285 0.0855 -3.34 <0.001 10.2 -0.453 -0.118
#> wt -0.746 0.1418 -5.26 <0.001 22.7 -1.024 -0.468
#>
#> Type: response
#> Comparison: eY/eX
#> Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
avg_slopes(mod, slope = "eydx")
#>
#> Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
#> hp -0.00173 0.000502 -3.46 <0.001 10.8 -0.00272 -0.000751
#> wt -0.21165 0.037849 -5.59 <0.001 25.4 -0.28583 -0.137464
#>
#> Type: response
#> Comparison: eY/dX
#> Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
avg_slopes(mod, slope = "dyex")
#>
#> Term Estimate Std. Error z Pr(>|z|) S 2.5 % 97.5 %
#> hp -4.66 1.32 -3.52 <0.001 11.2 -7.26 -2.06
#> wt -12.48 2.04 -6.13 <0.001 30.1 -16.47 -8.49
#>
#> Type: response
#> Comparison: dY/eX
#> Columns: term, contrast, estimate, std.error, statistic, p.value, s.value, conf.low, conf.high
20 Elasticity
In some contexts, it is useful to interpret the results of a regression model in terms of elasticity or semi-elasticity. One strategy to achieve that is to estimate a log-log or a semilog model, where the left and/or right-hand side variables are logged. Another approach is to note that \(\frac{\partial ln(x)}{\partial x}=\frac{1}{x}\), and to post-process the marginal effects to transform them into elasticities or semi-elasticities.
For example, say we estimate a linear model of this form:
\[y = \beta_0 + \beta_1 x_1 + \beta_2 x_2 + \varepsilon\]
Let \(\hat{y}\) be the adjusted prediction made by the model for some combination of covariates \(x_1\) and \(x_2\). The slope with respect to \(x_1\) (or “marginal effect”) is:
\[\frac{\partial \hat{y}}{\partial x_1}\]
We can estimate the “eyex”, “eydx”, and “dyex” (semi-)elasticities with respect to \(x_1\) as follows:
\[\eta_1=\frac{\partial \hat{y}}{\partial x_1}\cdot \frac{x_1}{\hat{y}}\] \[\eta_2=\frac{\partial \hat{y}}{\partial x_1}\cdot \frac{1}{\hat{y}}\] \[\eta_3=\frac{\partial \hat{y}}{\partial x_1}\cdot x_1\]
with interpretations roughly as follows:
- A percentage point increase in \(x_1\) is associated to a \(\eta_1\) percentage points increase in \(y\).
- A unit increase in \(x_1\) is associated to a \(\eta_2\) percentage points increase in \(y\).
- A percentage point increase in \(x_1\) is associated to a \(\eta_3\) units increase in \(y\).
For further intuition, consider the ratio of change in \(y\) to change in \(x\): \(\frac{\Delta y}{\Delta x}\). We can turn this ratio into a ratio between relative changes by dividing both the numerator and the denominator: \(\frac{\frac{\Delta y}{y}}{\frac{\Delta x}{x}}\). This is of course linked to the expression for the \(\eta_1\) elasticity above.
With the marginaleffects
package, these quantities are easy to compute: