class: center, middle, inverse, title-slide .title[ # Multiple Regression &
Fundamentals of Causal Inference ] .subtitle[ ## Interaction/Conditional effects:
When the relation between
y
and
x
1
depends on
x
2
] .author[ ### Merlin Schaeffer
Department of Sociology ] .date[ ### 2025-11-19 ] --- # Goal of empirical sociology .font130[.center[Use data to .alert[discover patterns], <br> and the social mechanisms that bring them about.]] <img src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgykJbXZbwIX-nd1oVOyzmEfj09ho8aQ4HBhfMH27t6YeTYSBevfrD2DFTEQSe7e3UwaVnv-fAJAOVpjW2pSGfG2QNrgHfkTmFtRWK4VXl6KmWK6vVlLud0DRU6lFXAqBF7iij420oOTLFh/s1600/photo.JPG" width="50%" style="display: block; margin: auto;" /> --- class: inverse middle # Today's schedule 1. Application of the day: Xenophobia and education in Denmark and Bulgaria. + OLS is additive. + Conditional relationships are multiplicative. + Interaction terms between a continuous and a categorical variable. 2. Application 2: Do socialism and citizenship rights interact? + Interaction terms between two continuous variables. --- class: inverse <img src="./img/xeno_quest.png" width="70%" style="display: block; margin: auto;" /> <br> .push-left[ <iframe src='https://www.dw.com/en/denmark-tells-syrian-refugees-to-return-to-damascus/a-57174584' width='700' height='430' frameborder='0' scrolling='yes'></iframe> ] .push-right[ <iframe src='https://www.dw.com/en/why-do-so-many-refugees-avoid-bulgaria/a-18707897' width='700' height='430' frameborder='0' scrolling='yes'></iframe> ] --- # Preparations .panelset[ .panel[.panel-name[Get the ESS data] .left-column[ Download the "ESS9e03_1.sav" data from Absalon. Place them into the folder for this course and where you have your R-project. ] .right-column[ .font90[ ``` r pacman::p_load( tidyverse, # Data manipulation, haven, # Handle labelled data. ggplot2, # beautiful figures, estimatr, # Regression for weighted data, modelsummary) # regression tables with nice layout. # Read the ESS round 9 data ESS <- read_spss("../../../assets/ESS9e03_1.sav") %>% # Keep only Danes and Bulgarians filter(cntry == "DK" | cntry == "BG") %>% mutate( # Define variables as categorical and continuous cntry = as_factor(cntry) %>% fct_relevel("Denmark"), gndr = as_factor(gndr), eduyrs = case_when( # Recode education to sensible levels eduyrs < 9 ~ 9, eduyrs > 21 ~ 21, TRUE ~zap_labels(eduyrs)), agea = zap_labels(agea), # Outcome: Subtract from maximum to turn the scale around imwbcnt = 10 - zap_labels(imwbcnt)) %>% # Keep only a minimum set of variables we need today, select(idno, cntry, pspwght, gndr, eduyrs, agea, imwbcnt) %>% drop_na() # Delete cases with missing values. ``` ]]] .panel[.panel-name[The resulting data] ``` r ESS # # A tibble: 3,330 × 7 # idno cntry pspwght gndr eduyrs agea imwbcnt # <dbl> <fct> <dbl> <fct> <dbl> <dbl> <dbl> # 1 2 Bulgaria 0.933 Female 12 72 9 # 2 27 Bulgaria 0.455 Male 17 63 8 # 3 37 Bulgaria 1.02 Male 9 74 8 # 4 50 Bulgaria 0.845 Female 12 39 7 # 5 70 Bulgaria 0.740 Female 12 70 5 # 6 76 Bulgaria 0.704 Female 12 52 3 # 7 137 Bulgaria 1.86 Male 9 24 5 # 8 182 Bulgaria 0.503 Male 17 58 4 # 9 208 Bulgaria 1.32 Male 12 52 1 # 10 234 Bulgaria 1.49 Male 17 76 7 # # ℹ 3,320 more rows ``` ]] --- # Multiple OLS .panelset[ .panel[.panel-name[Denmark vis-á-vis Bulgaria] .push-right[ .font90[
Xeno
Xeno
Xeno
(Intercept)
4.326***
4.909***
4.206***
(0.071)
(0.213)
(0.237)
cntryBulgaria
1.680***
1.635***
1.608***
(0.098)
(0.100)
(0.100)
eduyrs
-0.043**
-0.040**
(0.014)
(0.014)
agea
0.014***
(0.003)
Num.Obs.
3330
3330
3330
R2
0.113
0.116
0.128
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
]] .push-left[ <img src="11-Conditionals_files/figure-html/Coefplot-1.png" width="100%" style="display: block; margin: auto;" /> ]] .panel[.panel-name[Code] .push-left[ .font90[ ``` r bind_rows( # Combine estimate results into one tibble, ols_bi %>% tidy(), ols_mult_1 %>% tidy(), ols_mult_2 %>% tidy(), .id = "model") %>% filter(term == "cntryBulgaria") %>% # Keep country estimates, mutate( # Rename the model variable, term = case_when( model == 1 ~ "Bivariate", model == 2 ~ "Controlled for \n education", model == 3 ~ "Controlled for \n education and age") %>% fct_relevel("Controlled for \n education and age", "Controlled for \n education", "Bivariate")) %>% ggplot(aes(y = estimate, x = term, # Plot the results, ymin = conf.low, ymax = conf.high)) + geom_pointrange() + geom_hline(yintercept = 0, lty = "dashed", color = "red") + coord_flip() + labs(y = "Difference in xenophobia between Denmark (reference) and Bulgaria (10-point Likert Scale)", x = "") + theme_minimal() ``` ]] ..push-right[ .font90[ ``` r ols_bi <- lm_robust(imwbcnt ~ cntry, data = ESS, weights = pspwght) ols_mult_1 <- lm_robust(imwbcnt ~ cntry + eduyrs, data = ESS, weights = pspwght) ols_mult_2 <- lm_robust(imwbcnt ~ cntry + eduyrs + agea, data = ESS, weights = pspwght) modelsummary(list("Xeno" = ols_bi, "Xeno" = ols_mult_1, "Xeno" = ols_mult_2), gof_map = c("nobs", "r.squared"), stars = TRUE, output = "gt") ``` ]]] .panel[.panel-name[Multiple OLS is additive] .right-column[ .font90[
Xeno
Xeno
Xeno
(Intercept)
4.326***
4.909***
4.206***
(0.071)
(0.213)
(0.237)
cntryBulgaria
1.680***
1.635***
1.608***
(0.098)
(0.100)
(0.100)
eduyrs
-0.043**
-0.040**
(0.014)
(0.014)
agea
0.014***
(0.003)
Num.Obs.
3330
3330
3330
R2
0.113
0.116
0.128
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
]] .left-column[ **Multiple OLS, so far:** Allows us to express an outcome as linear function of several continuous and categorical predictor variables. The predictors are adjusted/controlled for each other .alert[and additive]. .font70[ $$ \operatorname{\widehat{imwbcnt}} = 4.21 + 1.61(\operatorname{cntry}_{\operatorname{Bulgaria}}) - 0.04(\operatorname{eduyrs}) + 0.01(\operatorname{agea}) $$ `\(\rightarrow\)` For a Bulgarian who is 20 and has 10 years of education, Model 3 predicts: `\(4.21 + 1.61 + (10\times -0.04) + (20 \times 0.01) = 5.62 \text{ Xeno}\)` ]]] .panel[.panel-name[Additive implies linear] .push-left[ .font90[
Xeno
Xeno
Xeno
(Intercept)
4.326***
4.909***
4.206***
(0.071)
(0.213)
(0.237)
cntryBulgaria
1.680***
1.635***
1.608***
(0.098)
(0.100)
(0.100)
eduyrs
-0.043**
-0.040**
(0.014)
(0.014)
agea
0.014***
(0.003)
Num.Obs.
3330
3330
3330
R2
0.113
0.116
0.128
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
]] .push-right[
]]] --- # But what if relations are conditional? .panelset[ .panel[.panel-name[Plot] .push-left[ .font90[ .content-box-blue[ Two predictors `\(X_{1i}\)` and `\(X_{2i}\)` are **"conditional on each other"** or **"interact"**, when the association between the outcome `\(Y_{i}\)` and one of the predictors depends on values of the other. ] - The association between xenophobia and education is more negative in Denmark than in Bulgaria. + .backgrnote[That is, the association between education and xenophobia depends on the country people live in.] - The difference in average xenophobia between people living in Denmark or Bulgarian is larger among better educated people. + .backgrnote[That is, the association between country and xenophobia depends on people's level of education.] ]] .push-right[ <img src="11-Conditionals_files/figure-html/conditional1-1.png" width="90%" style="display: block; margin: auto;" /> ]] .panel[.panel-name[Code] ``` r ggplot(data = ESS, aes(y = imwbcnt, x = eduyrs, color = cntry)) + geom_jitter(aes(size = pspwght), alpha = 1/8) + geom_smooth(method = "lm") + labs(y = "Immigration makes DK/BG \n a worse place to live", x = "Years of education") + theme_minimal() + theme(legend.position = "bottom") + guides(size = FALSE) ``` ]] --- # Then specify them as *multiplicative* `$$Y_{i} = \alpha + \beta_{1} X_{1} + \beta_{2}X_{2} + \color{red}{\beta_{3}(X_{1}\times X_{2})} + e_{i}$$` .push-left[ .font80[ ``` r *ols_inter <- lm_robust(imwbcnt ~ cntry*eduyrs, data = ESS, weights = pspwght) # Report results in a regression table modelsummary(list("Xeno" = ols_mult_1, "Xeno" = ols_inter), gof_map = c("nobs", "r.squared"), stars = TRUE, output = "gt") ```
Xeno
Xeno
(Intercept)
4.909***
5.873***
(0.213)
(0.279)
cntryBulgaria
1.635***
-0.375
(0.100)
(0.385)
eduyrs
-0.043**
-0.113***
(0.014)
(0.019)
cntryBulgaria × eduyrs
0.153***
(0.027)
Num.Obs.
3330
3330
R2
0.116
0.127
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
]] -- .push-right[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-11-1.png" width="75%" style="display: block; margin: auto;" /> - R *adds* an "interaction term", technically the product of the two interacted variables (i.e. `\(\text{Bulgaria} \times \text{Education}\)`). - Thereby, multiple OLS processes the conditional relation in an additive way (aka "linearization"). ] --- # Then specify them as *multiplicative* .push-left[ ``` r ESS <- ESS %>% mutate( cntry_num = case_when( # Numerical cntry dummy variable, cntry == "Denmark" ~ 0, cntry == "Bulgaria" ~ 1), # Generate interaction term by hand intr_trm = cntry_num * eduyrs) ESS %>% select(idno, cntry, cntry_num, eduyrs, intr_trm) # # A tibble: 3,330 × 5 # idno cntry cntry_num eduyrs intr_trm # <dbl> <fct> <dbl> <dbl> <dbl> # 1 2 Bulgaria 1 12 12 # 2 27 Bulgaria 1 17 17 # 3 37 Bulgaria 1 9 9 # 4 50 Bulgaria 1 12 12 # 5 70 Bulgaria 1 12 12 # 6 76 Bulgaria 1 12 12 # 7 137 Bulgaria 1 9 9 # 8 182 Bulgaria 1 17 17 # 9 208 Bulgaria 1 12 12 # 10 234 Bulgaria 1 17 17 # # ℹ 3,320 more rows ``` ] .push-right[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-13-1.png" width="75%" style="display: block; margin: auto;" /> - R *adds* an "interaction term", technically the product of the two interacted variables (i.e. `\(\text{Bulgaria} \times \text{Education}\)`). - Thereby, multiple OLS processes the conditional relation in an additive way (aka "linearization"). ] --- # Then specify them as *multiplicative* .push-left[ .font90[ ``` r ols_inter2 <- lm_robust(imwbcnt ~ cntry + eduyrs + intr_trm, data = ESS, weights = pspwght) # Report results in a regression table modelsummary(list("Xeno" = ols_inter, "Xeno" = ols_inter2), gof_map = c("nobs", "r.squared"), stars = TRUE, output = "gt") ```
Xeno
Xeno
(Intercept)
5.873***
5.873***
(0.279)
(0.279)
cntryBulgaria
-0.375
-0.375
(0.385)
(0.385)
eduyrs
-0.113***
-0.113***
(0.019)
(0.019)
cntryBulgaria × eduyrs
0.153***
(0.027)
intr_trm
0.153***
(0.027)
Num.Obs.
3330
3330
R2
0.127
0.127
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
]] .push-right[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-15-1.png" width="75%" style="display: block; margin: auto;" /> - R *adds* an "interaction term", technically the product of the two interacted variables (i.e. `\(\text{Bulgaria} \times \text{Education}\)`). - Thereby, multiple OLS processes the conditional relation in an additive way (aka "linearization"). ] --- layout: true class: clear # Tricky interpretation .font70[The two conditional "Main terms"] --- .push-left[ .font90[
Xeno
Xeno
(Intercept)
4.909***
5.873***
(0.213)
(0.279)
cntryBulgaria
1.635***
-0.375
(0.100)
(0.385)
eduyrs
-0.043**
-0.113***
(0.014)
(0.019)
cntryBulgaria × eduyrs
0.153***
(0.027)
Num.Obs.
3330
3330
R2
0.116
0.127
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
]] .push-right[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-17-1.png" width="75%" style="display: block; margin: auto;" /> <br> `$$\beta_1\color{red}{|x_2=0} \text{ & } \beta_2\color{red}{|x_1=0}$$` `\(\rightarrow \beta_{\text{eduyrs}}\color{red}{|\text{Bulgaria} = 0},\)` `\(\rightarrow \beta_{\text{Bulgaria}}\color{red}{|\text{eduyrs} = 0}.\)` ] --- .push-left[ .font90[
Xeno
Xeno
(Intercept)
4.909***
5.873***
(0.213)
(0.279)
cntryBulgaria
1.635***
-0.375
(0.100)
(0.385)
eduyrs
-0.043**
-0.113***
(0.014)
(0.019)
cntryBulgaria × eduyrs
0.153***
(0.027)
Num.Obs.
3330
3330
R2
0.116
0.127
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
]] .push-right[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-19-1.png" width="75%" style="display: block; margin: auto;" /> <br> `$$\beta_1\color{red}{|x_2=0} \text{ & } \beta_2\color{red}{|x_1=0}$$` `\(\rightarrow \beta_{\text{eduyrs}}\color{red}{|\text{Bulgaria} = 0}\)`: A year increase in education goes along with a decline of xenophobia by -0.113 scale points, .alert[if Bulgaria = 0]. `\(\rightarrow\)` .alert[In Denmark] `\(\rightarrow \beta_{\text{Bulgaria}}\color{red}{|\text{eduyrs} = 0}.\)` ] --- .push-left[ .font90[
Xeno
Xeno
(Intercept)
4.909***
5.873***
(0.213)
(0.279)
cntryBulgaria
1.635***
-0.375
(0.100)
(0.385)
eduyrs
-0.043**
-0.113***
(0.014)
(0.019)
cntryBulgaria × eduyrs
0.153***
(0.027)
Num.Obs.
3330
3330
R2
0.116
0.127
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
]] .push-right[ <img src="11-Conditionals_files/figure-html/conditional2-1.png" width="75%" style="display: block; margin: auto;" /> <br> `$$\beta_1\color{red}{|x_2=0} \text{ & } \beta_2\color{red}{|x_1=0}$$` `\(\rightarrow \beta_{\text{eduyrs}}\color{red}{|\text{Bulgaria} = 0}\)`: A year increase in education goes along with a decline of xenophobia by -0.113 scale points, .alert[if Bulgaria = 0]. `\(\rightarrow\)` .alert[In Denmark]. `\(\rightarrow \beta_{\text{Bulgaria}}\color{red}{|\text{eduyrs} = 0}.\)` ] --- layout: false class: clear # Solution: Rescale .font70[(e.g. mean center, or z-standardize)] .push-left[ ``` r ESS <- ESS %>% mutate( # Center education around the mean of education. mc_eduyrs = eduyrs - mean(eduyrs, na.rm = TRUE)) summary(ESS$mc_eduyrs) # Min. 1st Qu. Median Mean 3rd Qu. Max. # -4.24 -2.24 -1.24 0.00 2.76 7.76 ``` ] .push-right[ <img src="11-Conditionals_files/figure-html/conditional3-1.png" width="100%" style="display: block; margin: auto;" /> ] --- layout: true class: clear # Improved interpretation .font70[The two conditional "Main terms"] --- .push-left[
Xeno
Xeno
(Intercept)
5.873***
4.378***
(0.279)
(0.072)
cntryBulgaria
-0.375
1.650***
(0.385)
(0.099)
eduyrs
-0.113***
(0.019)
cntryBulgaria × eduyrs
0.153***
(0.027)
mc_eduyrs
-0.113***
(0.019)
cntryBulgaria × mc_eduyrs
0.153***
(0.027)
Num.Obs.
3330
3330
R2
0.127
0.127
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
] .push-right[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-22-1.png" width="75%" style="display: block; margin: auto;" /> <br> `$$\beta_1\color{red}{|x_2=0} \text{ & } \beta_2\color{red}{|x_1=0}$$` `\(\rightarrow \beta_{\text{eduyrs}}\color{red}{|\text{Bulgaria} = 0}\)`: A year increase in education goes along with a decline of xenophobia by -0.113 scale points, .alert[if Bulgaria = 0]. `\(\rightarrow\)` .alert[In Denmark]. `\(\rightarrow \beta_{\text{Bulgaria}}\color{red}{|\text{eduyrs} = 0}.\)` ] --- .push-left[
Xeno
Xeno
(Intercept)
5.873***
4.378***
(0.279)
(0.072)
cntryBulgaria
-0.375
1.650***
(0.385)
(0.099)
eduyrs
-0.113***
(0.019)
cntryBulgaria × eduyrs
0.153***
(0.027)
mc_eduyrs
-0.113***
(0.019)
cntryBulgaria × mc_eduyrs
0.153***
(0.027)
Num.Obs.
3330
3330
R2
0.127
0.127
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
] .push-right[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-24-1.png" width="50%" style="display: block; margin: auto;" /> <br> `$$\beta_1\color{red}{|x_2=0} \text{ & } \beta_2\color{red}{|x_1=0}$$` `\(\rightarrow \beta_{\text{eduyrs}}\color{red}{|\text{Bulgaria} = 0}\)`: A year increase in education goes along with a decline of xenophobia by -0.113 scale points, .alert[if Bulgaria = 0]. `\(\rightarrow\)` .alert[In Denmark]. `\(\rightarrow \beta_{\text{Bulgaria}}\color{red}{|\text{eduyrs} = 0}\)`: Bulgarians are, on average, 1.65 scale points more xenophobic than Danes, .alert[if education = 0]. `\(\rightarrow\)` .alert[Among persons with an average level of education]. ] --- layout: true class: clear # Interpretation .font70[The "Interaction term" *alters* the main terms] .push-left[ Interaction terms, `\(\beta_3(X_{1i}\times X_{2i})\)`, have two interpretations!
Xeno
(Intercept)
4.378***
(0.072)
cntryBulgaria
1.650***
(0.099)
mc_eduyrs
-0.113***
(0.019)
cntryBulgaria × mc_eduyrs
0.153***
(0.027)
Num.Obs.
3330
R2
0.127
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
] --- .push-right[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-25-1.png" width="66%" style="display: block; margin: auto;" /> <br> 1) When the variable `cntryBulgaria` increases by one unit, `\(\beta_{\text{Educ}}\)` increases by `\(\beta_{3}\)`. + In Bulgaria, the association between education and xenophobia is `\(-0.113 + 0.153 = 0.04\)`. + The difference in `\(\beta_{\text{Educ}}\)` between Denmark and Bulgaria is 0.153. ] --- .push-right[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-26-1.png" width="66%" style="display: block; margin: auto;" /> <br> 2) When the variable `mc_eduyrs` increases by one unit, `\(\beta_{\text{cntryBulgaria}}\)` increases by `\(\beta_{3}\)`. + Among the best educated (i.e. 8 years more than the average), the xenophobia gap between Danes and Bulgarians is `\(1.65 + 8 \times 0.153 = 2.874\)` + With every year of education, the average xenophobia gap between Danes and Bulgarians changes by 0.153. ] --- layout: false # Another example .panelset[ .panel[.panel-name[Age and gender] .left-column[ .font90[
Xeno
(Intercept)
5.278***
(0.075)
mc_eduyrs
-0.060**
(0.021)
gndrFemale
-0.097
(0.102)
mc_eduyrs × gndrFemale
-0.037
(0.029)
Num.Obs.
3330
R2
0.013
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
]] .right-column[ <img src="11-Conditionals_files/figure-html/conditional4-1.png" width="90%" style="display: block; margin: auto;" /> ]] .panel[.panel-name[Interpretation Main terms] .left-column[ .font90[
Xeno
(Intercept)
5.278***
(0.075)
mc_eduyrs
-0.060**
(0.021)
gndrFemale
-0.097
(0.102)
mc_eduyrs × gndrFemale
-0.037
(0.029)
Num.Obs.
3330
R2
0.013
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
]] .right-column[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-28-1.png" width="45%" style="display: block; margin: auto;" /> `\(\rightarrow \beta_{Educ}|\text{Female}=0\)`: Among men, an additional year of education is significantly associated with -0.06 scale points less xenophobia. `\(\rightarrow \beta_{Female}|\text{Educ}=0\)`: At average levels of education, the gender gap in xenophobia is -0.097 but insignificant. ]] .panel[.panel-name[Interpretation Interaction term] .left-column[ .font90[
Xeno
(Intercept)
5.278***
(0.075)
mc_eduyrs
-0.060**
(0.021)
gndrFemale
-0.097
(0.102)
mc_eduyrs × gndrFemale
-0.037
(0.029)
Num.Obs.
3330
R2
0.013
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
]] .right-column[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-30-1.png" width="45%" style="display: block; margin: auto;" /> 1) The association between education and xenophobia among women is `\(-0.06 + -0.037= -0.097\)`. This is -0.037 more negative than the association among men, but this different in slopes is not significant. 2) With every year increase of education, the gender gap in xenophobia declines by -0.037. At two years more education than the average citizen, the gender gap is `\(-0.097 + 2\times -0.037= -0.171\)` scale points, but (probably) insignificant. ]] .panel[.panel-name[Code] .push-left[ ``` r ols_inter <- lm_robust(imwbcnt ~ mc_eduyrs*gndr, data = ESS, weights = pspwght) # Report results in a regression table modelsummary(list("Xeno" = ols_inter), gof_map = c("nobs", "r.squared"), stars = TRUE, output = "gt") ``` ] .push-right[ ``` r ggplot(data = ESS, aes(y = imwbcnt, x = mc_eduyrs, color = gndr)) + geom_jitter(aes(size = pspwght), alpha = 1/8) + geom_vline(xintercept = 0, color = "red") + geom_smooth(method = "lm", fullrange=TRUE) + labs(y = "Immigration makes DK/BG \n a worse place to live", x = "Years of education \n (centered around the mean)") + theme_minimal() + scale_x_continuous(limits = c(-5, 8), breaks = c(-4, -2, 0, 2, 4, 6, 8)) + theme(legend.position = "bottom") + guides(size = FALSE) ``` ]]] --- # Control for potential confounders .font70[Easy ...] .push-left[
Xeno
Xeno
(Intercept)
4.378***
3.789***
(0.072)
(0.150)
cntryBulgaria
1.650***
1.625***
(0.099)
(0.099)
mc_eduyrs
-0.113***
-0.108***
(0.019)
(0.018)
cntryBulgaria × mc_eduyrs
0.153***
0.150***
(0.027)
(0.027)
gndrFemale
-0.135
(0.096)
agea
0.014***
(0.003)
Num.Obs.
3330
3330
R2
0.127
0.139
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
] --- # Visualize! .push-left[
Xeno
Xeno
(Intercept)
4.378***
3.789***
(0.072)
(0.150)
cntryBulgaria
1.650***
1.625***
(0.099)
(0.099)
mc_eduyrs
-0.113***
-0.108***
(0.019)
(0.018)
cntryBulgaria × mc_eduyrs
0.153***
0.150***
(0.027)
(0.027)
gndrFemale
-0.135
(0.096)
agea
0.014***
(0.003)
Num.Obs.
3330
3330
R2
0.127
0.139
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
] .push-right[ .panelset[ .panel[.panel-name[Fictional data] .font90[ ``` r (fict_dat <- tibble( * # Repeat the vector -5:8, each element twice * mc_eduyrs = rep(-5:8, each = 2), * # Repeat DK/BG 14 times * cntry = rep(c("Denmark", "Bulgaria"), 14), # Set controls to constant value gndr = "Female", agea = mean(ESS$agea, na.rm = TRUE))) # # A tibble: 28 × 4 # mc_eduyrs cntry gndr agea # <int> <chr> <chr> <dbl> # 1 -5 Denmark Female 52.2 # 2 -5 Bulgaria Female 52.2 # 3 -4 Denmark Female 52.2 # 4 -4 Bulgaria Female 52.2 # 5 -3 Denmark Female 52.2 # 6 -3 Bulgaria Female 52.2 # 7 -2 Denmark Female 52.2 # 8 -2 Bulgaria Female 52.2 # 9 -1 Denmark Female 52.2 # 10 -1 Bulgaria Female 52.2 # # ℹ 18 more rows ``` ]] .panel[.panel-name[Predictions] .font90[ ``` r *(fict_dat <- predict( * ols_inter4, newdata = fict_dat, * interval = "confidence", level = 0.95)$fit %>% as_tibble() %>% # Turn into a tibble, then bind_cols(fict_dat, .)) # Add to the synthetic data. # # A tibble: 28 × 7 # mc_eduyrs cntry gndr agea fit lwr upr # <int> <chr> <chr> <dbl> <dbl> <dbl> <dbl> # 1 -5 Denmark Female 52.2 4.91 4.64 5.19 # 2 -5 Bulgaria Female 52.2 5.79 5.55 6.02 # 3 -4 Denmark Female 52.2 4.81 4.56 5.05 # 4 -4 Bulgaria Female 52.2 5.83 5.63 6.04 # 5 -3 Denmark Female 52.2 4.70 4.48 4.92 # 6 -3 Bulgaria Female 52.2 5.87 5.69 6.06 # 7 -2 Denmark Female 52.2 4.59 4.39 4.79 # 8 -2 Bulgaria Female 52.2 5.92 5.75 6.08 # 9 -1 Denmark Female 52.2 4.48 4.30 4.66 # 10 -1 Bulgaria Female 52.2 5.96 5.80 6.11 # # ℹ 18 more rows ``` ]] .panel[.panel-name[Plot] <img src="11-Conditionals_files/figure-html/predictions-1.png" width="90%" style="display: block; margin: auto;" /> ] .panel[.panel-name[Code] ``` r ggplot(data = fict_dat, aes(y = fit, x = mc_eduyrs)) + geom_ribbon(aes(ymin = lwr, ymax = upr, fill = cntry), alpha = 0.5) + geom_line(aes(color = cntry)) + labs( fill = "Country:", color = "Country:", title = "Predictions based on multiple OLS", x = "By mean-centered years of education (At average age and gender set to women)", y = "Predicted xenophobia") + theme_minimal() + theme(legend.position = "bottom") ``` ]]] --- class: inverse middle center # Break <iframe src='https://panel.letstimeit.com/instant-timer/15-minute' width='600' height='400' frameborder='0' scrolling='yes'></iframe> --- class: middle clear .left-column[ <img src="https://www.laserfiche.com/wp-content/uploads/2014/10/femalecoder.jpg" width="80%" style="display: block; margin: auto;" /> <iframe src='https://panel.letstimeit.com/instant-timer/20-minute' width='600' height='400' frameborder='0' scrolling='yes'></iframe> ] .right-column[ <br> <iframe src='exercise1.html' width='1000' height='600' frameborder='0' scrolling='yes'></iframe> ] --- class: inverse middle center # Break <iframe src='https://panel.letstimeit.com/instant-timer/10-minute' width='600' height='400' frameborder='0' scrolling='yes'></iframe> --- class: inverse # Revised research question .center[.font140[ Is socialism good at reducing poverty if combined with citizenship rights,<br><br> but not good at it if under autocratic rule? ]] <br> .push-left[ <img src="https://miro.medium.com/max/1280/1*8Y_EPw2a67TRRos3b24YlA.jpeg" width="90%" style="display: block; margin: auto;" /> ] .push-right[ <img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/32/Marx_-_Engels_-_Lenin_-_Stalin.svg/984px-Marx_-_Engels_-_Lenin_-_Stalin.svg.png" width="96%" style="display: block; margin: auto;" /> ] --- class: inverse # Revised research question .center[.font140[ Is socialism good at reducing poverty if combined with citizenship rights,<br><br> but not good at it if under autocratic rule? ]] <br> .push-left[ <img src="11-Conditionals_files/figure-html/socialism-corr1-1.png" width="90%" style="display: block; margin: auto;" /> ] .push-right[ <img src="11-Conditionals_files/figure-html/citizen-corr1-1.png" width="90%" style="display: block; margin: auto;" /> ] --- class: clear # Interaction of two continuous variables .panelset[ .panel[.panel-name[Additive multiple OLS] .push-left[
Poverty
(Intercept)
14.391***
(1.552)
mc_socialist
-0.212+
(0.109)
mc_equal_liberty
-28.855***
(6.797)
Num.Obs.
161
R2
0.131
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
] .push-right[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-41-1.png" width="80%" style="display: block; margin: auto;" /> ]] .panel[.panel-name[Interactions imply non-linearity!] .push-left[
Poverty
Poverty
(Intercept)
14.391***
14.765***
(1.552)
(1.592)
mc_socialist
-0.212+
-0.107
(0.109)
(0.126)
mc_equal_liberty
-28.855***
-27.768***
(6.797)
(6.662)
mc_socialist × mc_equal_liberty
0.828*
(0.356)
Num.Obs.
161
161
R2
0.131
0.148
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
<img src="11-Conditionals_files/figure-html/socialism2-1.png" width="504" style="display: block; margin: auto;" /> ] .push-right[ <img src="11-Conditionals_files/figure-html/unnamed-chunk-42-1.png" width="80%" style="display: block; margin: auto;" /> ]] .panel[.panel-name[Interpretation] .push-left[
Poverty
Poverty
(Intercept)
14.391***
14.765***
(1.552)
(1.592)
mc_socialist
-0.212+
-0.107
(0.109)
(0.126)
mc_equal_liberty
-28.855***
-27.768***
(6.797)
(6.662)
mc_socialist × mc_equal_liberty
0.828*
(0.356)
Num.Obs.
161
161
R2
0.131
0.148
+ p < 0.1, * p < 0.05, ** p < 0.01, *** p < 0.001
<img src="11-Conditionals_files/figure-html/unnamed-chunk-43-1.png" width="504" style="display: block; margin: auto;" /> ] .push-right[ `\(\beta_{\text{Socialism}}|\text{Citizen Rights}=0\)`: At average levels of citizenship rights, socialism is significantly (but only marginally) related to declines in poverty. `\(\beta_{\text{Citizen Rights}}|\text{Socialism}=0\)`: At average levels of socialism, a unit increase in citizenship rights is significantly related to a -27.768 percentage point decline in poverty. `$$\beta_{\text{Socialism} \times \text{Citizen Rights}}$$` 1) With every unit more citizenship rights, socialism gets 0.828 percentage points worse at being related to declines in poverty. 2) With every additional year of socialism, citizenship rights are 0.828 percentage points less associated with declines in poverty. ]] .panel[.panel-name[Code] ``` r # Same data as Lecture 2 Dat <- Dat %>% mutate( # Mean-center predictors mc_socialist = socialist - mean(socialist, na.rm = TRUE), mc_equal_liberty = equal_liberty - mean(equal_liberty, na.rm = TRUE)) # Multiple OLS ols_inter <- lm_robust(poverty ~ mc_socialist*mc_equal_liberty, data = Dat) # Report results in a regression table modelsummary(list("Poverty" = ols_add, "Poverty" = ols_inter), gof_map = c("nobs", "r.squared"), stars = TRUE, output = "gt") # The 3-D plot library(rockchalk) plotPlane(ols_inter, "mc_equal_liberty", "mc_socialist", pch=16, alty=1,alwd=1, theta=50, phi=10) ``` ]] --- class: inverse middle center .font150[ Are citizenship rights off-setting the benefits of socialism? Or Is socialism off-setting the benefits of citizenship rights? <br> <br> `\(\rightarrow\)` We would need a clever *causal research design* to answer that! ] --- layout: false class: inverse # Today's general lessons 1. *Interaction*: When the association between an outcome and one predictor depends on the values of another predictor, the two predictors are said to be "conditional on each other" or to "interact." 2. *Interaction Term*: A mathematical expression that captures the interaction effect between two predictors. It is typically created by multiplying the two variables involved in the interaction. 3. *Interpreting Interaction Terms*: + *Main Effects*: The relationship between each predictor and the outcome when the other predictor is held at zero. + *Interaction Effect*: The change in the relationship between a predictor and the outcome as the other predictor increases by one unit, and vice versa. 4. *Visualization*: Due to the complexity of interaction terms, visualizing them using predictions is recommended.