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.3 ✔ 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"
setwd("/Users/leo/Documents/Soc202")
getwd()
## [1] "/Users/leo/Documents/Soc202"
ess <- read_fst("All-ESS-Data.fst")
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).
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$polintr)
##
## 1 2 3 4 7 8 9
## 52651 171323 170951 93989 251 955 435
table(ess$trstplc)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 24330 13723 21599 30024 32899 69638 52804 76423 85520 45796 31750 340 5136
## 99
## 573
table(ess$trstun)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 36141 18141 26456 35081 38942 84213 54835 61626 51175 20924 13556 784 47586
## 99
## 1095
france_data <- ess %>%
filter(cntry == "FR") %>%
mutate(
polintr = ifelse(polintr %in% c(7, 8, 9), NA, polintr),
trstplc = ifelse(trstplc %in% c(77, 88, 99), NA, trstplc),
trstun = ifelse(trstun %in% c(77, 88, 99), NA, trstun),
)
table(france_data$polintr)
##
## 1 2 3 4
## 2713 5720 6947 3648
table(france_data$trstplc)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 539 299 690 1064 1328 3271 2679 3741 3588 1062 730
table(france_data$trstun)
##
## 0 1 2 3 4 5 6 7 8 9 10
## 1239 590 1220 1552 1727 4213 2463 2572 1824 537 330
datasummary_skim(france_data %>% select(polintr, trstplc, trstun))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| polintr | 5 | 0 | 2.6 | 1.0 | 1.0 | 3.0 | 4.0 | |
| trstplc | 12 | 0 | 6.0 | 2.2 | 0.0 | 6.0 | 10.0 | |
| trstun | 12 | 4 | 5.0 | 2.4 | 0.0 | 5.0 | 10.0 |
we can see that France has a very discrete distribution along the variable of polintr which is how interested in politics, impling that France citizens demonstrates varying interested toward politics. The distribution of variable “trstplc” tend to be left skewed, impling that people tend to show higher level of trust toward police. The distribution of the variable “trstun” is mostly normal.
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).
table(ess$year)
##
## 2002 2004 2006 2008 2010 2012 2014 2016 2018 2020
## 42359 47537 43000 56752 52458 54673 40185 44387 49519 59685
trstplc <- france_data %>%
group_by(year) %>%
summarize(mean_trstplc = mean(trstplc, na.rm = TRUE))
trstplc
## # A tibble: 10 × 2
## year mean_trstplc
## <dbl> <dbl>
## 1 2002 5.89
## 2 2004 5.75
## 3 2006 5.75
## 4 2008 5.77
## 5 2010 5.63
## 6 2012 5.93
## 7 2014 6.13
## 8 2016 6.42
## 9 2018 6.47
## 10 2020 6.36
ggplot(trstplc, aes(x = year, y = mean_trstplc)) +
geom_line(color = "blue", size = 1) +
geom_point(color = "red", size = 3) +
labs(title = "France people trust in the police (2002-2020)",
x = "Survey Year",
y = "trust level (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.
We can summarize that France people reveals a relatively stable trend in the level of trust toward police. The mean score is higher than 5 in every year between 2002 and 2020.
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("FR", "GR", "FI")) %>%
mutate(trstplc = ifelse(trstplc %in% c(77, 88, 99), NA, trstplc))
task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -trstplc, FUN=median), y = trstplc, fill = cntry)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Boxplot comparison for trust in police (France, Greece, Finland)",
x = "Country",
y = "Scale (0-10)")
task3plot
## Warning: Removed 149 rows containing non-finite values (`stat_boxplot()`).
According to the visualization, we can see that Finland has many lower outliers. France and Greece demonstrates a similar distribution of dataset with the same median, while France contained one lower outlier. However, they all demonstrates oveall high level of trust toward police.
This could be attributed to the quality security police provided to citizens, so police has gained trust from citizens in these countries.
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).
france_data <- france_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(france_data$geo)
##
## Peri-Urban Rural Urban
## 2361 13087 3584
table(france_data$domicil)
##
## 1 2 3 4 5 7 8
## 3584 2361 6257 5668 1162 3 3
trstplcgeo <- datasummary_crosstab(trstplc ~ geo, data = france_data)
trstplcgeo
| trstplc | Peri-Urban | Rural | Urban | All | |
|---|---|---|---|---|---|
| 0 | N | 59 | 368 | 111 | 539 |
| % row | 10.9 | 68.3 | 20.6 | 100.0 | |
| 1 | N | 35 | 213 | 50 | 299 |
| % row | 11.7 | 71.2 | 16.7 | 100.0 | |
| 2 | N | 85 | 467 | 138 | 690 |
| % row | 12.3 | 67.7 | 20.0 | 100.0 | |
| 3 | N | 102 | 772 | 190 | 1064 |
| % row | 9.6 | 72.6 | 17.9 | 100.0 | |
| 4 | N | 155 | 916 | 257 | 1328 |
| % row | 11.7 | 69.0 | 19.4 | 100.0 | |
| 5 | N | 381 | 2273 | 616 | 3271 |
| % row | 11.6 | 69.5 | 18.8 | 100.0 | |
| 6 | N | 349 | 1819 | 511 | 2679 |
| % row | 13.0 | 67.9 | 19.1 | 100.0 | |
| 7 | N | 486 | 2577 | 677 | 3741 |
| % row | 13.0 | 68.9 | 18.1 | 100.0 | |
| 8 | N | 475 | 2453 | 660 | 3588 |
| % row | 13.2 | 68.4 | 18.4 | 100.0 | |
| 9 | N | 142 | 690 | 228 | 1062 |
| % row | 13.4 | 65.0 | 21.5 | 100.0 | |
| 10 | N | 84 | 509 | 137 | 730 |
| % row | 11.5 | 69.7 | 18.8 | 100.0 | |
| All | N | 2361 | 13087 | 3584 | 19038 |
| % row | 12.4 | 68.7 | 18.8 | 100.0 |
france_data <- france_data %>%
mutate(pdwrk_recode = case_when(
pdwrk == 1 ~ 'yes',
pdwrk == 0 ~ 'no',
))
table(france_data$pdwrk_recode)
##
## no yes
## 9466 9572
table(france_data$pdwrk)
##
## 0 1
## 9466 9572
trstplcpdwrk <- datasummary_crosstab(trstplc ~ pdwrk_recode, data = france_data)
trstplcpdwrk
| trstplc | no | yes | All | |
|---|---|---|---|---|
| 0 | N | 267 | 272 | 539 |
| % row | 49.5 | 50.5 | 100.0 | |
| 1 | N | 151 | 148 | 299 |
| % row | 50.5 | 49.5 | 100.0 | |
| 2 | N | 348 | 342 | 690 |
| % row | 50.4 | 49.6 | 100.0 | |
| 3 | N | 499 | 565 | 1064 |
| % row | 46.9 | 53.1 | 100.0 | |
| 4 | N | 647 | 681 | 1328 |
| % row | 48.7 | 51.3 | 100.0 | |
| 5 | N | 1607 | 1664 | 3271 |
| % row | 49.1 | 50.9 | 100.0 | |
| 6 | N | 1278 | 1401 | 2679 |
| % row | 47.7 | 52.3 | 100.0 | |
| 7 | N | 1760 | 1981 | 3741 |
| % row | 47.0 | 53.0 | 100.0 | |
| 8 | N | 1874 | 1714 | 3588 |
| % row | 52.2 | 47.8 | 100.0 | |
| 9 | N | 556 | 506 | 1062 |
| % row | 52.4 | 47.6 | 100.0 | |
| 10 | N | 447 | 283 | 730 |
| % row | 61.2 | 38.8 | 100.0 | |
| All | N | 9466 | 9572 | 19038 |
| % row | 49.7 | 50.3 | 100.0 |
table(france_data$trstplc, france_data$pdwrk_recode) %>%
cprop()
##
## no yes All
## 0 2.8 2.8 2.8
## 1 1.6 1.5 1.6
## 2 3.7 3.6 3.6
## 3 5.3 5.9 5.6
## 4 6.9 7.1 7.0
## 5 17.0 17.4 17.2
## 6 13.5 14.7 14.1
## 7 18.7 20.7 19.7
## 8 19.9 17.9 18.9
## 9 5.9 5.3 5.6
## 10 4.7 3.0 3.8
## Total 100.0 100.0 100.0
we can see that where the highest concentration of people who have not been paid in the past 7 days is in “8”. It’s close to the concentration of people who have been paid which is “8”. After “8”, there is a significantly decreasing number in people who have not been paid.
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).
df <- france_data %>%
filter(!is.na(geo) & !is.na(trstplc))
table(df$geo)
##
## Peri-Urban Rural Urban
## 2353 13057 3575
france_clean <- france_data %>%
filter(!is.na(geo) & !is.na(trstplc))
france_probs <- france_clean %>%
count(trstplc, geo) %>%
group_by(geo) %>%
mutate(prob = n / sum(n))
ggplot(france_probs, aes(x = as.factor(trstplc), y = prob, color = geo)) +
geom_point() +
geom_line(aes(group = geo)) +
labs(title = "Conditional Probabilities of Police Trust in France",
subtitle = "by Place of Residence",
x = "Satisfaction (0-10)",
y = "Probability") +
theme_minimal()
The distribution is left-skewed, showing that the probability of showing less satisfaction has a smaller probability comparing to showing satisfaction between level 5-8.