class: center, middle, inverse, title-slide .title[ # PADP 7120 Data Applications in PA ] .subtitle[ ## Regression with Interactions ] .author[ ### Alex Combs ] .institute[ ### UGA | SPIA | PADP ] .date[ ### Last updated: September 28, 2026 ] --- # Outline - Interact numerical and categorical variables --- # Parallel Slopes <img src="Regression-with-Interactions_files/figure-html/unnamed-chunk-2-1.png" alt="" style="display: block; margin: auto;" /> --- # Example: Parallel Slopes `$$Salary=\beta_0+\beta_1YrsEmployed+\beta_2Sex + \epsilon$$` ``` r sal_mod <- lm(salary ~ yrs.service + sex, data = Salaries) ``` ``` r get_regression_table(sal_mod) ``` <table> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std_error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p_value </th> <th style="text-align:right;"> lower_ci </th> <th style="text-align:right;"> upper_ci </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> intercept </td> <td style="text-align:right;"> 92356.947 </td> <td style="text-align:right;"> 4740.188 </td> <td style="text-align:right;"> 19.484 </td> <td style="text-align:right;"> 0.000 </td> <td style="text-align:right;"> 83037.722 </td> <td style="text-align:right;"> 101676.171 </td> </tr> <tr> <td style="text-align:left;"> yrs.service </td> <td style="text-align:right;"> 747.612 </td> <td style="text-align:right;"> 111.396 </td> <td style="text-align:right;"> 6.711 </td> <td style="text-align:right;"> 0.000 </td> <td style="text-align:right;"> 528.607 </td> <td style="text-align:right;"> 966.617 </td> </tr> <tr> <td style="text-align:left;"> sex: Male </td> <td style="text-align:right;"> 9071.800 </td> <td style="text-align:right;"> 4861.644 </td> <td style="text-align:right;"> 1.866 </td> <td style="text-align:right;"> 0.063 </td> <td style="text-align:right;"> -486.208 </td> <td style="text-align:right;"> 18629.808 </td> </tr> </tbody> </table> `$$\hat{Salary}=92357+748 \times YrsEmployed+9072 \times Sex$$` - Note that Female is the reference group, Sex = 1 for Male and 0 for Female --- class: inverse, middle, center # We aren't required to model the slopes to be parallel. Could allow the effect of years employed to differ between male and female professors. --- # Visualizing Interaction Model .pull-left[ ``` r ggplot(Salaries, aes(x = yrs.service, y = salary, * color = sex)) + geom_point(alpha = 0.6, size = 5) + geom_smooth(method = 'lm', se = FALSE, linewidth = 2) + theme_minimal() + theme(text = element_text(size = 20)) ``` ] .pull-right[ <img src="Regression-with-Interactions_files/figure-html/unnamed-chunk-7-1.png" alt="" width="99%" /> ] --- # Interaction model implication `$$y = \beta_0 + \beta_1x + \beta_2d + \beta_3 xd + \epsilon$$` `$$\hat{y} = b_0 + b_1x + b_2d + b_3 xd$$` - Now `\(x\)` and `\(d\)` are interacted (multiplied together), allowing the effect of `\(x\)` to differ between groups of `\(d\)` --- # Interaction model equations `$$\hat{y} = b_0 + b_1x + b_2d + b_3 xd$$` -- - For the `\(d=0\)` group: `$$\hat{y} = b_0 + b_1x$$` -- - For the `\(d=1\)` group: `$$\hat{y} = b_0 + b_1x + b_2 + b_3x$$` `$$\hat{y} = (b_0 + b_2) + (b_1+b_3)x$$` - Intercept *and* slope *can* differ between groups - Intercept differs by `\(b_2\)` and slope (affect of `\(x\)`) differs by `\(b_3\)` --- # Interaction - An interaction allows the effect of one variable to differ depending on the value of another variable `$$Salary=\beta_0+\beta_1YrsEmp+\beta_2Sex + \beta_3YrsEmp*Sex + \epsilon$$` - `\(\beta_3\)` tells us how the slopes compare - For Sex = 1 (male), the association between years and salary is `\(\beta_1+\beta_3\)` - For Sex = 0 (female), it is `\(\beta_1\)` - `\(\beta_2\)` tells us how the intercepts compare - For Sex = 1 (male), intercept is `\(\beta_0+\beta_2\)` - For Sex = 0 (female), it is `\(\beta_0\)` --- # Running an interaction model ``` r sal_mod2 <- lm(salary ~ yrs.service + sex + * yrs.service*sex, data = Salaries) ``` ``` r get_regression_table(sal_mod2) ``` <table> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std_error </th> <th style="text-align:right;"> statistic </th> <th style="text-align:right;"> p_value </th> <th style="text-align:right;"> lower_ci </th> <th style="text-align:right;"> upper_ci </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> intercept </td> <td style="text-align:right;"> 82068.51 </td> <td style="text-align:right;"> 7568.72 </td> <td style="text-align:right;"> 10.84 </td> <td style="text-align:right;"> 0.00 </td> <td style="text-align:right;"> 67188.26 </td> <td style="text-align:right;"> 96948.76 </td> </tr> <tr> <td style="text-align:left;"> yrs.service </td> <td style="text-align:right;"> 1637.30 </td> <td style="text-align:right;"> 523.03 </td> <td style="text-align:right;"> 3.13 </td> <td style="text-align:right;"> 0.00 </td> <td style="text-align:right;"> 609.02 </td> <td style="text-align:right;"> 2665.58 </td> </tr> <tr> <td style="text-align:left;"> sex: Male </td> <td style="text-align:right;"> 20128.63 </td> <td style="text-align:right;"> 7991.14 </td> <td style="text-align:right;"> 2.52 </td> <td style="text-align:right;"> 0.01 </td> <td style="text-align:right;"> 4417.90 </td> <td style="text-align:right;"> 35839.35 </td> </tr> <tr> <td style="text-align:left;"> yrs.service:sexMale </td> <td style="text-align:right;"> -931.74 </td> <td style="text-align:right;"> 535.24 </td> <td style="text-align:right;"> -1.74 </td> <td style="text-align:right;"> 0.08 </td> <td style="text-align:right;"> -1984.04 </td> <td style="text-align:right;"> 120.56 </td> </tr> </tbody> </table> `$$\hat{Sal}=82069+1637*YrsEmp+20129*Sex - 932*YrsEmp*Sex$$` --- # Interpreting an interaction model `$$\hat{Sal}=82069+1637*YrsEmp+20129*Sex - 932*YrsEmp*Sex$$` - Recall that `Sex` = 1 for male, 0 for female - What is the predicted change in salary for a male professor given one more year of employment? - What is the predicted change in salary for a female professor given one more year of employment? --- # Interpreting an interaction model `$$\hat{Sal}=82069+1637*YrsEmp+20129*Sex - 932*YrsEmp*Sex$$` - What is the predicted salary for a female professor employed for 10 years? - What is the predicted salary for a male professor employed for 10 years?