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.
R solution -> dont' peek to early ;) !
ESS <- ESS %>%
mutate(
xeno = scale(xeno) %>% as.numeric(),
cntry = fct_drop(cntry)
) %>%
select(xeno, relig, eduyrs, par_edu, cntry, gndr, pspwght) %>%
drop_na()
Regress xenophobia on religiosity; does religiosity have a curve-shaped relationship with xenophobia? Make sure to not use orthogonal polynomials for this question. Yes, initially religiosity goes along with increased xenophobia, but then that relationship declines in strength Yes, religiosity goes along with declining levels of xenophobia, but that relationship vanishes Yes, religiosity goes along with declining levels of xenophobia, and that relationship grows exponentially No
R solution -> dont' peek to early ;) !
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
Model 3: Add cntry
, par_edu
, agea', and
gndr` as control variables, what happens to our two religiosity estimates? They somewhat change but remain insignificant Particularly the main, but also the squared term decline in strength The main term declines in strength, but not at all the squared term Only the squared term declines in strength
R solution -> dont' peek to early ;) !
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
Visualize the (not really curve-linear) relationship (i.e., visualize the predictions from your model.).
R solution -> dont' peek to early ;) !
# 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()
Is the relation between parental education and xenophobia conditional on the country of residence? No, it is not statistically different across the countries Yes, in Germany parental education is a more important predictor of xenophobia than in Denmark Yes, in Sweden and Norway parental education is a less important predictor of xenophobia than in Denmark Yes, in Norway parental education is a less important predictor of xenophobia than in Denmark
R solution -> dont' peek to early ;) !
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
Visualize the interaction effect.
R solution -> dont' peek to early ;) !
# 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()