Setup: Generate a folder for this course and get access to the Danish European Social Survey 2014 data. Moreover, download the ESS Codebook to find out the variable names.
1. Keep only respondents who were born in Denmark and have Danish-born parents. How many such respondents are there?
library(tidyverse) # Add the tidyverse package to my current library.
library(haven) # Handle labelled data.
library(essurvey) # Add ESS API package.
# Import the ESS round 7 data via the API
ESS <- import_country(country = "Denmark", rounds = 7, ess_email = YOUR_EMAIL) %>%
recode_missings() %>%
# Keep only respondents with Danish-born parents.
filter(brncntr == 1 & mocntr == 1 & facntr == 1)
# Number of remaining respondents.
nrow(ESS)
# [1] 1304
# . use "./../../assets/ESS7/ESS7DK.dta", clear
#
# . quietly do "./../../assets/ESS7/ESS7e02_2_formats_unicode.do"
# .
# . keep if brncntr == 1 & mocntr == 1 & facntr == 1
# (198 observations deleted)
#
# .
# . count
# 1,304
#
# .
There are such respondents.
2. Generate an additive classic racist prejudice index. How many percent of the respondents score the highest value on this index?
ESS <- ESS %>%
mutate(
# Make variables numeric
smegbli = zap_labels(smegbli),
smegbhw = zap_labels(smegbhw),
smctmbe = zap_labels(smctmbe),
# Recode 2 (No) to zero
smegbli = case_when(smegbli == 2 ~ 0, TRUE ~ smegbli),
smegbhw = case_when(smegbhw == 2 ~ 0, TRUE ~ smegbhw),
smctmbe = case_when(smctmbe == 2 ~ 0, TRUE ~ smctmbe),
# Generate additive scale 0 to 3
racism = smegbli + smegbhw + smctmbe
)
table(ESS$racism) %>% prop.table()
#
# 0 1 2 3
# 0.26 0.44 0.23 0.06
# . use "./../../assets/ESS7/ESS7DK.dta", clear
#
# . quietly do "./../../assets/ESS7/ESS7e02_2_formats_unicode.do"
# . keep if brncntr == 1 & mocntr == 1 & facntr == 1
# (198 observations deleted)
#
# .
# . recode smegbli smegbhw smctmbe (2 = 0)
# (smegbli: 1131 changes made)
# (smegbhw: 771 changes made)
# (smctmbe: 480 changes made)
#
# . gen racism = smegbli + smegbhw + smctmbe
# (107 missing values generated)
#
# . tab racism
#
# racism | Freq. Percent Cum.
# ------------+-----------------------------------
# 0 | 312 26.07 26.07
# 1 | 532 44.44 70.51
# 2 | 281 23.48 93.98
# 3 | 72 6.02 100.00
# ------------+-----------------------------------
# Total | 1,197 100.00
#
# .
% of respondents with Danish-born parents score the highest possible value of three on classic racist prejudice index.
3. Generate an overall additive xenophobia index that combines the overall, economic and cultural xenophopic prejudice variables. What is the correlation between classic racist and xenophobic prejudice? Don’t remember what a Correlation (\(\rho\)) was, see below.
library(ggplot2) # For nice graphs.
ESS <- ESS %>%
mutate(
# Make variables numeric
imtcjob = zap_labels(imtcjob),
imbleco = zap_labels(imbleco),
rlgueim = zap_labels(rlgueim),
imbgeco = zap_labels(imbgeco),
imueclt = zap_labels(imueclt),
imwbcnt = zap_labels(imwbcnt),
# Generate additive scale 0 to 10
xeno = 10 - ((imtcjob + imbleco + rlgueim + imbgeco + imueclt + imwbcnt) / 6))
# A scatterplot
ggplot(data = ESS, aes(y = racism, x = xeno, weight = pspwght)) +
geom_jitter(aes(size = pspwght), alpha = 1/4) +
geom_smooth(se = FALSE, color = "red") +
geom_smooth(method = "lm") +
labs(y = "Classic racist prejudice", x = "Xenophobic prejudice") +
theme_minimal() +
theme(legend.position = "none")
# The weighted correlation
cor <- ESS %>% select(racism, xeno, pspwght) %>%
drop_na() %>%
cov.wt(., wt = .$pspwght, cor = TRUE)
cor$cor
# racism xeno pspwght
# racism 1.00 0.31 0.11
# xeno 0.31 1.00 0.17
# pspwght 0.11 0.17 1.00
# . use "./../../assets/ESS7/ESS7DK.dta", clear
#
# . quietly do "./../../assets/ESS7/ESS7e02_2_formats_unicode.do"
# . keep if brncntr == 1 & mocntr == 1 & facntr == 1
# (198 observations deleted)
#
# . recode smegbli smegbhw smctmbe (2 = 0)
# (smegbli: 1131 changes made)
# (smegbhw: 771 changes made)
# (smctmbe: 480 changes made)
#
# . gen racism = smegbli + smegbhw + smctmbe
# (107 missing values generated)
#
# .
# . gen xeno = 10 - ((imtcjob + imbleco + rlgueim + imbgeco + imueclt + imwbcnt)
# > / 6)
# (108 missing values generated)
#
# . corr racism xeno [w = pspwght]
# (analytic weights assumed)
# (sum of wgt is 1,106.71625825745)
# (obs=1,118)
#
# | racism xeno
# -------------+------------------
# racism | 1.0000
# xeno | 0.3106 1.0000
#
#
# .
The correlation between classic racist and xenophobic prejudice is .
4. Operationalize the frequency of inter-racial or -ethnic contact as a continuous variable. How strongly is it correlated with Monday’s measures of racism and xenophobia?
Don’t remember what a Correlation (\(\rho\)) was? The idea is pretty simple:
\[\begin{align*} \rho(Y,X) &= \frac{\sum^{n}_{i=1} (Y_i - \bar{Y}_{.})(X_i - \bar{X}_{.})}{\sqrt{\sum_{i=1}^{n}(Y_i-\bar{Y}_{.})^2 \sum_{i=1}^{n}(X_i - \bar{X}_{.})^2}}, \\ &= \frac{\sum_{i=1}^{n}Z_{Yi}Z_{Xi}}{M-1} \end{align*}\]
# The weighted correlations
cor <- ESS %>% select(racism, xeno, dfegcon, pspwght) %>%
drop_na() %>%
cov.wt(., wt = .$pspwght, cor = TRUE)
cor$cor
# racism xeno dfegcon pspwght
# racism 1.00 0.31 -0.1118 0.1078
# xeno 0.31 1.00 -0.1275 0.1714
# dfegcon -0.11 -0.13 1.0000 -0.0088
# pspwght 0.11 0.17 -0.0088 1.0000
# . use "./../../assets/ESS7/ESS7DK.dta", clear
#
# . quietly do "./../../assets/ESS7/ESS7e02_2_formats_unicode.do"
# . keep if brncntr == 1 & mocntr == 1 & facntr == 1
# (198 observations deleted)
#
# . recode smegbli smegbhw smctmbe (2 = 0)
# (smegbli: 1131 changes made)
# (smegbhw: 771 changes made)
# (smctmbe: 480 changes made)
#
# . gen racism = smegbli + smegbhw + smctmbe
# (107 missing values generated)
#
# .
# . gen xeno = 10 - ((imtcjob + imbleco + rlgueim + imbgeco + imueclt + imwbcnt)
# > / 6)
# (108 missing values generated)
#
# . corr racism xeno dfegcon [w = pspwght]
# (analytic weights assumed)
# (sum of wgt is 1,105.00237299491)
# (obs=1,116)
#
# | racism xeno dfegcon
# -------------+---------------------------
# racism | 1.0000
# xeno | 0.3088 1.0000
# dfegcon | -0.1118 -0.1275 1.0000
#
#
# .
The frequency of superficial inter-racial or -ethnic contact correlates with with racist and with xenophobic prejudice.
5. Regress our measure of xenophobic prejudice on the frequency of inter-racial or -ethnic contact as a continuous variable. What does the result suggest about how average levels of xenophobia changes with an increase of contact by one unit?
library(tidyverse) # Add the tidyverse package to my current library.
library(haven) # Handle labelled data.
library(essurvey) # Add ESS API package.
library(estimatr) # For regression with robust SE
library(texreg) # For nicely-formatted regression tables
# Import the ESS round 7 data via the API
ESS <- import_country(
country = "Denmark",
rounds = 7,
ess_email = YOUR_EMAIL) %>%
recode_missings() %>%
# Keep only respondents with Danish-born parents.
filter(brncntr == 1 & mocntr == 1 & facntr == 1) %>%
mutate(
# Make variables numeric
## Racism
smegbli = zap_labels(smegbli),
smegbhw = zap_labels(smegbhw),
smctmbe = zap_labels(smctmbe),
imtcjob = zap_labels(imtcjob),
## Xenophobia
imbleco = zap_labels(imbleco),
rlgueim = zap_labels(rlgueim),
imbgeco = zap_labels(imbgeco),
imueclt = zap_labels(imueclt),
imwbcnt = zap_labels(imwbcnt),
dfegcon = zap_labels(dfegcon),
# Recode 2 (No) to zero
smegbli = case_when(smegbli == 2 ~ 0, TRUE ~ smegbli),
smegbhw = case_when(smegbhw == 2 ~ 0, TRUE ~ smegbhw),
smctmbe = case_when(smctmbe == 2 ~ 0, TRUE ~ smctmbe),
# Generate additive racist prejudice scale 0 to 3
racism = smegbli + smegbhw + smctmbe,
# Generate additive xenophobic prejudice scale 0 to 10
xeno = 10 - ((imtcjob + imbleco + rlgueim + imbgeco + imueclt + imwbcnt) / 6))
# The weighted correlations
ols1 <- lm_robust(data = ESS, formula = xeno ~ dfegcon, weight = pspwght)
screenreg(ols1, include.ci = FALSE, digits = 3)
#
# =========================
# Model 1
# -------------------------
# (Intercept) 5.564 ***
# (0.143)
# dfegcon -0.123 ***
# (0.027)
# -------------------------
# R^2 0.020
# Adj. R^2 0.019
# Num. obs. 1194
# RMSE 1.649
# =========================
# *** p < 0.001; ** p < 0.01; * p < 0.05
# . use "./../../assets/ESS7/ESS7DK.dta", clear
#
# . quietly do "./../../assets/ESS7/ESS7e02_2_formats_unicode.do"
# . keep if brncntr == 1 & mocntr == 1 & facntr == 1
# (198 observations deleted)
#
# . recode smegbli smegbhw smctmbe (2 = 0)
# (smegbli: 1131 changes made)
# (smegbhw: 771 changes made)
# (smctmbe: 480 changes made)
#
# . gen racism = smegbli + smegbhw + smctmbe
# (107 missing values generated)
#
# . gen xeno = 10 - ((imtcjob + imbleco + rlgueim + imbgeco + imueclt + imwbcnt)
# > / 6)
# (108 missing values generated)
#
# .
# . reg xeno dfegcon [w = pspwght], rob
# (analytic weights assumed)
# (sum of wgt is 1,185.23266195804)
#
# Linear regression Number of obs = 1,194
# F(1, 1192) = 20.80
# Prob > F = 0.0000
# R-squared = 0.0201
# Root MSE = 1.6549
#
# ------------------------------------------------------------------------------
# | Robust
# xeno | Coef. Std. Err. t P>|t| [95% Conf. Interval]
# -------------+----------------------------------------------------------------
# dfegcon | -.1228782 .026942 -4.56 0.000 -.1757371 -.0700193
# _cons | 5.564098 .1425176 39.04 0.000 5.284485 5.843711
# ------------------------------------------------------------------------------
#
# .
With every unit increase in superficial inter-racial/ethnic contact, the average level of xenophobic prejudice declines by on its scale from 0 to 10.
6. Control for respondents’ years of education, age, gender, and employment status. What difference does that make?
# The weighted correlations
ols1 <- lm_robust(data = ESS, formula = xeno ~ dfegcon + eduyrs + agea + factor(gndr) + factor(uempla), weight = pspwght)
screenreg(ols1, include.ci = FALSE, digits = 3)
#
# =============================
# Model 1
# -----------------------------
# (Intercept) 6.449 ***
# (0.267)
# dfegcon -0.083 **
# (0.030)
# eduyrs -0.078 ***
# (0.010)
# agea -0.001
# (0.003)
# factor(gndr)2 -0.127
# (0.101)
# factor(uempla)1 0.055
# (0.247)
# -----------------------------
# R^2 0.067
# Adj. R^2 0.064
# Num. obs. 1188
# RMSE 1.610
# =============================
# *** p < 0.001; ** p < 0.01; * p < 0.05
# . use "./../../assets/ESS7/ESS7DK.dta", clear
#
# . quietly do "./../../assets/ESS7/ESS7e02_2_formats_unicode.do"
# . keep if brncntr == 1 & mocntr == 1 & facntr == 1
# (198 observations deleted)
#
# . recode smegbli smegbhw smctmbe (2 = 0)
# (smegbli: 1131 changes made)
# (smegbhw: 771 changes made)
# (smctmbe: 480 changes made)
#
# . gen racism = smegbli + smegbhw + smctmbe
# (107 missing values generated)
#
# . gen xeno = 10 - ((imtcjob + imbleco + rlgueim + imbgeco + imueclt + imwbcnt)
# > / 6)
# (108 missing values generated)
#
# .
# . reg xeno dfegcon eduyrs agea i.gndr i.uempla [w = pspwght], rob
# (analytic weights assumed)
# (sum of wgt is 1,178.26042400013)
#
# Linear regression Number of obs = 1,188
# F(5, 1182) = 16.55
# Prob > F = 0.0000
# R-squared = 0.0675
# Root MSE = 1.6163
#
# ------------------------------------------------------------------------------
# | Robust
# xeno | Coef. Std. Err. t P>|t| [95% Conf. Interval]
# -------------+----------------------------------------------------------------
# dfegcon | -.0832311 .0299116 -2.78 0.005 -.1419168 -.0245454
# eduyrs | -.0776921 .0104485 -7.44 0.000 -.0981917 -.0571924
# agea | -.0013075 .0029339 -0.45 0.656 -.0070638 .0044488
# |
# gndr |
# Female | -.1270295 .1011019 -1.26 0.209 -.3253887 .0713297
# |
# uempla |
# Marked | .0549316 .2443594 0.22 0.822 -.424495 .5343582
# _cons | 6.448836 .2661017 24.23 0.000 5.926752 6.970921
# ------------------------------------------------------------------------------
#
# .
Some of the variables seems to be confounders. Accordingly the strength of the relationship