packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")
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
## [[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"
ess <- read_fst("All-ESS-Data.fst")
ess$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
ess$year[ess$essround == i] <- replacements[i]
}
#Task 1 Do a data summary table of three variables of interest. Discuss briefly what you note (i.e., add text in your markdown after the Task 1 code).
germany_data <- ess %>%
filter(cntry == "DE") %>%
mutate(
pltcare = ifelse(pltcare %in% c(7, 8, 9), NA, pltcare),
prtdgcl = ifelse(prtdgcl %in% c(6,7, 8, 9), NA, prtdgcl),
trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep),
)
table(germany_data$pltcare)
##
## 1 2 3 4 5
## 840 1063 777 181 40
table(germany_data$prtdgcl)
##
## 1 2 3 4
## 1277 9902 4872 336
table(germany_data$trstep)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 3233 1794 3094 4223 3869 6707 3508 3084 2021 550 362
datasummary_skim(germany_data %>% select(pltcare, prtdgcl, trstep))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| pltcare | 6 | 92 | 2.1 | 1.0 | 1.0 | 2.0 | 5.0 | |
| prtdgcl | 5 | 52 | 2.3 | 0.6 | 1.0 | 2.0 | 4.0 | |
| trstep | 12 | 6 | 4.2 | 2.4 | 0.0 | 5.0 | 10.0 |
#trstep has a very high mean compared to pltcare and prtdgcl. The largest number of responses for trstep were seen in the "5" response which is the median response for the level of Trust in the European Parliament prompt.For the prtdgcl variable, a lot of respondents answered option 2 which is "quite close" for the prompt : How close to party. For the pltcare prompt, which asks "do you think that politicians in general care what people like you think?", the highest number of responses were seen in value 2 which is "Very few care".
#Task 2 Choose one of the three variables you just summarized in the table. This will be your current main outcome of interest.
Produce a visual that showcases the mean (average) for your outcome of interest by survey year (can be, e.g., point + line plot or ridge plot, depending on your variable). Discuss briefly what you note (i.e., add text in your markdown after the Task 2 code).
trust_in_eup <- germany_data %>%
group_by(year) %>%
summarize(mean_trust_eup = mean(trstep, na.rm = TRUE))
trust_in_eup
## # A tibble: 10 × 2
## year mean_trust_eup
## <dbl> <dbl>
## 1 2002 4.46
## 2 2004 4.17
## 3 2006 4.00
## 4 2008 4.27
## 5 2010 3.93
## 6 2012 4.29
## 7 2014 3.99
## 8 2016 4.35
## 9 2018 4.56
## 10 2020 4.25
ggplot(trust_in_eup, aes(x = year, y = mean_trust_eup)) +
geom_line(color = "black", size = 1) +
geom_point(color = "purple", size = 3) +
labs(title = " Trust in the European Parliament in Germany (2002-2020)",
x = "Survey Year",
y = "Trust (0-10)") +
ylim(0, 10) + # Setting the y-axis limits from 0 to 10
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# It can be seen that the mean ranges from the low end of 3.927338 (2010) to the high end of 4.562143 (2018). If we view the means historically, there have been no dramatic increases or decreases in how respondents respond to the question "Trust in the European Parliament" ; the low mean(s) indicate that many German respondents do not really trust in the European Parliament.
#Task 3 Provide a comparison visual of your outcome of interest with two other countries. You can choose the geom() you prefer. Discuss briefly what you note (i.e., add text in your markdown after the Task 3 code).
ess_selected <- ess %>%
filter(cntry %in% c("DE", "HR", "PT")) %>%
mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep))
task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -trstep, FUN=median), y = trstep, fill = cntry)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Boxplot comparison for Trust in the European Parliament (Germany, Croatia, Portugal)",
x = "Country",
y = "Scale (0 = No trust at all - 10 = Complete trust")
task3plot
## Warning: Removed 4805 rows containing non-finite values (`stat_boxplot()`).
# It can be seen that both Germany (DE) and Portugal (PT) have the same median meaning that in both countries, respondents are in the middle when it comes to trusting the European parliament while in Croatia (HR), respondents are on average, less likely to trust the European parliament.
#Task 4 Produce a cross-tab between your outcome of interest and a socio-demographic variable (use datasummary_crosstab). Then, calculate column percentages using cprop(), making sure to pick a second socio-demographic variable. Discuss briefly what you note (i.e., add text in your markdown after the Task 4 code).
germany_data <- germany_data %>%
mutate(
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
table(germany_data$educ_level)
##
## BA No BA
## 8783 25642
trstepedu <- datasummary_crosstab(trstep ~ educ_level, data = germany_data)
trstepedu
| trstep | BA | No BA | All | |
|---|---|---|---|---|
| 0 | N | 575 | 2658 | 3233 |
| % row | 17.8 | 82.2 | 100.0 | |
| 1 | N | 404 | 1390 | 1794 |
| % row | 22.5 | 77.5 | 100.0 | |
| 2 | N | 731 | 2363 | 3094 |
| % row | 23.6 | 76.4 | 100.0 | |
| 3 | N | 1014 | 3209 | 4223 |
| % row | 24.0 | 76.0 | 100.0 | |
| 4 | N | 991 | 2878 | 3869 |
| % row | 25.6 | 74.4 | 100.0 | |
| 5 | N | 1699 | 5008 | 6707 |
| % row | 25.3 | 74.7 | 100.0 | |
| 6 | N | 1113 | 2395 | 3508 |
| % row | 31.7 | 68.3 | 100.0 | |
| 7 | N | 1038 | 2046 | 3084 |
| % row | 33.7 | 66.3 | 100.0 | |
| 8 | N | 692 | 1329 | 2021 |
| % row | 34.2 | 65.8 | 100.0 | |
| 9 | N | 191 | 359 | 550 |
| % row | 34.7 | 65.3 | 100.0 | |
| 10 | N | 81 | 281 | 362 |
| % row | 22.4 | 77.6 | 100.0 | |
| All | N | 8783 | 25642 | 34425 |
| % row | 25.5 | 74.5 | 100.0 |
table(germany_data$trstep, germany_data$educ_level) %>%
cprop()
##
## BA No BA All
## 0 6.7 11.1 10.0
## 1 4.7 5.8 5.5
## 2 8.6 9.9 9.5
## 3 11.9 13.4 13.0
## 4 11.6 12.0 11.9
## 5 19.9 20.9 20.7
## 6 13.0 10.0 10.8
## 7 12.2 8.6 9.5
## 8 8.1 5.6 6.2
## 9 2.2 1.5 1.7
## 10 0.9 1.2 1.1
## Total 100.0 100.0 100.0
germany_data <- germany_data %>%
mutate(religion = case_when(
rlgblg == 2 ~ "No",
rlgblg == 1 ~ "Yes",
rlgblg %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(rlgblg)
))
table(germany_data$religion)
##
## No Yes
## 11417 14197
table(germany_data$trstep, germany_data$religion) %>%
cprop()
##
## No Yes All
## 0 11.5 7.5 9.3
## 1 6.4 4.5 5.4
## 2 10.5 8.7 9.5
## 3 14.0 13.1 13.5
## 4 12.2 13.2 12.8
## 5 20.1 21.5 20.9
## 6 9.6 12.2 11.0
## 7 8.4 10.1 9.4
## 8 4.9 6.5 5.8
## 9 1.3 1.5 1.4
## 10 0.9 1.2 1.1
## Total 100.0 100.0 100.0
# Overall, the numbers on the cross-tabs show that there are many more Non BA holding respondents than those who do hold a BA, the amount of BA holders is always signifcantly less than those who dont hold BAs.It can also be seen that the peak of responses for both BA and non BA holders is at the median value (5) which is in the middle of the scale representing Trust in the European Parliament, this shows that way more respondents answered in the middle than anywhere else, regardless of their BA status.
# As for the variable of religion, it can be seen that those who do belong to a certain religion or denomination have less trust up until the median variable (5) than those who don't belong to any group, from the median onwards they begin to show higher numbers of respondents who trust in the European parliament.
#Task 5 Choose one of the two socio-demographic variables you just worked with. Visualize the conditional probability (or column percentages) of your outcome given your selected socio-dem variable. Discuss briefly what you note (i.e., add text in your markdown after the Task 5 code).
dde <- germany_data %>%
filter(!is.na(educ_level) & !is.na(trstep))
dde <- dde %>%
mutate(trstep = ifelse(trstep %in% c(77, 88, 99), NA, trstep))
table(dde$trstep)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 3233 1794 3094 4223 3869 6707 3508 3084 2021 550 362
table(dde$trstep, dde$educ_level) %>%
cprop() %>%
as.data.frame() %>%
filter(Var1 != "Total",
Var2 != "All") %>%
ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
geom_col(position = "dodge") +
labs(title="Trusting the European Parliament as a German respondent",
y = "Conditional Percentage",
x = "Trust level (0 = No trust at all - 10 = Complete trust)",
fill = "At least BA vs. Not")
# It can be seen based off this graph that there is a unimodal peak, when the Trust Level is 5, both BA and non BA response percentages hit a high peak, it can be seen that before this peak, there was a higher percentage of responses from Non BA holders but after the peak, there is a higher percentage of BA responses. This could possibly mean that BA holders are more likely to trust in the European parliament than non BA holders, hence the higher percentages after the median value of 5. Overall, as the scale hits 10 (complete trust), the percentage of respondents both BA and Non BA decreases to the lowest overall, meaning that a very low eprcentage of BA and non BA repsondents had full complete trust in the European Parliament.