# List of packages
packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")
# Install packages if they aren't installed already
new_packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)
# Load the 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")
setwd("/Users/Yixuan/Homework_5_Project")
Important: Must post link to your markdown on the discussion board (file and “knit” html), along with post detailing your general takeaways from exploring variables of interest, comparing to other countries, and looking into socio-demographics. You must also attach your R markdown file.
In your post, please link to the ESS website for the three variables you considered, as well as highlight the country you wish to focus on and for what main reason. Finally, please attach (or provide a screenshot) the visual that you produced while doing the tasks that is most interesting to you and explain why (just one visual not all).
You cannot use the same three variables we considered in the tutorial. You can use one of the three, but not all three.
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).
Create from corresponding essrounds.
table(ess$essround)
##
## 1 2 3 4 5 6 7 8 9 10
## 42359 47537 43000 56752 52458 54673 40185 44387 49519 59685
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]
}
table(ess$wrclmch)
##
## 1 2 3 4 5 6 7 8 9
## 4793 16034 44210 28111 7832 1954 63 645 430
table(ess$trstlgl)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 39001 21330 33539 44149 42941 76019 50619 62821 61714 29185 16544 451 11587
## 99
## 655
table(ess$happy)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 3933 3540 7343 13759 17016 52117 43707 87869 130802 76229 51006
## 77 88 99
## 330 2163 741
cyprus_data <- ess %>%
filter(cntry == "CY") %>%
mutate(
wrclmch = ifelse(pplfair == 2, 0, ifelse(wrclmch %in% c(7, 8, 9), NA, wrclmch)),
trstlgl = ifelse(trstlgl %in% c(77, 88, 99), NA, trstlgl),
happy = ifelse(happy %in% c(77, 88, 99), NA, happy),
)
Then do a datasummary_skim
datasummary_skim(cyprus_data %>% select(wrclmch, trstlgl, happy))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| wrclmch | 8 | 77 | 1.9 | 1.8 | 0.0 | 2.0 | 6.0 | |
| trstlgl | 12 | 3 | 5.3 | 2.7 | 0.0 | 5.0 | 10.0 | |
| happy | 12 | 1 | 7.3 | 1.9 | 0.0 | 8.0 | 10.0 |
In summary, the variable ‘wrclmch’ has only eight unique values, but suffers from a data missing rate of up to 77%. Its mean is 1.9 and its standard deviation is 1.8. The variable ‘trstlgl’ shows 12 unique values and only 3% of the data is missing. It has a higher mean of 5.3 and a higher standard deviation of 2.7. Similarly, the ‘happy’ variable also showed 12 unique values with a data missing rate as low as 1%, a relatively high mean of 7.3, a standard deviation of 1.9, a range of values from 0.0 to 10.0, and a median of 8.0. ‘trstlgl’ and ‘happy’ have more unique values than ‘wrclmch’, but the latter has a higher percentage of missing data.
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_by_year <- cyprus_data %>%
group_by(year) %>%
summarize(mean_trust = mean(trstlgl, na.rm = TRUE))
trust_by_year
## # A tibble: 6 × 2
## year mean_trust
## <dbl> <dbl>
## 1 2006 6.07
## 2 2008 6.24
## 3 2010 5.63
## 4 2012 4.82
## 5 2018 4.42
## 6 2020 4.41
#Visualizing + Exploring
ggplot(trust_by_year, aes(x = year, y = mean_trust)) +
geom_line(color = "blue", size = 1) +
geom_point(color = "red", size = 3) +
labs(title = "Trust in the legal system in Cyprus (2002-2020)",
x = "Survey Year",
y = "Trust (0-10)") +
ylim(0, 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.
The graph shows that trust in the legal system in Cyprus gradually
declines from 2002 to 2020 as time goes on. The decline from 2006 to
2010 was relatively large and has been slow since 2012.
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("GB", "CY", "HU")) %>%
mutate(trstlgl = ifelse(trstlgl %in% c(77, 88, 99), NA, trstlgl))
task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -trstlgl, FUN=median), y = trstlgl, fill = cntry)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Boxplot comparison for trust in the legal system (Great Britian, Finland, Hungary)",
x = "Country",
y = "Scale (0-10)")
task3plot
## Warning: Removed 1130 rows containing non-finite values (`stat_boxplot()`).
In this boxplot, we can see that they all have the same median and Q1,
but Hungary will have a slightly longer scale than the other two. And it
is obvious that Cyprus and Great Britain have the same boxplot,
definitely same IQR and median.
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).
Place of residence
cyprus_data <- cyprus_data %>%
mutate(geo = recode(as.character(domicil),
'1' = "Urban",
'2' = "Peri-Urban",
'3' = "Rural",
'4' = "Rural",
'5' = "Rural",
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_))
table(cyprus_data$geo)
##
## Peri-Urban Rural Urban
## 865 2981 2203
table(cyprus_data$domicil)
##
## 1 2 3 4 5 9
## 2203 865 1035 1945 1 16
trstlglgeo <- datasummary_crosstab(trstlgl ~ geo, data = cyprus_data)
trstlglgeo
| trstlgl | Peri-Urban | Rural | Urban | All | |
|---|---|---|---|---|---|
| 0 | N | 67 | 199 | 132 | 401 |
| % row | 16.7 | 49.6 | 32.9 | 100.0 | |
| 1 | N | 48 | 135 | 78 | 262 |
| % row | 18.3 | 51.5 | 29.8 | 100.0 | |
| 2 | N | 53 | 158 | 119 | 334 |
| % row | 15.9 | 47.3 | 35.6 | 100.0 | |
| 3 | N | 82 | 238 | 153 | 474 |
| % row | 17.3 | 50.2 | 32.3 | 100.0 | |
| 4 | N | 63 | 218 | 180 | 461 |
| % row | 13.7 | 47.3 | 39.0 | 100.0 | |
| 5 | N | 155 | 570 | 405 | 1133 |
| % row | 13.7 | 50.3 | 35.7 | 100.0 | |
| 6 | N | 90 | 303 | 213 | 607 |
| % row | 14.8 | 49.9 | 35.1 | 100.0 | |
| 7 | N | 113 | 372 | 297 | 784 |
| % row | 14.4 | 47.4 | 37.9 | 100.0 | |
| 8 | N | 106 | 388 | 301 | 795 |
| % row | 13.3 | 48.8 | 37.9 | 100.0 | |
| 9 | N | 45 | 199 | 150 | 395 |
| % row | 11.4 | 50.4 | 38.0 | 100.0 | |
| 10 | N | 24 | 120 | 119 | 263 |
| % row | 9.1 | 45.6 | 45.2 | 100.0 | |
| All | N | 865 | 2981 | 2203 | 6065 |
| % row | 14.3 | 49.2 | 36.3 | 100.0 |
Paid work status
cyprus_data <- cyprus_data %>%
mutate(pdwrk_recode = case_when(
pdwrk == 1 ~ 'yes',
pdwrk == 0 ~ 'no',
))
table(cyprus_data$pdwrk_recode)
##
## no yes
## 2819 3246
table(cyprus_data$pdwrk)
##
## 0 1
## 2819 3246
trstlglpdwrk <- datasummary_crosstab(trstlgl ~ pdwrk, data = cyprus_data)
trstlglpdwrk
| trstlgl | 0 | 1 | All | |
|---|---|---|---|---|
| 0 | N | 169 | 232 | 401 |
| % row | 42.1 | 57.9 | 100.0 | |
| 1 | N | 128 | 134 | 262 |
| % row | 48.9 | 51.1 | 100.0 | |
| 2 | N | 149 | 185 | 334 |
| % row | 44.6 | 55.4 | 100.0 | |
| 3 | N | 190 | 284 | 474 |
| % row | 40.1 | 59.9 | 100.0 | |
| 4 | N | 198 | 263 | 461 |
| % row | 43.0 | 57.0 | 100.0 | |
| 5 | N | 544 | 589 | 1133 |
| % row | 48.0 | 52.0 | 100.0 | |
| 6 | N | 258 | 349 | 607 |
| % row | 42.5 | 57.5 | 100.0 | |
| 7 | N | 343 | 441 | 784 |
| % row | 43.8 | 56.2 | 100.0 | |
| 8 | N | 370 | 425 | 795 |
| % row | 46.5 | 53.5 | 100.0 | |
| 9 | N | 192 | 203 | 395 |
| % row | 48.6 | 51.4 | 100.0 | |
| 10 | N | 166 | 97 | 263 |
| % row | 63.1 | 36.9 | 100.0 | |
| All | N | 2819 | 3246 | 6065 |
| % row | 46.5 | 53.5 | 100.0 |
It can be seen from these data that people’s trust in the legal system has a certain relationship with their place in residence. People living in Peri-Urban are less likely to believe in the legal system, while most people living in rural areas believe in it. Probably because they’re pinning their hopes on the legal system. And in the second part, there wasn’t much difference between yes or no in people’s paid work status.
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).
df <- cyprus_data %>%
filter(!is.na(pdwrk) & !is.na(trstlgl))
df <- df %>%
mutate(trstlgl = case_when(
trstlgl == 1 ~ "Yes",
trstlgl == 0 ~ "No",
TRUE ~ as.character(trstlgl)
))
df_probs <- cyprus_data %>%
count(trstlgl, pdwrk) %>%
group_by(pdwrk) %>%
mutate(prob = n / sum(n))
ggplot(df_probs, aes(x = as.factor(trstlgl), y = prob, color = pdwrk)) +
geom_point() +
geom_line(aes(group = pdwrk)) +
labs(title = "Conditional Probabilities of Trust in Legal System",
subtitle = "by paid work status",
x = "Level of Trust",
y = "Probability") +
theme_minimal()
In this image, it is evident that individuals at level 5, which signifies a lower degree of trust, exhibit the most pronounced sense of trust, appearing to be in a state of cautious optimism. There are fewer individuals at the extreme ends of the spectrum – those who have unwavering faith and those who harbor complete skepticism.