##Set Environment
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr", "infer", "broom", "remotes", "effects", "packrat", "rsconnect")
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
lapply(packages, library, character.only = TRUE)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.3 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.0
## ✔ ggplot2 3.4.3 ✔ tibble 3.2.1
## ✔ lubridate 1.9.2 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: viridisLite
##
##
## Attaching package: 'kableExtra'
##
##
## The following object is masked from 'package:dplyr':
##
## group_rows
##
##
## Loading required package: carData
##
## lattice theme set by effectsTheme()
## See ?effectsTheme for details.
##
##
## Attaching package: 'packrat'
##
##
## The following object is masked from 'package:remotes':
##
## install_local
## [[1]]
## [1] "lubridate" "forcats" "stringr" "dplyr" "purrr" "readr"
## [7] "tidyr" "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [13] "grDevices" "utils" "datasets" "methods" "base"
##
## [[2]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [16] "datasets" "methods" "base"
##
## [[3]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [16] "datasets" "methods" "base"
##
## [[4]]
## [1] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [6] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [11] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [16] "utils" "datasets" "methods" "base"
##
## [[5]]
## [1] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [6] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [11] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[6]]
## [1] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "stats" "graphics" "grDevices" "utils" "datasets"
## [21] "methods" "base"
##
## [[7]]
## [1] "knitr" "viridis" "viridisLite" "fst" "RColorBrewer"
## [6] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [11] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [16] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "methods" "base"
##
## [[8]]
## [1] "kableExtra" "knitr" "viridis" "viridisLite" "fst"
## [6] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [11] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [16] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [21] "utils" "datasets" "methods" "base"
##
## [[9]]
## [1] "rmarkdown" "kableExtra" "knitr" "viridis" "viridisLite"
## [6] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [21] "grDevices" "utils" "datasets" "methods" "base"
##
## [[10]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[11]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "fst" "RColorBrewer" "modelsummary" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[12]]
## [1] "questionr" "ggridges" "rmarkdown" "kableExtra" "knitr"
## [6] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [11] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [16] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
##
## [[13]]
## [1] "infer" "questionr" "ggridges" "rmarkdown" "kableExtra"
## [6] "knitr" "viridis" "viridisLite" "fst" "RColorBrewer"
## [11] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [16] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [21] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [26] "datasets" "methods" "base"
##
## [[14]]
## [1] "broom" "infer" "questionr" "ggridges" "rmarkdown"
## [6] "kableExtra" "knitr" "viridis" "viridisLite" "fst"
## [11] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [16] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [21] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [26] "utils" "datasets" "methods" "base"
##
## [[15]]
## [1] "remotes" "broom" "infer" "questionr" "ggridges"
## [6] "rmarkdown" "kableExtra" "knitr" "viridis" "viridisLite"
## [11] "fst" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [16] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [21] "tibble" "ggplot2" "tidyverse" "stats" "graphics"
## [26] "grDevices" "utils" "datasets" "methods" "base"
##
## [[16]]
## [1] "effects" "carData" "remotes" "broom" "infer"
## [6] "questionr" "ggridges" "rmarkdown" "kableExtra" "knitr"
## [11] "viridis" "viridisLite" "fst" "RColorBrewer" "modelsummary"
## [16] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [21] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [26] "stats" "graphics" "grDevices" "utils" "datasets"
## [31] "methods" "base"
##
## [[17]]
## [1] "packrat" "effects" "carData" "remotes" "broom"
## [6] "infer" "questionr" "ggridges" "rmarkdown" "kableExtra"
## [11] "knitr" "viridis" "viridisLite" "fst" "RColorBrewer"
## [16] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [21] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [26] "tidyverse" "stats" "graphics" "grDevices" "utils"
## [31] "datasets" "methods" "base"
##
## [[18]]
## [1] "rsconnect" "packrat" "effects" "carData" "remotes"
## [6] "broom" "infer" "questionr" "ggridges" "rmarkdown"
## [11] "kableExtra" "knitr" "viridis" "viridisLite" "fst"
## [16] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [21] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [26] "ggplot2" "tidyverse" "stats" "graphics" "grDevices"
## [31] "utils" "datasets" "methods" "base"
ess <- read_fst("/Users/sarah/Documents/UOFT/Second Year/SOC202/All-ESS-Data.fst")
##CONTEXT
germany_data <- ess %>%
filter (cntry == "DE") %>%
mutate (
edulvla = edulvla %>%
na_if(55) %>%
na_if(77) %>%
na_if(88) %>%
na_if(99),
edulvlb = edulvlb %>%
na_if(5555) %>%
na_if(7777) %>%
na_if(8888) %>%
na_if(9999),
educ_level = case_when(
essround < 5 & edulvla == 5 ~ 1,
essround >= 5 & edulvlb > 600 ~ 1,
TRUE ~ 0
),
trstprl = ifelse(trstprl %in% c(77, 88, 99), NA, trstprl),
imbgeco = ifelse(imbgeco %in% c(77, 88, 99), NA, imbgeco)
)
#Checking all the table’s
table(germany_data$educ_level)
##
## 0 1
## 26041 8384
table(germany_data$trstprl)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 2385 1197 2764 4342 3964 6564 3775 4130 3010 934 727
table(germany_data$imbgeco)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1498 777 1833 2612 2541 8081 3721 4929 4363 1251 1911
##Creating the data summary table
d <- germany_data %>%
mutate(
educ_level = as.numeric(educ_level),
trstprl = as.numeric(trstprl),
imbgeco = as.numeric(imbgeco)
)
table1 <- datasummary_skim(d %>% dplyr::select(educ_level, trstprl, imbgeco), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
table1
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
educ_level | 2 | 0 | 0.2 | 0.4 | 0.0 | 0.0 | 1.0 |
trstprl | 12 | 2 | 4.7 | 2.5 | 0.0 | 5.0 | 10.0 |
imbgeco | 12 | 3 | 5.5 | 2.4 | 0.0 | 5.0 | 10.0 |
d <- d %>%
rename(
`Educational Attainment` = educ_level,
`Trust in Country's Parliament` = trstprl,
`Immigration Bad or Good for Country's Economy` = imbgeco
)
table1_v2 <- datasummary_skim(d %>% dplyr::select(`Educational Attainment`,`Trust in Country's Parliament`, `Immigration Bad or Good for Country's Economy`), title = "Descriptive Statistics of Variables", output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
table1_v2
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
Educational Attainment | 2 | 0 | 0.2 | 0.4 | 0.0 | 0.0 | 1.0 |
Trust in Country's Parliament | 12 | 2 | 4.7 | 2.5 | 0.0 | 5.0 | 10.0 |
Immigration Bad or Good for Country's Economy | 12 | 3 | 5.5 | 2.4 | 0.0 | 5.0 | 10.0 |
##FINDINGS
##Null Distribution
find <- germany_data %>%
filter (cntry == "DE") %>%
mutate (
edulvla = edulvla %>%
na_if(55) %>%
na_if(77) %>%
na_if(88) %>%
na_if(99),
edulvlb = edulvlb %>%
na_if(5555) %>%
na_if(7777) %>%
na_if(8888) %>%
na_if(9999),
educ_level = case_when(
essround < 5 & edulvla == 5 ~ 'BA',
essround >= 5 & edulvlb > 600 ~ 'BA',
TRUE ~ 'No BA'
),
trstprl = ifelse(trstprl %in% c(77, 88, 99), NA, trstprl),
imbgeco = ifelse(imbgeco %in% c(77, 88, 99), NA, imbgeco)
)
test_stat <- find %>%
specify(explanatory = educ_level,
response = imbgeco) %>%
hypothesize(null = "independence") %>%
calculate(stat = "t")
## Warning: Removed 908 rows containing missing values.
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "BA" - "No BA", or divided in the order "BA" / "No BA" for ratio-based
## statistics. To specify this order yourself, supply `order = c("BA", "No BA")`
## to the calculate() function.
print(test_stat$stat)
## t
## 42.79391
#A t-statistic of 42.79391 is quite large, indicating a significant difference in attitudes towards immigration being bad or good for country’s economy between those with a BA and those without a BA. It suggests that the mean attitudes towards immigration is different for the two groups.
null_dist <- find %>%
specify(explanatory = educ_level,
response = imbgeco) %>%
hypothesize(null = "independence") %>%
generate(reps = 1000, type = "permute") %>%
calculate(stat = "t")
## Warning: Removed 908 rows containing missing values.
## Warning: The statistic is based on a difference or ratio; by default, for
## difference-based statistics, the explanatory variable is subtracted in the
## order "BA" - "No BA", or divided in the order "BA" / "No BA" for ratio-based
## statistics. To specify this order yourself, supply `order = c("BA", "No BA")`
## to the calculate() function.
null_dist
## Response: imbgeco (numeric)
## Explanatory: educ_level (factor)
## Null Hypothesis: independence
## # A tibble: 1,000 × 2
## replicate stat
## <int> <dbl>
## 1 1 0.270
## 2 2 0.188
## 3 3 -1.50
## 4 4 0.480
## 5 5 0.0740
## 6 6 1.00
## 7 7 -1.39
## 8 8 0.0735
## 9 9 1.64
## 10 10 0.882
## # ℹ 990 more rows
p_val <- null_dist %>%
get_pvalue(obs_stat = test_stat, direction = "two-sided")
## Warning: Please be cautious in reporting a p-value of 0. This result is an
## approximation based on the number of `reps` chosen in the `generate()` step.
## See `?get_p_value()` for more information.
p_val
## # A tibble: 1 × 1
## p_value
## <dbl>
## 1 0
#A p-value of 0 means there is strong evidence to reject the null hypothesis.
conf_int <- null_dist %>%
get_confidence_interval(level = 0.95, type = "percentile")
null_dist %>%
visualize() +
shade_p_value(obs_stat = test_stat, direction = "two-sided") +
shade_confidence_interval(endpoints = conf_int)+
labs(title = "Figure 1. Simulation-Based Null Distribution",
x = "T statistic",
y = "Count")
#With a 95% confidence interval we can see strong evidence to reject the null hypothesis as the test statistic falls way outside the simulated T Null Distribution. Thus, further showing that there is a relationship between the variable educ_level and imbgeco.
##Regression Modelling
find$weight <- find$dweight * find$pweight
model1 <- lm(imbgeco ~ educ_level, data = find, weights = weight)
model2 <- lm(imbgeco ~ educ_level + trstprl, data = find, weights = weight)
model3 <- lm(imbgeco ~ educ_level + trstprl*educ_level, data = find, weights = weight)
modelsummary(
list(model1, model2, model3),
fmt = 1,
estimate = c( "{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}",
"{estimate} ({std.error}){stars}"),
statistic = NULL,
coef_omit = "Intercept")
| Â (1) | Â Â (2) | Â Â (3) | |
|---|---|---|---|
| educ_levelNo BA | −1.2 (0.0)*** | −0.9 (0.0)*** | −1.1 (0.1)*** |
| trstprl | 0.3 (0.0)*** | 0.3 (0.0)*** | |
| educ_levelNo BA × trstprl | 0.0 (0.0)* | ||
| Num.Obs. | 33517 | 33001 | 33001 |
| R2 | 0.045 | 0.137 | 0.138 |
| R2 Adj. | 0.045 | 0.137 | 0.138 |
| AIC | 157182.2 | 151317.7 | 151314.0 |
| BIC | 157207.5 | 151351.3 | 151356.0 |
| Log.Lik. | −78588.098 | −75654.866 | −75652.005 |
| RMSE | 2.39 | 2.25 | 2.25 |
##Boxplot
figure2plot <- ggplot(find, aes(x = reorder(educ_level, -imbgeco, FUN=median), y = imbgeco, fill = educ_level)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Figure 2. Boxplot Comparison for Attitude Towards Immigration and Educational Attainment in Germany",
x = "Educational Attainment",
y = "Attitude Towards Immigration (0-10)")
figure2plot
## Warning: Removed 908 rows containing non-finite values (`stat_boxplot()`).