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")

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]
}

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$cntry)
## 
##    AL    AT    BE    BG    CH    CY    CZ    DE    DK    EE    ES    FI    FR 
##  1201 15225 17451 13240 16925  6065 20090 34425 12408 16856 19452 19532 19038 
##    GB    GR    HR    HU    IE    IL    IS    IT    LT    LU    LV    ME    MK 
## 20979 12558  6535 16642 22233 16218  3975 10178 11652  3187  3921  2478  1429 
##    NL    NO    PL    PT    RO    RS    RU    SE    SI    SK    TR    UA    XK 
## 18329 16065 17689 17881  2146  3548 12458 18216 13484 11292  4272  9987  1295
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$smctmbe)
## 
##     1     2     7     8     9 
## 16295 21193   114  2551    32
table(ess$ipfrule)
## 
##      1      2      3      4      5      6      7      8      9 
##  48835 124274 108574  77317  69214  21325   1622   6875   4458
finland_data <- ess %>% 
  filter(cntry == "FI") %>% 
  mutate(
    trstplc = ifelse(trstplc %in% c(77, 88, 99), NA, trstplc),
    smctmbe = ifelse(smctmbe %in% c(7, 8, 9), NA, smctmbe),
    ipfrule = ifelse(ipfrule %in% c(7, 8, 9), NA, ipfrule),
)

table(finland_data$trstplc)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##  100   75  120  187  330  738  942 2325 5474 6554 2643
datasummary_skim(finland_data %>% select(trstplc, smctmbe, ipfrule))
Unique (#) Missing (%) Mean SD Min Median Max
trstplc 12 0 8.1 1.7 0.0 8.0 10.0
smctmbe 3 90 1.6 0.5 1.0 2.0 2.0
ipfrule 7 7 3.0 1.3 1.0 3.0 6.0

One thing that stands out is the fact that the smctmbe variable (some cultures: much better or all equal) has a missing percentage of 90%. This likely tells us one of two things: 1. the data collected on this variable is seriously lacking, or data has only recently begun to be collected, or 2. subjects usually feel uncomfortable speaking on this topic, due to its sensitive nature.

The mean and median of the trstplc variable are significantly shifted right, so the data visualization is significantly left-skewed.

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_by_year <- finland_data %>%
  group_by(year) %>%
  summarize(mean_trust = mean(trstplc, na.rm = TRUE))
trust_by_year
## # A tibble: 10 × 2
##     year mean_trust
##    <dbl>      <dbl>
##  1  2002       7.95
##  2  2004       7.96
##  3  2006       8.05
##  4  2008       7.99
##  5  2010       8.03
##  6  2012       8.10
##  7  2014       7.90
##  8  2016       8.22
##  9  2018       8.12
## 10  2020       8.34
ggplot(trust_by_year, aes(x = year, y = mean_trust)) +
  geom_line(color = "purple", size = 1) + 
  geom_point(color = "red", size = 3) + 
  labs(title = "Trust in the Police in Finland (2002-2020)", 
       x = "Survey Year", 
       y = "Average 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 mean is fairly high, meaning that the average Finnish person generally trusts the police. The mean has also remained very constant over the last 20 years, so public perceptions of the police have most likely not changed since 2002.

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("FI", "CH", "NL")) %>%
  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 the Police (Finland, Switzerland, Netherlands)", 
       x = "Country", 
       y = "Level of Trust (0-10)")

task3plot
## Warning: Removed 216 rows containing non-finite values (`stat_boxplot()`).

The first thing that jumps out is the number of outliers in Finland, as well as Switzerland. Although public trust in the police is seemingly high in Finland, the number of outliers can raise questions, such as whether those outliers belong to marginalized groups who have perhaps had less pleasant experiences with the police.

When looking past the outliers, the median and all of the quartiles for Finland are higher than both other countries. This could mean that the police in Finland are more trustworthy as an organization than in other countries. However, closer examination of the data is needed before drawing any conclusions.

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).

table(ess$brncntr)
## 
##      1      2      7      8      9 
## 443915  45887     78    108    567
finland_data <- finland_data %>%
  mutate(born_cntry = case_when(
    brncntr == 1 ~ "Born in Country",
    brncntr == 2 ~ "Not Born in Country",
    brncntr %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(brncntr)
  ))

table(finland_data$born_cntry)
## 
##     Born in Country Not Born in Country 
##               18839                 693
trstplc_born_cntry <- datasummary_crosstab(trstplc ~ born_cntry, data = finland_data)

trstplc_born_cntry
trstplc Born in Country Not Born in Country All
0 N 88 12 100
% row 88.0 12.0 100.0
1 N 71 4 75
% row 94.7 5.3 100.0
2 N 113 7 120
% row 94.2 5.8 100.0
3 N 176 11 187
% row 94.1 5.9 100.0
4 N 310 20 330
% row 93.9 6.1 100.0
5 N 711 27 738
% row 96.3 3.7 100.0
6 N 908 34 942
% row 96.4 3.6 100.0
7 N 2250 75 2325
% row 96.8 3.2 100.0
8 N 5311 163 5474
% row 97.0 3.0 100.0
9 N 6370 184 6554
% row 97.2 2.8 100.0
10 N 2492 151 2643
% row 94.3 5.7 100.0
All N 18839 693 19532
% row 96.5 3.5 100.0
finland_data <- finland_data %>%
  mutate(pdwrk_recode = case_when(
    pdwrk == 1 ~ 'PW in last wk',
    pdwrk == 0 ~ 'No PW in last wk', 
  ))

table(finland_data$pdwrk_recode)
## 
## No PW in last wk    PW in last wk 
##             9355            10177
table(finland_data$born_cntry, finland_data$pdwrk_recode) %>%
  cprop()
##                      
##                       No PW in last wk PW in last wk All  
##   Born in Country      96.9             96.1          96.5
##   Not Born in Country   3.1              3.9           3.5
##   Total               100.0            100.0         100.0

Here, the most noticeable thing is the discrepancy between the values for people born in the country vs. people not born in the country. These values do not change much when contrasted with both categories of the variable “pdwrk”, but the difference between the top row of values and the middle row is extreme.

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).

finland_clean <- finland_data %>%
  filter(!is.na(born_cntry) & !is.na(trstplc))

finland_probs <- finland_clean %>%
  count(trstplc, born_cntry) %>%
  group_by(born_cntry) %>%
  mutate(prob = n / sum(n))

ggplot(finland_probs, aes(x = as.factor(trstplc), y = prob, color = born_cntry)) +
  geom_point() +
  geom_line(aes(group = born_cntry)) +
  labs(title = "Trust in the Police",
       subtitle = "by subjects born in Finland vs. not born in Finland",
       x = "Trust (0-10)", 
       y = "Conditional Probability") +
  theme_minimal()

This graph is interesting because it expands on the previous data visualizations from a different perspective. It can also be seen here that the data falls mostly within the upper range of the x-axis (the trust scale). However, upon adding the variable “brncntr” to the equation (born in country vs. not born in country), we can see that although both lines have the same basic shape, the conditional probability of Finish people born in Finland trusting the police is higher than than that of Finish people not born in Finland.