1. Use the set-up of exercise 9 (i.e., taks 1 and 2). Z-standardize xeno, drop unused factor levels in cntry, and make a design matrix like I did in my slides (i.e., keep only xeno, relig, cntry, par_edu, gndr, pspwght, and apply casewise deletion). That design matrix has observations left.
ESS <- ESS %>%
  mutate(
    xeno = scale(xeno) %>% as.numeric(),
    cntry = fct_drop(cntry)
  ) %>%
  select(xeno, relig, eduyrs, par_edu, cntry, gndr, pspwght) %>%
  drop_na()
  1. Regress xenophobia on religiosity; does religiosity have a curve-shaped relationship with xenophobia? Make sure to not use orthogonal polynomials for this question.
ols1 <- lm_robust(xeno ~ poly(relig, 2, raw = TRUE), w = pspwght, data = ESS)
screenreg(ols1, include.ci = FALSE)
# 
# ========================================
#                              Model 1    
# ----------------------------------------
# (Intercept)                     0.07 ***
#                                (0.02)   
# poly(relig, 2, raw = TRUE)1    -0.03    
#                                (0.02)   
# poly(relig, 2, raw = TRUE)2    -0.02    
#                                (0.01)   
# ----------------------------------------
# R^2                             0.00    
# Adj. R^2                        0.00    
# Num. obs.                    5197       
# RMSE                            1.00    
# ========================================
# *** p < 0.001; ** p < 0.01; * p < 0.05
  1. Model 3: Add cntry, par_edu, agea', andgndr` as control variables, what happens to our two religiosity estimates?
ols2 <- lm_robust(xeno ~ poly(relig, 2, raw = TRUE) + par_edu + cntry + gndr, w = pspwght, data = ESS)
screenreg(ols2, include.ci = FALSE)
# 
# ========================================
#                              Model 1    
# ----------------------------------------
# (Intercept)                     0.69 ***
#                                (0.05)   
# poly(relig, 2, raw = TRUE)1     0.02    
#                                (0.02)   
# poly(relig, 2, raw = TRUE)2    -0.00    
#                                (0.01)   
# par_edu                        -0.13 ***
#                                (0.01)   
# cntryGermany                   -0.08    
#                                (0.04)   
# cntryNorway                    -0.15 ** 
#                                (0.04)   
# cntrySweden                    -0.44 ***
#                                (0.05)   
# gndrFemale                     -0.13 ***
#                                (0.03)   
# ----------------------------------------
# R^2                             0.07    
# Adj. R^2                        0.07    
# Num. obs.                    5197       
# RMSE                            0.96    
# ========================================
# *** p < 0.001; ** p < 0.01; * p < 0.05
  1. Visualize the (not really curve-linear) relationship (i.e., visualize the predictions from your model.).
# Generate synthetic data to 
# visualize parental education
synth_data <- ESS %>%
   # Generate theoretically-informative 
   # but artificial tibble for predictions.
   data_grid(par_edu, gndr, cntry, relig) %>%
   mutate( # Set confounders constant at their mean,
     eduyrs = mean(ESS$eduyrs, na.rm = TRUE),
     par_edu = mean(ESS$par_edu, na.rm = TRUE),
     gndr = "Female",
     cntry = "Denmark") %>% # then
   unique() # Drop duplicates.

# Generate predictions and 95% confidence intervals, then
synth_data <- predict(ols2, newdata = synth_data,
                      interval = "confidence",
                      level=0.95)$fit %>%
  as_tibble() %>% # Turn into a tibble, then
  bind_cols(synth_data, .) # Add to the synthetic data.


ggplot(data = synth_data, 
       aes(y = fit, x = relig)) +
  geom_ribbon(aes(ymin = lwr, ymax = upr), 
              alpha = 0.5) +
  geom_line() +
  labs(x = "Religiosity",
  y = "Xenophobia",
  caption = "(Covariates set to: German women at mean levels of (parental) education)") + 
  theme_minimal()

  1. Is the relation between parental education and xenophobia conditional on the country of residence?
ols3 <- lm_robust(xeno ~ poly(relig, 2, raw = TRUE) + par_edu*cntry + gndr, w = pspwght, data = ESS)
screenreg(ols3, include.ci = FALSE)
# 
# ========================================
#                              Model 1    
# ----------------------------------------
# (Intercept)                     0.73 ***
#                                (0.08)   
# poly(relig, 2, raw = TRUE)1     0.02    
#                                (0.02)   
# poly(relig, 2, raw = TRUE)2    -0.00    
#                                (0.01)   
# par_edu                        -0.14 ***
#                                (0.02)   
# cntryGermany                    0.14    
#                                (0.11)   
# cntryNorway                    -0.28 ** 
#                                (0.10)   
# cntrySweden                    -0.60 ***
#                                (0.10)   
# gndrFemale                     -0.14 ***
#                                (0.03)   
# par_edu:cntryGermany           -0.06 *  
#                                (0.03)   
# par_edu:cntryNorway             0.04    
#                                (0.03)   
# par_edu:cntrySweden             0.05    
#                                (0.03)   
# ----------------------------------------
# R^2                             0.08    
# Adj. R^2                        0.07    
# Num. obs.                    5197       
# RMSE                            0.96    
# ========================================
# *** p < 0.001; ** p < 0.01; * p < 0.05
  1. Visualize the interaction effect.
# Generate synthetic data to 
# visualize parental education
synth_data <- ESS %>%
   # Generate theoretically-informative 
   # but artificial tibble for predictions.
   data_grid(par_edu, gndr, cntry, relig) %>%
   mutate( # Set confounders constant at their mean,
     relig = mean(ESS$relig, na.rm = TRUE),
     eduyrs = mean(ESS$eduyrs, na.rm = TRUE),
     gndr = "Female") %>% # then
   unique() # Drop duplicates.

# Generate predictions and 95% confidence intervals, then
synth_data <- predict(ols3, newdata = synth_data,
                      interval = "confidence",
                      level=0.95)$fit %>%
  as_tibble() %>% # Turn into a tibble, then
  bind_cols(synth_data, .) # Add to the synthetic data.


ggplot(data = synth_data, 
       aes(y = fit, x = par_edu)) +
  geom_ribbon(aes(ymin = lwr, ymax = upr, fill = cntry), alpha = 0.5) +
  geom_line(aes(color = cntry)) +
  labs(x = "Parental education",
  y = "Xenophobia",
  caption = "(Covariates set to: Women at mean levels of education and religiosity)") + 
  theme_minimal()