Note: The process of re-coding and re-scaling variables was undertaken with the invaluable collaboration of my colleague, Liddell Hastings.
# Setting working directing
setwd(dirname(rstudioapi::getSourceEditorContext()$path))
# Loading relevant packages
library(haven)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(vctrs)
##
## Attaching package: 'vctrs'
## The following object is masked from 'package:dplyr':
##
## data_frame
library(stats)
library(remotes)
library(retroharmonize)
##
## Attaching package: 'retroharmonize'
## The following objects are masked from 'package:haven':
##
## as_factor, read_dta, read_spss
library(modelsummary)
## Warning in !is.null(rmarkdown::metadata$output) && rmarkdown::metadata$output
## %in% : 'length(x) = 2 > 1' in coercion to 'logical(1)'
library(kableExtra)
##
## Attaching package: 'kableExtra'
## The following object is masked from 'package:dplyr':
##
## group_rows
library(jtools)
library(interactions)
library(sandwich)
library(lmtest)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(reshape2)
library(margins)
library(gmodels)
library(knitr)
library(viridis)
## Loading required package: viridisLite
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2022). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.3. https://CRAN.R-project.org/package=stargazer
library(gt)
library(officer)
library(gtsummary)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.3 ✔ tibble 3.2.1
## ✔ purrr 1.0.2 ✔ tidyr 1.3.1
## ✔ readr 2.1.5
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ forcats::as_factor() masks retroharmonize::as_factor(), haven::as_factor()
## ✖ tibble::data_frame() masks vctrs::data_frame(), dplyr::data_frame()
## ✖ dplyr::filter() masks stats::filter()
## ✖ kableExtra::group_rows() masks dplyr::group_rows()
## ✖ dplyr::lag() masks stats::lag()
## ✖ retroharmonize::read_dta() masks haven::read_dta()
## ✖ readr::read_rds() masks retroharmonize::read_rds()
## ✖ retroharmonize::read_spss() masks haven::read_spss()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Loading the dataset
ces21 <- read_dta("2021 Canadian Election Study v2.0.dta")
# Recoding immigrant status
ces21$immigrant <- NA
ces21$immigrant[ces21$cps21_bornin_canada == 1] <- 0 # Canadian-Born
ces21$immigrant[ces21$cps21_bornin_canada == 2] <- 1 # Foreign-Born
# Recoding attitudes towards current immigration levels
ces21 <- ces21 %>%
mutate(imm_level = case_when(ces21$cps21_imm == 2 ~ 0, # # Fewer immigrants
ces21$cps21_imm == 3 ~ 0.5, # About the same
ces21$cps21_imm == 1 ~ 1, # More immigrants
TRUE ~ NA_real_))
# Recoding cultural threat
ces21 <- ces21 %>%
mutate(cultural_threat = case_when
(pes21_fitin == 1 ~ 0, # "Strongly Disagree"
pes21_fitin == 2 ~ 0.25, # "Somewhat Disagree"
pes21_fitin == 3 ~ 0.5, # "Neither Agree nor Disagree"
pes21_fitin == 4 ~ 0.75, # "Somewhat Agree"
pes21_fitin == 5 ~ 1, # "Strongly Agree"
TRUE ~ NA_real_))
# Recoding economic threat (general and personal)
# Over this past year, has the economy/personal financial situation gotten ...
ces21 <- ces21 %>%
mutate(econ_sociotropic = case_when(cps21_econ_retro == 1 ~ 0, # Better
cps21_econ_retro == 2 ~ 0.5, # Same
cps21_econ_retro == 3 ~ 1, # Worse
TRUE ~ NA_real_)) %>%
mutate(econ_egocentric = case_when(cps21_own_fin_retro == 1 ~ 0, #Better
cps21_own_fin_retro == 2 ~ 0.5,#Same
cps21_own_fin_retro == 3 ~ 1,#Worse
TRUE ~ NA_real_))
# Recoding 2021 vote choice
ces21 <- ces21 %>%
mutate(libsup = case_when(pes21_votechoice2021 == 1 | pes21_pr_votechoice == 1 ~ 1,
pes21_votechoice2021 != 1 | pes21_pr_votechoice != 1 ~ 0))
ces21 <- ces21 %>%
mutate(consup = case_when(pes21_votechoice2021 == 2 | pes21_pr_votechoice == 2 ~ 1,
pes21_votechoice2021 != 2 | pes21_pr_votechoice != 2 ~ 0))
ces21 <- ces21 %>%
mutate(ndpsup = case_when(pes21_votechoice2021 == 3 | pes21_pr_votechoice == 3 ~ 1,
pes21_votechoice2021 != 3 | pes21_pr_votechoice != 3 ~ 0))
ces21 <- ces21 %>%
mutate(bqsup= case_when(pes21_votechoice2021 == 4 | pes21_pr_votechoice == 4 ~ 1,
pes21_votechoice2021 != 4 | pes21_pr_votechoice != 4 ~ 0))
ces21 <- ces21 %>%
mutate(greensup= case_when(pes21_votechoice2021 == 5 | pes21_pr_votechoice == 5 ~ 1,
pes21_votechoice2021 != 5 | pes21_pr_votechoice != 5 ~ 0))
ces21 <- ces21 %>%
mutate(ppcsup = case_when(pes21_votechoice2021 == 6 | pes21_pr_votechoice == 6 ~ 1,
pes21_votechoice2021 != 6 | pes21_pr_votechoice != 6 ~ 0))
ces21 <- ces21 %>%
mutate(vote = case_when(libsup == 1 ~ "Liberal",
consup == 1 ~ "Conservative",
ndpsup == 1 ~ "NDP",
bqsup == 1 ~ "Bloq",
greensup == 1 ~ "Green",
ppcsup == 1 ~ "PPC"))
# Recoding gender as binary
ces21$woman <- NA
ces21$woman [ces21$cps21_genderid == 1] <- 0 # Man
ces21$woman [ces21$cps21_genderid == 2] <- 1 # Woman
# Recoding education as binary (completed university degree = 1; did not = 0)
ces21 <- ces21 %>%
mutate(university = case_when(cps21_education == 9 | cps21_education == 10 | cps21_education == 11 ~ 1,
cps21_education <= 8 ~ 0,
TRUE ~ NA_real_))
# Recoding income
ces21 <- ces21 %>%
mutate(income = case_when(cps21_income_number == 0 ~ 1,
(1 & cps21_income_number <= 30000) ~ 2,
cps21_income_number >= 30001 & cps21_income_number <= 60000 ~ 3,
cps21_income_number >= 60001 & cps21_income_number <= 90000 ~ 4,
cps21_income_number >= 90001 & cps21_income_number <= 110000 ~ 5,
cps21_income_number >= 110001 & cps21_income_number <= 150000 ~ 6,
cps21_income_number >= 150001 & cps21_income_number <= 200000 ~ 7,
cps21_income_number > 200000 ~ 8,
TRUE ~ NA_real_))
# Recoding race as binaries
ces21 <- ces21 %>%
mutate(white = case_when(cps21_vismin_9 == 1 &
cps21_vismin_1 != 1 &
cps21_vismin_2 != 1 &
cps21_vismin_3 != 1 &
cps21_vismin_4 != 1 &
cps21_vismin_5 != 1 &
cps21_vismin_6 != 1 &
cps21_vismin_7 != 1 &
cps21_vismin_8 != 1 &
cps21_vismin_10 != 1 ~ 1,
cps21_vismin_9 != 1 &
cps21_vismin_1 == 1 |
cps21_vismin_2 == 1 |
cps21_vismin_3 == 1 |
cps21_vismin_4 == 1 |
cps21_vismin_5 == 1 |
cps21_vismin_6 == 1 |
cps21_vismin_7 == 1 |
cps21_vismin_8 == 1 |
cps21_vismin_10 == 1 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(indigenous = case_when(cps21_vismin_4 == 1 ~ 1,
cps21_vismin_4 != 1 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(southasian = case_when(cps21_vismin_6 == 1 ~ 1,
cps21_vismin_6 != 1 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(asian = case_when(cps21_vismin_2 == 1 | cps21_vismin_7 == 1 ~ 1,
cps21_vismin_2 != 1 | cps21_vismin_7 != 1 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(black = case_when(cps21_vismin_3 == 1 ~ 1,
cps21_vismin_3 != 1 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(latino = case_when(cps21_vismin_5 == 1 ~ 1,
cps21_vismin_5 != 1 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(arab = case_when(cps21_vismin_1 == 1 ~ 1,
cps21_vismin_1 != 1 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(other = case_when(cps21_vismin_10 == 1 ~ 1,
cps21_vismin_10 != 1 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(races = case_when(white == 1 ~ "White",
indigenous == 1 ~ "Indigenous",
southasian == 1 ~ "South Asian",
asian == 1 ~ "Asian",
black == 1 ~ "Black",
latino == 1 ~ "Latino",
arab == 1 ~ "Arab"))
# Recoding urban/rural as a binary
ces21 <- ces21 %>%
mutate(rural = case_when(pes21_rural_urban == 1 | pes21_rural_urban == 2 | pes21_rural_urban == 3 ~ 1,
pes21_rural_urban == 4 | pes21_rural_urban == 5 ~ 0,
TRUE ~ NA_real_))
#Recoding Province of Residence ####
ces21 <- ces21 %>%
mutate(province_pre = case_when(cps21_province == 1 ~ 1, # Alberta
cps21_province == 2 ~ 2, # British Columbia
cps21_province == 3 ~ 3, # Manitoba
cps21_province == 4 ~ 4, # New Brunswick
cps21_province == 5 ~ 5, # Newfoundland
cps21_province == 6 ~ 6, # Northwest Territories
cps21_province == 7 ~ 7, # Nova Scotia
cps21_province == 8 ~ 8, # Nunavut
cps21_province == 9 ~ 9, # Ontario
cps21_province == 10 ~ 10, # Prince Edward Island
cps21_province == 11 ~ 11, # Quebec
cps21_province == 12 ~ 12, # Saskatchewan
cps21_province == 13 ~ 13, # Yukon
TRUE ~ NA_real_)) %>%
mutate(province_post = case_when(pes21_province == 1 ~ 1, # Alberta
pes21_province == 2 ~ 2, # British Columbia
pes21_province == 3 ~ 3, # Manitoba
pes21_province == 4 ~ 4, # New Brunswick
pes21_province == 5 ~ 5, # Newfoundland
pes21_province == 6 ~ 6, # Northwest Territories
pes21_province == 7 ~ 7, # Nova Scotia
pes21_province == 8 ~ 8, # Nunavut
pes21_province == 9 ~ 9, # Ontario
pes21_province == 10 ~ 10, # Prince Edward Island
pes21_province == 11 ~ 11, # Quebec
pes21_province == 12 ~ 12, # Saskatchewan
pes21_province == 13 ~ 13, # Yukon
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(BC = case_when(province_pre == 2 ~ 1,
province_pre != 2 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(Ontario = case_when(province_pre == 9 ~ 1,
province_pre != 9 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(Quebec = case_when(province_pre == 11 ~ 1,
province_pre != 11 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(Alberta = case_when(province_pre == 1 ~ 1,
province_pre != 1 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(Atlantic = case_when(province_pre == 4 | province_pre == 5 | province_pre == 7 | province_pre == 10 ~ 1,
province_pre == 1 | province_pre == 2 | province_pre == 3 |
province_pre == 6 | province_pre == 8 | province_pre == 9 |
province_pre == 11 | province_pre == 12 | province_pre == 13 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(Prarie = case_when(province_pre == 1 | province_pre == 3 | province_pre == 12 ~ 1,
province_pre == 2 | province_pre == 4 | province_pre == 5 |
province_pre == 6 | province_pre == 7 | province_pre == 8 |
province_pre == 9 | province_pre == 10 | province_pre == 11 |
province_pre == 13 ~ 0,
TRUE ~ NA_real_))
ces21 <- ces21 %>%
mutate(Territories = case_when(province_pre == 6 | province_pre == 8 | province_pre == 13 ~ 1,
province_pre == 1 | province_pre == 2 | province_pre == 3 |
province_pre == 4 | province_pre == 5 | province_pre == 7 |
province_pre == 9 | province_pre == 10 | province_pre == 11 |
province_pre == 12 ~ 0,
TRUE ~ NA_real_))
# MODEL 1: CANADIAN-BORN LEVELS OF THREAT TOWARDS IMMIGRATION
## Filter the dataset for Canadian-born residents
canadian_born <- ces21 %>%
filter(immigrant == 0)
model_canadian <- lm(imm_level ~ econ_sociotropic + econ_egocentric + cultural_threat +
university + income + woman + indigenous + southasian +
asian + black + latino + arab + other + consup + ndpsup +
bqsup + greensup + ppcsup + rural + BC + Quebec +
Prarie + Alberta + Territories + Atlantic, data = canadian_born)
# MODEL 2: FOREIGN-BORN LEVELS OF THREAT TOWARDS IMMIGRATION
## Filter the dataset for foreign-born residents
foreign_born <- ces21 %>%
filter(immigrant == 1)
model_foreign <- lm(imm_level ~ econ_sociotropic + econ_egocentric + cultural_threat +
university + income + woman + indigenous + southasian +
asian + black + latino + arab + other + consup + ndpsup +
bqsup + greensup + ppcsup + rural + BC + Quebec +
Prarie + Alberta + Territories + Atlantic, data = foreign_born)
# Viewing the 2 models together
modcomparison <- stargazer(model_canadian, model_foreign,
title = "Modeling perceived cultural and economic anxiety towards immigration between Canadian- and foreign-born voters",
type = "text")
##
## Modeling perceived cultural and economic anxiety towards immigration between Canadian- and foreign-born voters
## ========================================================================
## Dependent variable:
## ----------------------------------------------------
## imm_level
## (1) (2)
## ------------------------------------------------------------------------
## econ_sociotropic -0.061*** -0.068***
## (0.010) (0.024)
##
## econ_egocentric -0.046*** -0.064***
## (0.010) (0.025)
##
## cultural_threat -0.589*** -0.473***
## (0.009) (0.023)
##
## university 0.045*** -0.009
## (0.006) (0.016)
##
## income 0.002 -0.001
## (0.002) (0.005)
##
## woman -0.060*** -0.050***
## (0.006) (0.015)
##
## indigenous 0.009 0.145
## (0.017) (0.113)
##
## southasian 0.064** -0.020
## (0.030) (0.025)
##
## asian 0.018 -0.006
## (0.017) (0.019)
##
## black 0.082** 0.085**
## (0.032) (0.036)
##
## latino 0.059 0.007
## (0.047) (0.036)
##
## arab 0.043 -0.028
## (0.045) (0.041)
##
## other -0.019 -0.040
## (0.021) (0.041)
##
## consup -0.048*** -0.099***
## (0.008) (0.019)
##
## ndpsup 0.004 -0.008
## (0.009) (0.021)
##
## bqsup -0.022** 0.001
## (0.011) (0.057)
##
## greensup -0.015 -0.021
## (0.022) (0.050)
##
## ppcsup -0.158*** -0.139***
## (0.017) (0.048)
##
## rural 0.002 -0.013
## (0.006) (0.020)
##
## BC 0.019* 0.037*
## (0.011) (0.021)
##
## Quebec 0.131*** 0.086***
## (0.008) (0.024)
##
## Prarie 0.029** 0.076**
## (0.013) (0.036)
##
## Alberta -0.017 -0.041
## (0.015) (0.040)
##
## Territories -0.020 0.177
## (0.055) (0.157)
##
## Atlantic 0.039*** 0.064
## (0.013) (0.043)
##
## Constant 0.816*** 0.870***
## (0.014) (0.036)
##
## ------------------------------------------------------------------------
## Observations 9,946 1,831
## R2 0.398 0.307
## Adjusted R2 0.396 0.297
## Residual Std. Error 0.289 (df = 9920) 0.310 (df = 1805)
## F Statistic 262.111*** (df = 25; 9920) 31.962*** (df = 25; 1805)
## ========================================================================
## Note: *p<0.1; **p<0.05; ***p<0.01
write.csv(modcomparison, "modcomparison.csv")
# Convert ordered categorical variables to numeric
canadian_born <- canadian_born %>%
mutate(across(c(econ_sociotropic, econ_egocentric, cultural_threat, cps21_groups_therm_2, imm_level), as.numeric))
foreign_born <- foreign_born %>%
mutate(across(c(econ_sociotropic, econ_egocentric, cultural_threat, cps21_groups_therm_2, imm_level), as.numeric))
total_pop <- ces21 %>%
mutate(across(c(econ_sociotropic, econ_egocentric, cultural_threat, cps21_groups_therm_2, imm_level), as.numeric))
# Calculate average values for Canadian-born
canadian_mean <- canadian_born %>%
summarize(across(c(econ_sociotropic, econ_egocentric, cultural_threat, cps21_groups_therm_2, imm_level), mean, na.rm = TRUE))
## Warning: There was 1 warning in `summarize()`.
## ℹ In argument: `across(...)`.
## Caused by warning:
## ! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
## Supply arguments directly to `.fns` through an anonymous function instead.
##
## # Previously
## across(a:b, mean, na.rm = TRUE)
##
## # Now
## across(a:b, \(x) mean(x, na.rm = TRUE))
# Calculate average values for Foreign-born
foreign_mean <- foreign_born %>%
summarize(across(c(econ_sociotropic, econ_egocentric, cultural_threat, cps21_groups_therm_2, imm_level), mean, na.rm = TRUE))
# Calculate average values for total population
total_mean <- total_pop %>%
summarize(across(c(econ_sociotropic, econ_egocentric, cultural_threat, cps21_groups_therm_2, imm_level), mean, na.rm = TRUE))
# Create a table to compare mean values
comparison_table <- bind_rows(
canadian_mean %>% mutate(Immigrant_Status = "Canadian-Born"),
foreign_mean %>% mutate(Immigrant_Status = "Foreign-Born"),
total_mean %>% mutate(Immigrant_Status = "Total Population")
)
# Display the table
comparison_table %>%
gt() %>%
tab_header(
title = "Comparison of Mean Values by Immigrant Status"
)
| Comparison of Mean Values by Immigrant Status | |||||
| econ_sociotropic | econ_egocentric | cultural_threat | cps21_groups_therm_2 | imm_level | Immigrant_Status |
|---|---|---|---|---|---|
| 0.8009961 | 0.5395714 | 0.5201951 | 54.19915 | 0.4638985 | Canadian-Born |
| 0.7633752 | 0.5505097 | 0.4931283 | 58.47645 | 0.5239018 | Foreign-Born |
| 0.7951879 | 0.5415584 | 0.5160678 | 54.70655 | 0.4729893 | Total Population |
# Convert immigrant status variable to a factor
ces21$immigrant_status <- factor(ces21$immigrant, levels = c(0, 1), labels = c("Canadian-Born", "Foreign-Born"))
# Calculate mean proportions for each combination of race and immigrant status
mean_props <- aggregate(imm_level ~ races + immigrant_status, data = ces21, FUN = mean)
# Create a cross-tabulation of mean proportions
cross_tab <- xtabs(imm_level ~ races + immigrant_status, data = mean_props)
# Calculate the difference between Canadian-born and foreign-born values for each row
diff_column <- cross_tab[,"Canadian-Born"] - cross_tab[,"Foreign-Born"]
# Add the difference column to the sorted cross-tabulation table
cross_tab_with_diff <- cbind(cross_tab, "Difference" = diff_column)
# Print the sorted cross-tabulation table with the difference column
print(cross_tab_with_diff)
## Canadian-Born Foreign-Born Difference
## Arab 0.5352941 0.5896226 -0.05432852
## Asian 0.5280528 0.4829787 0.04507408
## Black 0.5205479 0.6851852 -0.16463724
## Indigenous 0.4161017 0.5454545 -0.12935285
## Latino 0.4507042 0.5543478 -0.10364360
## South Asian 0.5668203 0.5164384 0.05038192
## White 0.4625737 0.5158730 -0.05329927
mean_props5 <- aggregate(cultural_threat ~ races + immigrant_status, data = ces21, FUN = mean)
cross_tab5 <- xtabs(cultural_threat ~ races + immigrant_status, data = mean_props5)
diff_column5 <- cross_tab5[,"Canadian-Born"] - cross_tab5[,"Foreign-Born"]
cross_tab_with_diff5 <- cbind(cross_tab5, "Difference" = diff_column5)
print(cross_tab_with_diff5)
## Canadian-Born Foreign-Born Difference
## Arab 0.4545455 0.4243421 0.03020335
## Asian 0.4275862 0.5521617 -0.12457545
## Black 0.4642857 0.3717391 0.09254658
## Indigenous 0.5556931 0.6388889 -0.08319582
## Latino 0.5526316 0.4423077 0.11032389
## South Asian 0.4180000 0.4882812 -0.07028125
## White 0.5226394 0.4858300 0.03680944
mean_props6 <- aggregate(econ_sociotropic ~ races + immigrant_status, data = ces21, FUN = mean)
cross_tab6 <- xtabs(econ_sociotropic ~ races + immigrant_status, data = mean_props6)
diff_column6 <- cross_tab6[,"Canadian-Born"] - cross_tab6[,"Foreign-Born"]
cross_tab_with_diff6 <- cbind(cross_tab6, "Difference" = diff_column6)
print(cross_tab_with_diff6)
## Canadian-Born Foreign-Born Difference
## Arab 0.8108108 0.6904762 0.12033462
## Asian 0.8069728 0.7864286 0.02054422
## Black 0.7676056 0.6843575 0.08324809
## Indigenous 0.8154246 0.5500000 0.26542461
## Latino 0.8809524 0.7954545 0.08549784
## South Asian 0.8005051 0.7081006 0.09240449
## White 0.7975157 0.7849026 0.01261307
mean_props7 <- aggregate(econ_egocentric ~ races + immigrant_status, data = ces21, FUN = mean)
cross_tab7 <- xtabs(econ_egocentric ~ races + immigrant_status, data = mean_props7)
diff_column7 <- cross_tab7[,"Canadian-Born"] - cross_tab7[,"Foreign-Born"]
cross_tab_with_diff7 <- cbind(cross_tab7, "Difference" = diff_column7)
print(cross_tab_with_diff7)
## Canadian-Born Foreign-Born Difference
## Arab 0.5406977 0.5855856 -0.044887911
## Asian 0.5183767 0.5647773 -0.046400605
## Black 0.5128205 0.5150000 -0.002179487
## Indigenous 0.5846906 0.7000000 -0.115309446
## Latino 0.6111111 0.5212766 0.089834515
## South Asian 0.5115741 0.5357143 -0.024140212
## White 0.5350865 0.5496183 -0.014531845
# Indigenous more difficult to interpret comparing foreign-born since we are
# referring to Indigenous populations who have been in Canada.
mean_props1 <- aggregate(cultural_threat ~ vote + immigrant_status, data = ces21, FUN = mean)
cross_tab1 <- xtabs(cultural_threat ~ vote + immigrant_status, data = mean_props1)
diff_column1 <- cross_tab1[,"Canadian-Born"] - cross_tab1[,"Foreign-Born"]
cross_tab_with_diff1 <- cbind(cross_tab1, "Difference" = diff_column1)
print(cross_tab_with_diff1)
## Canadian-Born Foreign-Born Difference
## Bloq 0.6315007 0.5460526 0.0854481114
## Conservative 0.6662479 0.6710766 -0.0048287361
## Green 0.4044811 0.4042553 0.0002258129
## Liberal 0.4014467 0.4193830 -0.0179363489
## NDP 0.3579492 0.3142857 0.0436634861
## PPC 0.7408192 0.7075472 0.0332720392
mean_props2 <- aggregate(econ_sociotropic ~ vote + immigrant_status, data = ces21, FUN = mean)
cross_tab2 <- xtabs(econ_sociotropic ~ vote + immigrant_status, data = mean_props2)
diff_column2 <- cross_tab2[,"Canadian-Born"] - cross_tab2[,"Foreign-Born"]
cross_tab_with_diff2 <- cbind(cross_tab2, "Difference" = diff_column2)
print(cross_tab_with_diff2)
## Canadian-Born Foreign-Born Difference
## Bloq 0.8074926 0.7638889 0.0436036927
## Conservative 0.8892395 0.8754579 0.0137815799
## Green 0.8040201 0.8043478 -0.0003277256
## Liberal 0.6595613 0.6577993 0.0017620477
## NDP 0.8032209 0.7822823 0.0209386538
## PPC 0.9567039 0.9423077 0.0143962183
mean_props3 <- aggregate(econ_egocentric~ vote + immigrant_status, data = ces21, FUN = mean)
cross_tab3 <- xtabs(econ_egocentric ~ vote + immigrant_status, data = mean_props3)
diff_column3 <- cross_tab3[,"Canadian-Born"] - cross_tab3[,"Foreign-Born"]
cross_tab_with_diff3 <- cbind(cross_tab3, "Difference" = diff_column3)
print(cross_tab_with_diff3)
## Canadian-Born Foreign-Born Difference
## Bloq 0.5024876 0.5394737 -0.03698612
## Conservative 0.5718007 0.5902527 -0.01845199
## Green 0.5374449 0.5588235 -0.02137860
## Liberal 0.4834543 0.5164027 -0.03294843
## NDP 0.5481599 0.5263889 0.02177104
## PPC 0.6547945 0.6727273 -0.01793275
mean_propsa <- aggregate(imm_level ~ vote + immigrant_status, data = ces21, FUN = mean)
cross_taba <- xtabs(imm_level ~ vote + immigrant_status, data = mean_propsa)
diff_columna <- cross_taba[,"Canadian-Born"] - cross_taba[,"Foreign-Born"]
cross_tab_with_diffa <- cbind(cross_taba, "Difference" = diff_columna)
print(cross_tab_with_diffa)
## Canadian-Born Foreign-Born Difference
## Bloq 0.4861516 0.5972222 -0.11107062
## Conservative 0.3342273 0.3476277 -0.01340045
## Green 0.5227273 0.5729167 -0.05018939
## Liberal 0.5719821 0.5951821 -0.02320002
## NDP 0.5656130 0.6160458 -0.05043282
## PPC 0.1647727 0.2884615 -0.12368881
# Calculate mean scores for imm_level, cultural threat, econ_sociotropic, and econ_egocentric --- by race
mean_scores <- aggregate(cbind(imm_level, cultural_threat, econ_sociotropic, econ_egocentric) ~ races + immigrant_status, data = ces21, FUN = mean)
# Print the table of mean scores
print(mean_scores)
## races immigrant_status imm_level cultural_threat econ_sociotropic
## 1 Arab Canadian-Born 0.5444444 0.4722222 0.8444444
## 2 Asian Canadian-Born 0.5146277 0.4288564 0.7819149
## 3 Black Canadian-Born 0.5406977 0.4709302 0.7441860
## 4 Indigenous Canadian-Born 0.4166667 0.5638889 0.8236111
## 5 Latino Canadian-Born 0.5000000 0.5530303 0.8333333
## 6 South Asian Canadian-Born 0.5357143 0.4174107 0.8303571
## 7 White Canadian-Born 0.4549344 0.5283396 0.7914537
## 8 Arab Foreign-Born 0.5597015 0.4179104 0.6641791
## 9 Asian Foreign-Born 0.4468750 0.5572917 0.7885417
## 10 Black Foreign-Born 0.6785714 0.3826531 0.6734694
## 11 Indigenous Foreign-Born 0.5555556 0.6388889 0.5555556
## 12 Latino Foreign-Born 0.5416667 0.4523810 0.7738095
## 13 South Asian Foreign-Born 0.5042735 0.4914530 0.7158120
## 14 White Foreign-Born 0.5005537 0.4903101 0.7846069
## econ_egocentric
## 1 0.5888889
## 2 0.5093085
## 3 0.4941860
## 4 0.6027778
## 5 0.5454545
## 6 0.5223214
## 7 0.5353412
## 8 0.5447761
## 9 0.5781250
## 10 0.4540816
## 11 0.6666667
## 12 0.5357143
## 13 0.5277778
## 14 0.5487265
write.csv(mean_scores, "meanscores.csv")
# Calculate mean scores for imm_level, cultural threat, econ_sociotropic, and econ_egocentric -- by partisanship
mean_scores_pol <- aggregate(cbind(imm_level, cultural_threat, econ_sociotropic, econ_egocentric) ~ vote + immigrant_status, data = ces21, FUN = mean)
# Sort the table by races and then by immigrant status
mean_scores_pol_sorted <- mean_scores_pol %>%
arrange(vote, immigrant_status)
# Print the sorted table of mean scores
print(mean_scores_pol_sorted)
## vote immigrant_status imm_level cultural_threat econ_sociotropic
## 1 Bloq Canadian-Born 0.4834776 0.6317860 0.8064516
## 2 Bloq Foreign-Born 0.5857143 0.5500000 0.7714286
## 3 Conservative Canadian-Born 0.3311025 0.6667841 0.8883410
## 4 Conservative Foreign-Born 0.3476190 0.6704762 0.8752381
## 5 Green Canadian-Born 0.5187166 0.4144385 0.7967914
## 6 Green Foreign-Born 0.5595238 0.4285714 0.7976190
## 7 Liberal Canadian-Born 0.5737410 0.4009974 0.6543492
## 8 Liberal Foreign-Born 0.5940280 0.4218551 0.6524778
## 9 NDP Canadian-Born 0.5596081 0.3681274 0.7977681
## 10 NDP Foreign-Born 0.6189711 0.3143087 0.7829582
## 11 PPC Canadian-Born 0.1611446 0.7447289 0.9578313
## 12 PPC Foreign-Born 0.2812500 0.7239583 0.9479167
## econ_egocentric
## 1 0.5035405
## 2 0.5285714
## 3 0.5755548
## 4 0.5952381
## 5 0.5401070
## 6 0.5595238
## 7 0.4816874
## 8 0.5120712
## 9 0.5517148
## 10 0.5289389
## 11 0.6611446
## 12 0.6770833
write.csv(mean_scores_pol_sorted, "meanscores_pol.csv")