class: center, middle, inverse, title-slide .title[ #
Interactions 3
] .subtitle[ ## Data Analysis for Psychology in R 2
] .author[ ### dapR2 Team ] .institute[ ### Department of Psychology
The University of Edinburgh ] --- # Weeks Learning Objectives 1. Interpret interactions for between two categorical variables (dummy coding). 2. Visualize and probe interactions. 3. Be able to read interaction plots. --- # Topics for today + Categorical-categorical interaction. + Begin with 2x2 (two binary variables) + Move on to look at examples with more levels + Visualizing and understanding categorical interactions. --- # General definition + When the effects of one predictor on the outcome differ across levels of another predictor. + Categorical*categorical interaction: + There is a difference in the differences between groups across levels of a second factor. + This idea of a difference in differences can be quite tricky to think about. + So we will start with some visualization, and then look at two examples. --- # Visualizing interactions + In our class example we have been looking at predicting salary from years of service and department. + Suppose our company also had two sites, London and Birmingham, and we wanted to see if the salaries across department differed depending on location. + The table below contains the average salary in thousands of pounds for each group. <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> London </th> <th style="text-align:right;"> Birmingham </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Accounts </td> <td style="text-align:right;"> 50 </td> <td style="text-align:right;"> 40 </td> </tr> <tr> <td style="text-align:left;"> Manager </td> <td style="text-align:right;"> 30 </td> <td style="text-align:right;"> 20 </td> </tr> </tbody> </table> --- # Basic plot .pull-left[ <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> London </th> <th style="text-align:right;"> Birmingham </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Accounts </td> <td style="text-align:right;"> 50 </td> <td style="text-align:right;"> 40 </td> </tr> <tr> <td style="text-align:left;"> Manager </td> <td style="text-align:right;"> 30 </td> <td style="text-align:right;"> 20 </td> </tr> </tbody> </table> + And now let's look at the plot: + x-axis shows locations + y-axis is our salaries + The colours represent departments ] .pull-right[ ![](dapr2_08_interactions3_files/figure-html/unnamed-chunk-5-1.png)<!-- --> ] --- # Difference in differences (1) .pull-left[ <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> London </th> <th style="text-align:right;"> Birmingham </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Accounts </td> <td style="text-align:right;"> 50 </td> <td style="text-align:right;"> 40 </td> </tr> <tr> <td style="text-align:left;"> Manager </td> <td style="text-align:right;"> 30 </td> <td style="text-align:right;"> 20 </td> </tr> </tbody> </table> + In each plot we look at, think about subtracting the average store managers salary (blue triangle) from the average accounts salary (red circle) + In both cases it is £20,000. + Note, the lines are parallel + Remember what we have said about parallel lines...no interaction ] .pull-right[ ![](dapr2_08_interactions3_files/figure-html/unnamed-chunk-8-1.png)<!-- --> ] --- # Difference in differences (2) .pull-left[ <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> London </th> <th style="text-align:right;"> Birmingham </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Accounts </td> <td style="text-align:right;"> 50 </td> <td style="text-align:right;"> 40 </td> </tr> <tr> <td style="text-align:left;"> Manager </td> <td style="text-align:right;"> 40 </td> <td style="text-align:right;"> 20 </td> </tr> </tbody> </table> + This time we can see the difference differs. + £20,000 in Birmingham + £10,000 in London. + Note the lines are no longer parallel. + Suggests interaction. + But not crossing (so ordinal interaction) ] .pull-right[ ![](dapr2_08_interactions3_files/figure-html/unnamed-chunk-11-1.png)<!-- --> ] --- # Difference in differences (3) .pull-left[ <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> London </th> <th style="text-align:right;"> Birmingham </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Accounts </td> <td style="text-align:right;"> 40 </td> <td style="text-align:right;"> 40 </td> </tr> <tr> <td style="text-align:left;"> Manager </td> <td style="text-align:right;"> 60 </td> <td style="text-align:right;"> 20 </td> </tr> </tbody> </table> + This time we can see the difference differs. + £20,000 in Birmingham + £20,000 in London + Note the lines are no longer parallel. + Suggests interaction. + Now crossing (so disordinal interaction) ] .pull-right[ ![](dapr2_08_interactions3_files/figure-html/unnamed-chunk-14-1.png)<!-- --> ] --- class: center, middle # Question break ... --- # Lecture notation `$$y_i = \beta_0 + \beta_1 x_{i} + \beta_2 z_{i} + \beta_3 xz_{i} + \epsilon_i$$` + Lecture notation: + `\(y\)` is a continuous outcome + `\(x\)` is a categorical predictor ( `location` ) + `\(z\)` is a categorical predictor (`department` ) + `\(xz\)` is their product or interaction predictor --- # Product for dummy variables <table> <thead> <tr> <th style="text-align:left;"> location </th> <th style="text-align:left;"> department </th> <th style="text-align:right;"> x </th> <th style="text-align:right;"> z </th> <th style="text-align:right;"> xz </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> London </td> <td style="text-align:left;"> Accounts </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> London </td> <td style="text-align:left;"> Manager </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> Birmingham </td> <td style="text-align:left;"> Accounts </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> Birmingham </td> <td style="text-align:left;"> Manager </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> </tr> </tbody> </table> --- # Interpretation: Categorical*categorical interaction (dummy codes) `$$y_i = \beta_0 + \beta_1 x_{i} + \beta_2 z_{i} + \beta_3 xz_{i} + \epsilon_i$$` + `\(\beta_0\)` = Value of `\(y\)` when `\(x\)` and `\(z\)` are 0 + Expected salary for Accounts in London. --- # Interpretation: Categorical*categorical interaction (dummy codes) `$$y_i = \beta_0 + \beta_1 x_{i} + \beta_2 z_{i} + \beta_3 xz_{i} + \epsilon_i$$` + `\(\beta_1\)` = Difference between levels of `\(x\)` when `\(z\)` = 0 + The difference in salary between Accounts in London and Birmingham --- # Interpretation: Categorical*categorical interaction (dummy codes) `$$y_i = \beta_0 + \beta_1 x_{i} + \beta_2 z_{i} + \beta_3 xz_{i} + \epsilon_i$$` + `\(\beta_2\)` = Difference between levels of `\(z\)` when `\(x\)` = 0. + The difference in salary between Accounts and Store managers in London. --- # Interpretation: Categorical*categorical interaction (dummy codes) `$$y_i = \beta_0 + \beta_1 x_{i} + \beta_2 z_{i} + \beta_3 xz_{i} + \epsilon_i$$` + `\(\beta_3\)` = Difference between levels of `\(x\)` across levels of `\(z\)` + The difference between salary in Accounts and Store managers between London and Birmingham --- # Example: Categorical*categorical .pull-left[ ```r salary3 %>% group_by(location, department) %>% summarise( Salary = mean(salary) ) ``` ] .pull-right[ ``` ## # A tibble: 4 × 3 ## # Groups: location [2] ## location department Salary ## <fct> <fct> <dbl> ## 1 London Accounts 50.7 ## 2 London Manager 47.2 ## 3 Birmingham Accounts 48.7 ## 4 Birmingham Manager 36.8 ``` ] --- # Example: Categorical*categorical ```r m1 <- lm(salary ~ location*department, salary3) ``` ``` ## ## Call: ## lm(formula = salary ~ location * department, data = salary3) ## ## Residuals: ## Min 1Q Median 3Q Max ## -10.551 -3.389 0.579 2.937 14.325 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 50.6704 0.7144 70.927 < 2e-16 *** ## locationBirmingham -1.9796 1.4872 -1.331 0.186297 ## departmentManager -3.4625 1.3365 -2.591 0.011072 * ## locationBirmingham:departmentManager -8.4153 2.2779 -3.694 0.000367 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 5.052 on 96 degrees of freedom ## Multiple R-squared: 0.4774, Adjusted R-squared: 0.461 ## F-statistic: 29.23 on 3 and 96 DF, p-value: 1.636e-13 ``` --- # Example: Categorical*categorical .pull-left[ + We can visualise categorical interactions using `cat_plot()` from `interactions`. + `probe_interaction()` does not work with two categorical predictors. ```r cat_plot(m1, pred = location, modx = department) ``` ] .pull-right[ ![](dapr2_08_interactions3_files/figure-html/unnamed-chunk-21-1.png)<!-- --> ] --- # Example: Categorical*categorical ``` ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 50.67 0.71 70.93 0.00 ## locationBirmingham -1.98 1.49 -1.33 0.19 ## departmentManager -3.46 1.34 -2.59 0.01 ## locationBirmingham:departmentManager -8.42 2.28 -3.69 0.00 ``` .pull-left[ + `\(\beta_0\)` = Value of `\(y\)` when `\(x\)` and `\(z\)` are 0 + Expected salary for Accounts in London is £50,670. ] .pull-right[ <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> London </th> <th style="text-align:right;"> Birmingham </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Accounts </td> <td style="text-align:right;"> 50.67 </td> <td style="text-align:right;"> 48.69 </td> </tr> <tr> <td style="text-align:left;"> Manager </td> <td style="text-align:right;"> 47.21 </td> <td style="text-align:right;"> 36.81 </td> </tr> </tbody> </table> ] --- # Example: Categorical*categorical ``` ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 50.67 0.71 70.93 0.00 ## locationBirmingham -1.98 1.49 -1.33 0.19 ## departmentManager -3.46 1.34 -2.59 0.01 ## locationBirmingham:departmentManager -8.42 2.28 -3.69 0.00 ``` .pull-left[ + `\(\beta_1\)` = Difference between levels of `\(x\)` when `\(z\)` = 0 + The difference in salary between Accounts in London and Birmingham is £1,980. The salary is lower in Birmingham. + These are calculated as: `$$\text{Group} - \text{Reference group (intercept)}$$` ] .pull-right[ <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> London </th> <th style="text-align:right;"> Birmingham </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Accounts </td> <td style="text-align:right;"> 50.67 </td> <td style="text-align:right;"> 48.69 </td> </tr> <tr> <td style="text-align:left;"> Manager </td> <td style="text-align:right;"> 47.21 </td> <td style="text-align:right;"> 36.81 </td> </tr> </tbody> </table> ] --- # Example: Categorical*categorical ``` ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 50.67 0.71 70.93 0.00 ## locationBirmingham -1.98 1.49 -1.33 0.19 ## departmentManager -3.46 1.34 -2.59 0.01 ## locationBirmingham:departmentManager -8.42 2.28 -3.69 0.00 ``` .pull-left[ + `\(\beta_2\)` = Difference between levels of `\(z\)` when `\(x\)` = 0. + The difference in salary between Accounts and Store managers in London is £3,460. The salary is lower for Store Managers. + These are calculated as: `$$\text{Group} - \text{Reference group (intercept)}$$` ] .pull-right[ <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> London </th> <th style="text-align:right;"> Birmingham </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Accounts </td> <td style="text-align:right;"> 50.67 </td> <td style="text-align:right;"> 48.69 </td> </tr> <tr> <td style="text-align:left;"> Manager </td> <td style="text-align:right;"> 47.21 </td> <td style="text-align:right;"> 36.81 </td> </tr> </tbody> </table> ] --- # Example: Categorical*categorical ``` ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 50.67 0.71 70.93 0.00 ## locationBirmingham -1.98 1.49 -1.33 0.19 ## departmentManager -3.46 1.34 -2.59 0.01 ## locationBirmingham:departmentManager -8.42 2.28 -3.69 0.00 ``` .pull-left[ + `\(\beta_3\)` = Difference between levels of `\(x\)` across levels of `\(z\)` + The difference between salary for Accounts and Store managers between London and Birmingham, differs by £8,420. The difference is greater in Birmingham than in London. + These are fiddly (handout online that goes through this in detail) ] .pull-right[ <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:right;"> London </th> <th style="text-align:right;"> Birmingham </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> Accounts </td> <td style="text-align:right;"> 50.67 </td> <td style="text-align:right;"> 48.69 </td> </tr> <tr> <td style="text-align:left;"> Manager </td> <td style="text-align:right;"> 47.21 </td> <td style="text-align:right;"> 36.81 </td> </tr> </tbody> </table> ] --- class: center, middle # Good time to pause --- # Extending past 2x2 + When fitting an interaction to categorical variables with more than 2 levels, we need additional interaction terms. + Remember, I categorical variable with 3 levels is represented by 2 dummy coded variables. + So that means we have two variables to create products with. + The general rule on the number of interaction terms is: `$$(r-1)*(c-1)$$` + Where + `\(r\)` (row) = number of levels of the first categorical variable. + `\(c\)` (column) = number of levels of the second categorical variable. --- # Example + The data comes from a study into patient care in a paediatric wards. + A researcher was interested in whether the subjective well-being of patients differed dependent on the post-operation treatment schedule they were given, and the hospital in which they were staying. + **Condition 1**: `Treatment` (Levels: TreatA, TreatB, TreatC). + **Condition 2**: `Hosp` (Levels: Hosp1, Hosp2). + Total sample n = 180 (30 patients in each of 6 groups). + Between person design. + **Outcome**: Subjective well-being (SWB) + An average of multiple raters (the patient, a member of their family, and a friend). + SWB score ranged from 0 to 20. --- # The data ```r hosp_tbl <- read_csv("hospital.csv", col_types = "dff") hosp_tbl %>% slice(1:10) ``` ``` ## # A tibble: 10 × 3 ## SWB Treatment Hospital ## <dbl> <fct> <fct> ## 1 6.2 TreatA Hosp1 ## 2 15.9 TreatA Hosp1 ## 3 7.2 TreatA Hosp1 ## 4 11.3 TreatA Hosp1 ## 5 11.2 TreatA Hosp1 ## 6 9 TreatA Hosp1 ## 7 14.5 TreatA Hosp1 ## 8 7.3 TreatA Hosp1 ## 9 13.7 TreatA Hosp1 ## 10 12.6 TreatA Hosp1 ``` --- # The group means <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> Hosp1 </th> <th style="text-align:left;"> Hosp2 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> TreatA </td> <td style="text-align:left;"> 10.80 </td> <td style="text-align:left;"> 7.85 </td> </tr> <tr> <td style="text-align:left;"> TreatB </td> <td style="text-align:left;"> 9.43 </td> <td style="text-align:left;"> 13.11 </td> </tr> <tr> <td style="text-align:left;"> TreatC </td> <td style="text-align:left;"> 10.10 </td> <td style="text-align:left;"> 7.98 </td> </tr> </tbody> </table> --- # Model equations and coding `$$y_{ijk} = b_0 + \underbrace{(b_1D_1 + b_2D_2)}_{\text{Treatment}} + \underbrace{b_3D_3}_{\text{Hospital}} + \underbrace{b_4D_{13} + b_5D_{23}}_{\text{Interactions}} + \epsilon_{i}$$` ``` ## # A tibble: 6 × 7 ## Treatment Hospital D1 D2 D3 D13 D23 ## <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 A Hosp1 0 0 0 0 0 ## 2 A Hosp2 0 0 1 0 0 ## 3 B Hosp1 1 0 0 0 0 ## 4 B Hosp2 1 0 1 1 0 ## 5 C Hosp1 0 1 0 0 0 ## 6 C Hosp2 0 1 1 0 1 ``` --- # Interpretation with dummy coding `$$y_{ijk} = b_0 + \underbrace{(b_1D_1 + b_2D_2)}_{\text{Treatment}} + \underbrace{b_3D_3}_{\text{Hospital}} + \underbrace{b_4D_{13} + b_5D_{23}}_{\text{Interactions}} + \epsilon_{i}$$` + `\(b_0\)` = Mean of treatment A in hospital 1. + `\(b_1\)` = Difference between Treatment B and Treatment A in Hospital 1. + `\(b_2\)` = Difference between Treatment C and Treatment A in Hospital 1. + `\(b_3\)` = Difference between Treatment A in Hospital 1 and Hospital 2. + `\(b_4\)` = Difference between Treatment A and Treatment B between Hospital 1 and Hospital 2 + `\(b_5\)` = Difference between Treatment A and Treatment C between Hospital 1 and Hospital 2 --- # Our results ```r m4 <- lm(SWB ~ Treatment + Hospital + Treatment*Hospital, data = hosp_tbl) m4sum <- summary(m4) m4res <- round(m4sum$coefficients,2) m4res ``` ``` ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 10.80 0.37 29.19 0.00 ## TreatmentTreatB -1.37 0.52 -2.62 0.01 ## TreatmentTreatC -0.70 0.52 -1.33 0.18 ## HospitalHosp2 -2.95 0.52 -5.63 0.00 ## TreatmentTreatB:HospitalHosp2 6.63 0.74 8.97 0.00 ## TreatmentTreatC:HospitalHosp2 0.82 0.74 1.11 0.27 ``` --- # Interpretation with dummy coding ``` ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 10.80 0.37 29.19 0.00 ## TreatmentTreatB -1.37 0.52 -2.62 0.01 ## TreatmentTreatC -0.70 0.52 -1.33 0.18 ## HospitalHosp2 -2.95 0.52 -5.63 0.00 ## TreatmentTreatB:HospitalHosp2 6.63 0.74 8.97 0.00 ## TreatmentTreatC:HospitalHosp2 0.82 0.74 1.11 0.27 ``` .pull-left[ + `\(b_0\)` = Mean of treatment A in hospital 1. + `\(b_1\)` = Difference between Treatment B and Treatment A in Hospital 1. + `\(b_2\)` = Difference between Treatment C and Treatment A in Hospital 1. + `\(b_3\)` = Difference between Treatment A in Hospital 1 and Hospital 2. + `\(b_4\)` = Difference between Treatment A and Treatment B between Hospital 1 and Hospital 2 + `\(b_5\)` = Difference between Treatment A and Treatment C between Hospital 1 and Hospital 2 ] .pull-right[ <table> <thead> <tr> <th style="text-align:left;"> </th> <th style="text-align:left;"> Hosp1 </th> <th style="text-align:left;"> Hosp2 </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> TreatA </td> <td style="text-align:left;"> 10.80 </td> <td style="text-align:left;"> 7.85 </td> </tr> <tr> <td style="text-align:left;"> TreatB </td> <td style="text-align:left;"> 9.43 </td> <td style="text-align:left;"> 13.11 </td> </tr> <tr> <td style="text-align:left;"> TreatC </td> <td style="text-align:left;"> 10.10 </td> <td style="text-align:left;"> 7.98 </td> </tr> </tbody> </table> ] --- # Brief comment: 3-way interactions + **In principle** we can extend interactions to more than two variables. + Consider our salary example: + We could ask does the change in difference in the effect of years of service across departments, differ dependent on whether you are based in London or Birmingham. + Note we have three predictors here: + Years of service (continuous) + department (binary; Store Manager, Accounts) + location (binary; London, Birmingham) + This is a plausible question concerning organisatonal fairness. --- # Brief comment: 3-way interactions + In general, three-way interactions are tricky to interpret. + Require lots of data to test as the effects are often very small. + Extends the issues of power already discussed. + **Only test a three-way interaction if you have strong theoretical reason for doing so** --- # Summary + We have considered how we fit and interpret linear models with categorical interactions. + We have focussed on dummy coded (0,1) variables. + Next semester, we will return to this topic and consider other ways of treating categorical data when we have specific hypotheses we want to test. --- class: center, middle # Thanks for listening!