class: center, middle, inverse, title-slide # All Things Regression ## Part II ### Fernando Hoces la Guardia ### 07/21/2022 --- <style type="text/css"> .remark-slide-content { font-size: 30px; padding: 1em 1em 1em 1em; } </style> <style type="text/css"> @media print { .has-continuation { display: block !important; } } </style> # Housekeeping - Let's choose the chapter for the summary (still due Friday 5pm on gradescope) - Practice questions are up. Midterm will follow similar questions (but not exactly the same ones). - Goal: if you understood the concepts behind the practice questions, you will do well in the midterm. - Switching the order of the review session: will do a review on Monday (before the midterm), and on Wednesday we will start with new material. Bring questions! (I will not bring new material, if we finish early we can watch the first part of Run Lola Run) --- # Regression Journey - Regression as Matching on Groups. Ch2 of MM up to page 68 (not included). - Regression as Line Fitting and Conditional Expectation. Ch2 of MM, Appendix. - Multiple Regression and Omitted Variable Bias. Ch2 of MM pages 68-79 and Appendix. - All Things Regression: Anatomy, Inference, Logarithms, Binary Outcomes, and `\(R^2\)`. Ch2 of MM, Appendix + others. --- # Regression Journey - Regression as Matching on Groups. Ch2 of MM up to page 68 (not included). - Regression as Line Fitting and Conditional Expectation. Ch2 of MM, Appendix. - Multiple Regression and Omitted Variable Bias. Ch2 of MM pages 68-79 and Appendix. - **All Things Regression: Anatomy, Inference, Logarithms, Binary Outcomes, and `\(R^2\)`. Ch2 of MM, Appendix + others.** --- # Today and Tomorrow's Lecture - Regression Anatomy - Regression Inference - ** `\(R^2\)` ** - Non-linearities: - Logarithms - Others - Binary Outcomes --- # Analysis of Variance * Remember that `\(Y_i = \widehat{Y_i} + e_i\)`. * We have the following decomposition $$ `\begin{aligned} Var(Y) &= Var( \widehat Y + e) \\ &= Var(\widehat Y ) + Var(e) + 2 Cov(\widehat Y,e) \\ &= Var(\widehat Y ) + Var(e) \end{aligned}` $$ * __Total variation (SST) = Model explained (SSE) + Unexplained (SSR)__ * Because: * `\(Var(x+y) = Var(x) + Var(y) + 2Cov(x,y)\)` * `\(Cov(\hat{Y},e)=0\)` --- # Goodness of Fit .font90[ * The __ `\(R^2\)` __ measures how well the __model fits the data__. ] -- .font90[ $$ `\begin{equation} R^2 = \frac{\text{variance explained}}{\text{total variance}} = \frac{SSE}{SST} = 1 - \frac{SSR}{SST}\in[0,1] \end{equation}` $$ ] -- .font90[ * `\(R^2\)` close to `\(1\)` indicates a __very ***high*** explanatory power__ of the model. * `\(R^2\)` close to `\(0\)` indicates a __very ***low*** explanatory power__ of the model. ] -- .font90[ * *Interpretation:* an `\(R^2\)` of 0.5, for example, means that the variation in `\(x\)` "explains" 50% of the variation in `\(y\)`. ] -- .font90[ * ⚠️ Low `\(R^2\)` does __NOT__ mean it's a useless model! Remember that econometrics is interested in causal mechanisms, not prediction! ] -- .font90[ * ⚠️ The `\(R^2\)` is __NOT__ an indicator of whether a relationship is causal! ] --- class: inverse, middle # Non-linearities --- # Non-linearities `$$(\widehat{\text{Life Expectancy})_i} = 53.96 + 8\times 10^{-4} \cdot \text{GDP}_i$$` <img src="17_all_things_reg2_files/figure-html/unnamed-chunk-4-1.svg" style="display: block; margin: auto;" /> --- # Nonlinear Relationships Erroneus critique of regression: "many economic relationships are **nonlinear** (*e.g.*, most production functions, profit, diminishing marginal utility, tax revenue as a function of the tax rate, *etc.*), hence fitting straight lines is a bad way of estimating such relationships" -- **The flexibility of regression** OLS estimation can accommodate many, but not all, nonlinear relationships. - Underlying model must be linear-in-parameters. - Nonlinear transformations of variables are okay. - We have already seen the most nonlinear set of variables that you can imagine (binaries!) --- # Linearity .hi-green[Linear-in-parameters:] .green[Parameters] enter model as a weighted sum, where the weights are functions of the variables. - This is the one required to estimate OLS .hi-pink[Linear-in-variables:] .pink[Variables] enter the model as a weighted sum, where the weights are functions of the parameters. - This is the one the critique was pointing at. OLS works perfectly here. -- The standard linear regression model satisfies both properties: `$$Y_i = \beta_0 + \beta_1X_{1i} + \beta_2X_{2i} + \dots + \beta_kX_{ki} + e_i$$` --- # Linearity Which of the following is .hi-green[linear-in-parameters], .hi-pink[linear-in-variables], or .hi-purple[neither]? 1. `\(Y_i = \beta_0 + \beta_1X_{i} + \beta_2X_{i}^2 + \dots + \beta_kX_{i}^k + e_i\)` 2. `\(Y_i = \beta_0X_i^{\beta_1}e_i\)` 3. `\(Y_i = \beta_0 + \beta_1\beta_2X_{i} + e_i\)` --- count: false # Linearity Which of the following is .hi-green[linear-in-parameters], .hi-pink[linear-in-variables], or .hi-purple[neither]? 1. `\(\color{#007935}{Y_i = \beta_0 + \beta_1X_{i} + \beta_2X_{i}^2 + \dots + \beta_kX_{i}^k + e_i}\)` 2. `\(Y_i = \beta_0X_i^{\beta_1}e_i\)` 3. `\(Y_i = \beta_0 + \beta_1\beta_2X_{i} + e_i\)` Model 1 is .green[linear-in-parameters], but not linear-in-variables. --- count: false # Linearity Which of the following is .hi-green[linear-in-parameters], .hi-pink[linear-in-variables], or .hi-purple[neither]? 1. `\(\color{#007935}{Y_i = \beta_0 + \beta_1X_{i} + \beta_2X_{i}^2 + \dots + \beta_kX_{i}^k + e_i}\)` 2. `\(\color{#9370DB}{Y_i = \beta_0X_i^{\beta_1}e_i}\)` 3. `\(Y_i = \beta_0 + \beta_1\beta_2X_{i} + e_i\)` Model 1 is .green[linear-in-parameters], but not linear-in-variables. Model 2 is .purple[neither]. --- count: false # Linearity Which of the following is .hi-green[linear-in-parameters], .hi-pink[linear-in-variables], or .hi-purple[neither]? 1. `\(\color{#007935}{Y_i = \beta_0 + \beta_1X_{i} + \beta_2X_{i}^2 + \dots + \beta_kX_{i}^k + e_i}\)` 2. `\(\color{#9370DB}{Y_i = \beta_0X_i^{\beta_1}e_i}\)` 3. `\(\color{#e64173}{Y_i = \beta_0 + \beta_1\beta_2X_{i} + e_i}\)` Model 1 is .green[linear-in-parameters], but not linear-in-variables. Model 2 is .purple[neither]. Model 3 is .pink[linear-in-variables], but not linear-in-parameters. --- # We're Going to Take Logs The natural log is the inverse function for the exponential function: <br> `\(\quad \log(e^x) = x\)` for `\(x>0\)`. ## (Natural) Log Rules and Approximations 1. Product rule: `\(\log(AB) = \log(A) + \log(B)\)`. 2. Quotient rule: `\(\log(A/B) = \log(A) - \log(B)\)`. 3. Power rule: `\(\log(A^B) = B \cdot \log(A)\)`. 4. `\(\log(e) = 1\)`, `\(\log(1) = 0\)`, and `\(\log(x)\)` is undefined for `\(x \leq 0\)`. 5. **Approximation: `\(\log(1 + A) = A\)` If A is very small (~less than 0.2)** --- # Log-Linear Model .font80[ **Nonlinear Model** `$$Y_i = \alpha e^{\beta_1 X_i}e_i$$` - `\(Y > 0\)`, `\(X\)` is continuous, and `\(e_i\)` is a multiplicative error term. - Cannot estimate parameters with OLS directly. **Logarithmic Transformation** `$$\log(Y_i) = \log(\alpha) + \beta_1 X_i + \log(e_i)$$` - Redefine `\(\log(\alpha) \equiv \beta_0\)` and `\(\log(e_i) \equiv e_i\)`. **Transformed (Linear) Model** `$$\log(Y_i) = \beta_0 + \beta_1 X_i + e_i$$` - *Can* estimate with OLS, but coefficient interpretation changes. ] --- # Log-Linear Model `$$\log(Y_i) = \beta_0 + \beta_1 X_i + e_i$$` **Interpretation** - A one-unit increase in the explanatory variable increases the outcome variable by approximately `\(\beta_1\times 100\)` percent, on average. - *Example:* If `\(\log(\hat{\text{Pay}_i}) = 2.9 + 0.03 \cdot \text{School}_i\)`, then an additional year of schooling increases pay by approximately 3 percent, on average. --- count:false # Why? .font80[ .pull-left[ - We want to know how to interpret what is the associated increase in `\(Y\)`, when we increase `\(X\)` in one unit. $$ `\begin{aligned} \log(Y_i) &= \beta_0 + \beta_1 X_i + e_i\\ \widetilde \log(Y_i) &= \beta_0 + \beta_1 (X_i+1) + e_i \end{aligned}` $$ ] ] --- count:false # Why? .font80[ .pull-left[ - We want to know how to interpret what is the associated increase in `\(Y\)`, when we increase `\(X\)` in one unit. $$ `\begin{aligned} \log(Y_i) &= \beta_0 + \beta_1 X_i + e_i\\ \widetilde \log(Y_i) &= \beta_0 + \beta_1 (X_i+1) + e_i\\ \widetilde \log(Y_i) - \log(Y_i) &= \beta_0 + \beta_1 X_i+ \beta_1 + e_i - \\ & \quad (\beta_0 + \beta_1 X_i + e_i)\\ \widetilde \log(Y_i) - \log(Y_i) &= \beta_1 \end{aligned}` $$ ] .pull-right[ .center[**Comparing log(1+X) with X**] <img src="17_all_things_reg2_files/figure-html/unnamed-chunk-7-1.svg" style="display: block; margin: auto;" /> ] ] --- # Why? .font80[ .pull-left[ - We want to know how to interpret what is the associated increase in `\(Y\)`, when we increase `\(X\)` in one unit. $$ `\begin{aligned} \log(Y_i) &= \beta_0 + \beta_1 X_i + e_i\\ \widetilde \log(Y_i) &= \beta_0 + \beta_1 (X_i+1) + e_i\\ \widetilde \log(Y_i) - \log(Y_i) &= \beta_0 + \beta_1 X_i+ \beta_1 + e_i - \\ & \quad (\beta_0 + \beta_1 X_i + e_i)\\ \widetilde \log(Y_i) - \log(Y_i) &= \beta_1\\ \widetilde \log(Y_i) &= \log(Y_i) + \beta_1\\ \widetilde \log(Y_i) &\approx \log(Y_i) + log(1+\beta_1) \\ \widetilde \log(Y_i) &\approx \log(Y_i(1+\beta_1))\\ \widetilde Y_i &\approx Y_i(1+\beta_1) \end{aligned}` $$ ] .pull-right[ .center[**Comparing log(1+X) with X**] <img src="17_all_things_reg2_files/figure-html/unnamed-chunk-9-1.svg" style="display: block; margin: auto;" /> - A one-unit increase in the explanatory variable increases the outcome variable by approximately `\(\beta_1\times 100\)` percent, on average. - What if `\(\beta_1\)` is large (>0.2)? No problem, just divide X by 10, 100, or larger, to shrink the units of `\(\beta_1\)`. ] ] --- # (If X is Binary and `\(\beta > 0.2\)`: Use Exact) .font80[ $$ `\begin{aligned} \log(Y_i) &= \beta_0 + \beta_1 X_i + e_i\\ \widetilde \log(Y_i) &= \beta_0 + \beta_1 (X_i+1) + e_i\\ \widetilde \log(Y_i) - \log(Y_i) &= \beta_0 + \beta_1 X_i+ \beta_1 + e_i - \\ & \quad (\beta_0 + \beta_1 X_i + e_i) \end{aligned}` $$ .pull-left[ .center[Exact] $$ `\begin{aligned} \widetilde \log(Y_i) - \log(Y_i) &= \beta_1\\ \log(\widetilde Y_i / Y_i) &= \beta_1\\ \widetilde Y_i / Y_i &= e^{\beta_1}\\ (\widetilde Y_i - Y_i)/ Y_i &= e^{\beta_1} - 1 \text{ From } X = 0 \text{ to }X = 1\\ (\widetilde Y_i - Y_i)/ Y_i &= e^{-\beta_1} - 1 \text{ From } X = 1 \text{ to }X = 0 \end{aligned}` $$ ] .pull-right[ .center[Approximation] $$ `\begin{aligned} \widetilde \log(Y_i) &= \log(Y_i) + \beta_1\\ \widetilde \log(Y_i) &\approx \log(Y_i) + log(1+\beta_1) \\ \widetilde \log(Y_i) &\approx \log(Y_i(1+\beta_1))\\ \widetilde Y_i &\approx Y_i(1+\beta_1) \end{aligned}` $$ ] - If we cannot re-scale `\(X\)` to have a small `\(\beta\)` we need to compute the percentage difference using the exact formula (left). Also, interpretation from 1 to 0 does not work well in approximation. ] --- # Log-Linear Example `$$\log(\hat{Y_i}) = 10.02 + 0.73 \cdot \text{X}_i$$` <img src="17_all_things_reg2_files/figure-html/log linear plot-1.svg" style="display: block; margin: auto;" /> --- count: false # Log-Linear Example `$$\log(\hat{Y_i}) = 10.02 + 0.73 \cdot \text{X}_i$$` <img src="17_all_things_reg2_files/figure-html/log linear plot 2-1.svg" style="display: block; margin: auto;" /> --- # Log-Log Model .font90[ **Nonlinear Model** `$$Y_i = \alpha X_i^{\beta_1}e_i$$` - `\(Y > 0\)`, `\(X > 0\)`, and `\(e_i\)` is a multiplicative error term. - Cannot estimate parameters with OLS directly. ] -- .font90[ **Logarithmic Transformation** `$$\log(Y_i) = \log(\alpha) + \beta_1 \log(X_i) + \log(e_i)$$` - Redefine `\(\log(\alpha) \equiv \beta_0\)` and `\(\log(e_i) \equiv e_i\)`. ] -- .font90[ **Transformed (Linear) Model** `$$\log(Y_i) = \beta_0 + \beta_1 \log(X_i) + e_i$$` - *Can* estimate with OLS, but coefficient interpretation changes. ] --- # Log-Log Model **Regression Model** $$ \log(Y_i) = \beta_0 + \beta_1 \log(X_i) + e_i $$ **Interpretation** - A one-percent increase in the explanatory variable leads to a `\(\beta_1\)`-percent change in the outcome variable, on average. - This is the definition of an elasticity in economics `\((\Delta\%Q/\Delta\%P)\)` - *Example:* If `\(\log(\widehat{\text{Quantity Demanded}}_i) = 0.45 - 0.31 \cdot \log(\text{Income}_i)\)`, then each one-percent increase in income decreases quantity demanded by 0.31 percent. --- # Why? .font90[ - We want to know how to interpret what is the associated increase in `\(Y\)`, when we increase `\(X\)` in **1 percent unit** (differnent from before). $$ `\begin{aligned} \log(Y_i) = \beta_0 + \beta_1 \log(X_i) + e_i\\ \widetilde \log(Y_i) = \beta_0 + \beta_1 \log(X_i\times 1.01) + e_i\\ \widetilde \log(Y_i) - \log(Y_i) &= \beta_0 + \beta_1 X_i+ \beta_1\log(1.01) + e_i - \\ & \quad (\beta_0 + \beta_1 X_i + e_i)\\ \widetilde \log(Y_i) - \log(Y_i) &= \beta_1 \log(1.01)\\ \widetilde \log(Y_i) &= \log(Y_i) + \beta_1 \log(1.01)\\ \widetilde \log(Y_i) &\approx \log(Y_i) + \beta_1\times 0.01 \\ \widetilde \log(Y_i) &\approx \log(Y_i) + \log(1 + \beta_1/100) \\ \widetilde \log(Y_i) &\approx \log(Y_i(1+\beta_1/100)) \end{aligned}` $$ A one-percent increase in `\(X\)` leads to a `\(\beta_1\)`-percent increase in `\(Y\)`. ] --- # Log-Log Example `$$\log(\hat{Y_i}) = 0.01 + 2.99 \cdot \log(\text{X}_i)$$` <img src="17_all_things_reg2_files/figure-html/log log plot-1.svg" style="display: block; margin: auto;" /> --- count: false # Log-Log Example `$$\log(\hat{Y_i}) = 0.01 + 2.99 \cdot \log(\text{X}_i)$$` <img src="17_all_things_reg2_files/figure-html/log log plot 2-1.svg" style="display: block; margin: auto;" /> --- # Linear-Log Model .font90[ **Nonlinear Model** `$$e^{Y_i} = \alpha X_i^{\beta_1}e_i$$` - `\(X > 0\)` and `\(e_i\)` is a multiplicative error term. - Cannot estimate parameters with OLS directly. ] -- .font90[ **Logarithmic Transformation** `$$Y_i = \log(\alpha) + \beta_1 \log(X_i) + \log(e_i)$$` - Redefine `\(\log(\alpha) \equiv \beta_0\)` and `\(\log(e_i) \equiv e_i\)`. ] -- .font90[ **Transformed (Linear) Model** `$$Y_i = \beta_0 + \beta_1 \log(X_i) + e_i$$` - *Can* estimate with OLS, but coefficient interpretation changes. ] --- # Linear-Log Model **Regression Model** `$$Y_i = \beta_0 + \beta_1 \log(X_i) + e_i$$` **Interpretation** - A one-percent increase in the explanatory variable increases the outcome variable by approximately `\(\beta_1 \div 100\)`, on average. - *Example:* If `\(\hat{(\text{Blood Pressure})_i} = 150 - 9.1 \log(\text{Income}_i)\)`, then a one-percent increase in income decrease blood pressure by 0.091 points. --- # Linear-Log Example `$$\hat{Y_i} = 0 + 0.99 \cdot \log(\text{X}_i)$$` <img src="17_all_things_reg2_files/figure-html/linear log plot-1.svg" style="display: block; margin: auto;" /> --- count: false # Linear-Log Example `$$\hat{Y_i} = 0 + 0.99 \cdot \log(\text{X}_i)$$` <img src="17_all_things_reg2_files/figure-html/linear log plot 2-1.svg" style="display: block; margin: auto;" /> --- #(Approximate) Coefficient Interpretation .font90[ <table> <thead> <tr> <th style="text-align:left;"> Model </th> <th style="text-align:left;"> \(\beta_1\) Interpretation </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;font-weight: bold;font-style: italic;color: black !important;vertical-align:top;"> Level-level <br> \(Y_i = \beta_0 + \beta_1 X_i + e_i\) </td> <td style="text-align:left;font-style: italic;color: black !important;"> \(\Delta Y = \beta_1 \cdot \Delta X\) <br> A one-unit increase in \(X\) leads to a \(\beta_1\)-unit increase in \(Y\) </td> </tr> <tr> <td style="text-align:left;font-weight: bold;font-style: italic;color: black !important;vertical-align:top;"> Log-level <br> \(\log(Y_i) = \beta_0 + \beta_1 X_i + e_i\) </td> <td style="text-align:left;font-style: italic;color: black !important;"> \(\%\Delta Y = 100 \cdot \beta_1 \cdot \Delta X\) <br> A one-unit increase in \(X\) leads to a \(\beta_1 \cdot 100\%\) increase in \(Y\) </td> </tr> <tr> <td style="text-align:left;font-weight: bold;font-style: italic;color: black !important;vertical-align:top;"> Log-log <br> \(\log(Y_i) = \beta_0 + \beta_1 \log(X_i) + e_i\) </td> <td style="text-align:left;font-style: italic;color: black !important;"> \(\%\Delta Y = \beta_1 \cdot \%\Delta X\) <br> A one-percent increase in \(X\) leads to a \(\beta_1\%\) increase in \(Y\) </td> </tr> <tr> <td style="text-align:left;font-weight: bold;font-style: italic;color: black !important;vertical-align:top;"> Level-log <br> \(Y_i = \beta_0 + \beta_1 \log(X_i) + e_i\) </td> <td style="text-align:left;font-style: italic;color: black !important;"> \(\Delta Y = (\beta_1 \div 100) \cdot \%\Delta X\) <br> A one-percent increase in \(X\) leads to a \(\beta_1 \div 100\)-unit increase in \(Y\) </td> </tr> </tbody> </table> ] --- # Can We Do Better? `$$(\widehat{\text{Life Expectancy})_i} = 53.96 + 8\times 10^{-4} \cdot \text{GDP}_i \quad\quad R^2 = 0.34$$` <img src="17_all_things_reg2_files/figure-html/unnamed-chunk-14-1.svg" style="display: block; margin: auto;" /> --- # Can We Do Better? `$$\log( \widehat{\text{Life Expectancy}_i}) = 3.97 + 1.3\times 10^{-5} \cdot \text{GDP}_i \quad\quad R^2 = 0.3$$` <img src="17_all_things_reg2_files/figure-html/unnamed-chunk-16-1.svg" style="display: block; margin: auto;" /> --- # Can We Do Better? `$$\log ( \widehat{\text{Life Expectancy}_i} ) = 2.86 + 0.15 \cdot \log \left( \text{GDP}_i \right) \quad\quad R^2 = 0.61$$` <img src="17_all_things_reg2_files/figure-html/unnamed-chunk-18-1.svg" style="display: block; margin: auto;" /> --- # Can We Do Better? `$$( \widehat{\text{Life Expectancy}})_i = -9.1 + 8.41 \cdot \log \left( \text{GDP}_i \right) \quad\quad R^2 = 0.65$$` <img src="17_all_things_reg2_files/figure-html/unnamed-chunk-20-1.svg" style="display: block; margin: auto;" /> --- # Practical Considerations .font80[ **Consideration 1:** Do your data take negative numbers or zeros as values? ```r log(0) ``` ``` #> [1] -Inf ``` **Consideration 2:** What coefficient interpretation do you want? Unit change? Unit-free percent change? **Consideration 3:** Are your data skewed? .pull-left[ <img src="17_all_things_reg2_files/figure-html/skew 1-1.svg" style="display: block; margin: auto;" /> ] .pull-right[ <img src="17_all_things_reg2_files/figure-html/skew 2-1.svg" style="display: block; margin: auto;" /> ] ] --- # .font90[Final Message: Allways Plot Your Data (Anscombe's Quartet)] .center[**Four *"identical"* regressions:** Intercept .mono[=] 3, Slope .mono[=] 0.5, R.super[2] .mono[=] 0.67] <img src="17_all_things_reg2_files/figure-html/unnamed-chunk-22-1.svg" style="display: block; margin: auto;" /> --- # Other Non-linear Relationships - Binary dependent variable - Interactions (covered later in the course) - Polynomial regressors (not covered) --- # Binary Dependent Variable .font80[ - Previously, introductory courses spent significant time arguing that binary dependent outcomes invalidated regression. - The two main reasons were: 1. This is a highly non-linear relationship (draw plot) 2. The errors in this context have a variance that is correlated with the Xs (heteroskedasticity). - The approach we follow here does not focus on spending much time addressing this concerns. Because - Even if its non-linear, the CEF property #2 says that regression will find the best linear approximation. The key is to choose regressors well (in this case a collection of dummies probably will work better than a single slope). - We now use robust standard errors pretty much all the time. - Regression in this context takes the name Linear Probability Model (the other methods not covered here are Logit and Probit estimation). ] --- class: inverse, middle # (Polynomials Terms in a Regression) ## (Not Covered, but leaving it here in case you are interested. Requires a little knowing the derivative of polynomials) ## (Will mark each of the non-covered slides with an [NC]) --- # Quadratic (and other Polynomial) Relationships [NC] <img src="17_all_things_reg2_files/figure-html/quad plot-1.svg" style="display: block; margin: auto;" /> --- # Quadratic Regression [NC] .font80[ **Regression Model** `$$Y_i = \beta_0 + \beta_1 X_i + \beta_2 X_i^2 + e_i$$` **Interpretation** Sign of `\(\beta_2\)` indicates whether the relationship is convex (.mono[+]) or concave (.mono[-]) Sign of `\(\beta_1\)`? 🤷 Partial derivative of `\(Y\)` with respect to `\(X\)` is the .hi[marginal effect] of `\(X\)` on `\(Y\)`: `$$\color{#e64173}{\dfrac{\partial Y}{\partial X} = \beta_1 + 2 \beta_2 X}$$` - Effect of `\(X\)` depends on the level of `\(X\)` ] --- # Quadratic Regression [NC] ```r lm(y ~ x + I(x^2), data = quad_df) %>% tidy() ``` ``` #> # A tibble: 3 × 5 #> term estimate std.error statistic p.value #> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 (Intercept) 13.2 2.26 5.81 8.30e- 9 #> 2 x 15.7 1.03 15.3 1.99e- 47 #> 3 I(x^2) -2.50 0.0982 -25.4 2.46e-110 ``` .pink[What is the marginal effect of] `\(\color{#e64173}{X}\)` .pink[on] `\(\color{#e64173}{Y}\)`.pink[?] -- <br> `\(\widehat{\dfrac{\partial \text{Y}}{\partial \text{X}} } = \hat{\beta}_1 + 2\hat{\beta}_2 X = 15.69 + -4.99X\)` --- # Quadratic Regression [NC] ```r lm(y ~ x + I(x^2), data = quad_df) %>% tidy() ``` ``` #> # A tibble: 3 × 5 #> term estimate std.error statistic p.value #> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 (Intercept) 13.2 2.26 5.81 8.30e- 9 #> 2 x 15.7 1.03 15.3 1.99e- 47 #> 3 I(x^2) -2.50 0.0982 -25.4 2.46e-110 ``` .pink[What is the marginal effect of] `\(\color{#e64173}{X}\)` .pink[on] `\(\color{#e64173}{Y}\)` .pink[when] `\(\color{#e64173}{X=0}\)`.pink[?] -- <br> `\(\widehat{\dfrac{\partial \text{Y}}{\partial \text{X}} }\Bigg|_{\small \text{X}=0} = \hat{\beta}_1 = 15.69\)` --- # Quadratic Regression [NC] ```r lm(y ~ x + I(x^2), data = quad_df) %>% tidy() ``` ``` #> # A tibble: 3 × 5 #> term estimate std.error statistic p.value #> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 (Intercept) 13.2 2.26 5.81 8.30e- 9 #> 2 x 15.7 1.03 15.3 1.99e- 47 #> 3 I(x^2) -2.50 0.0982 -25.4 2.46e-110 ``` .pink[What is the marginal effect of] `\(\color{#e64173}{X}\)` .pink[on] `\(\color{#e64173}{Y}\)` .pink[when] `\(\color{#e64173}{X=2}\)`.pink[?] -- <br> `\(\widehat{\dfrac{\partial \text{Y}}{\partial \text{X}} }\Bigg|_{\small \text{X}=2} = \hat{\beta}_1 + 2\hat{\beta}_2 \cdot (2) = 15.69 -9.99 = 5.71\)` --- # Quadratic Regression [NC] ```r lm(y ~ x + I(x^2), data = quad_df) %>% tidy() ``` ``` #> # A tibble: 3 × 5 #> term estimate std.error statistic p.value #> <chr> <dbl> <dbl> <dbl> <dbl> #> 1 (Intercept) 13.2 2.26 5.81 8.30e- 9 #> 2 x 15.7 1.03 15.3 1.99e- 47 #> 3 I(x^2) -2.50 0.0982 -25.4 2.46e-110 ``` .pink[What is the marginal effect of] `\(\color{#e64173}{X}\)` .pink[on] `\(\color{#e64173}{Y}\)` .pink[when] `\(\color{#e64173}{X=7}\)`.pink[?] -- <br> `\(\widehat{\dfrac{\partial \text{Y}}{\partial \text{X}} }\Bigg|_{\small \text{X}=7} = \hat{\beta}_1 + 2\hat{\beta}_2 \cdot (7) = 15.69 -34.96 = -19.27\)` --- class: white-slide .center[**Fitted Regression Line**] <img src="17_all_things_reg2_files/figure-html/unnamed-chunk-28-1.svg" style="display: block; margin: auto;" /> --- class: white-slide .center[**Marginal Effect of X on Y**] <img src="17_all_things_reg2_files/figure-html/unnamed-chunk-29-1.svg" style="display: block; margin: auto;" /> --- # Quadratic Regression [NC] .font80[ **Where does the regression** `\(\hat{Y_i} = \hat{\beta}_0 + \hat{\beta}_1 X_i + \hat{\beta}_2 X_i^2\)` ***turn*?** - In other words, where is the peak (valley) of the fitted relationship? **Step 1:** Take the derivative and set equal to zero. `$$\widehat{\dfrac{\partial \text{Y}}{\partial \text{X}} } = \hat{\beta}_1 + 2\hat{\beta}_2 X = 0$$` **Step 2:** Solve for `\(X\)`. `$$X = -\dfrac{\hat{\beta}_1}{2\hat{\beta}_2}$$` **Example:** Peak of previous regression occurs at `\(X = 3.14\)`. ] --- # Acknowledgments .pull-left[ - [Kyle Raze's Undergraduate Econometrics 1](https://github.com/kyleraze/EC320_Econometrics) - MM - [Science Po Econometrics Course](https://raw.githack.com/ScPoEcon/ScPoEconometrics-Slides/master/chapter_slr/chapter_slr.html#71) - [Nick Huntington-Klein's Explanation of logs](file:///Users/fhoces/Desktop/sandbox/econ140summer2022/16_all_things_reg.html#38) ] .pull-right[ ]