## Setting Working Directory
setwd("~/Desktop/SOC202")

### Loading Packages 
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")
## Task 1

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$lrscale)
## 
##      0      1      2      3      4      5      6      7      8      9     10 
##  16299  10633  24208  41241  41150 139328  40165  44145  34501  11695  18027 
##     77     88     99 
##   9428  57354   2381
table(ess$pplhlp)
## 
##      0      1      2      3      4      5      6      7      8      9     10 
##  25623  21508  39518  56665  52819 100127  57395  65825  45516  13153   9663 
##     77     88     99 
##    117   2111    515
table(ess$polintr)
## 
##      1      2      3      4      7      8      9 
##  52651 171323 170951  93989    251    955    435
UK_data <- ess |>
  filter(cntry == "GB") |>
  mutate(
    lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale),
    pplhlp = ifelse(pplhlp %in% c(77, 88, 99), NA, pplhlp), 
    polintr = ifelse(polintr %in% c(7, 8, 9), NA, polintr),
  )

table(UK_data$lrscale)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##  487  305  802 1728 1935 7778 1959 1837 1149  271  366
table(UK_data$pplhlp)
## 
##    0    1    2    3    4    5    6    7    8    9   10 
##  293  320  767 1545 1950 4595 3087 4217 2946  762  423
table(UK_data$polintr)
## 
##    1    2    3    4 
## 2809 8701 5634 3819
datasummary_skim(UK_data |> select(lrscale,pplhlp, polintr))
Unique (#) Missing (%) Mean SD Min Median Max
lrscale 12 11 5.0 1.9 0.0 5.0 10.0
pplhlp 12 0 5.7 2.0 0.0 6.0 10.0
polintr 5 0 2.5 0.9 1.0 2.0 4.0

For the variable of placement on the left right scale (Lrscale), the distribution is unimodal with a mean of 5.0 (0-10 scale, 0 = left, 10 = right). This indicates the majority of UK residents consider themselves to be in the political centre, and because the graph skewed left and right, there are relatively even amounts of people who are more right or left leaning.

For the variable of interest in politics (polintr), the mean was 2.5 (1-4 scale, 0 = very interested, 4 = not at all interested). This data is relatively centric , the majority of people are either “quite interested” in politics or “hardly interested in politics. Overall the data is slightly left leaning, which means more people are at least “quite interested” in politics than not.

For the variable pplhlp (0-10 scale, 0 = people mostly look out for themselves, 10 = people mostly try to be helpful), most respondents are right in the middle, seeing as the mean is 5.7. Meaning most people perceive others as either somewhat helpful or somewhat out for themselves. The data does not appear to be pulled in either direction; it is quite unimodal. With a median of 6 and a mean of 5.7, it does seem that people in the UK feel that people try to be helpful slightly more than people only looking out for themselves.

All of this information together suggests, UK residents are fairly centrist and don’t lean very much in one way or the other.

## Task 2
interest_by_year <- UK_data |>
  group_by(year) |>
  summarize(mean_interest = mean(polintr, na.rm = TRUE))
interest_by_year
## # A tibble: 10 × 2
##     year mean_interest
##    <dbl>         <dbl>
##  1  2002          2.54
##  2  2004          2.64
##  3  2006          2.55
##  4  2008          2.46
##  5  2010          2.57
##  6  2012          2.62
##  7  2014          2.42
##  8  2016          2.37
##  9  2018          2.42
## 10  2020          2.30
ggplot(interest_by_year, aes(x = year, y = mean_interest)) +
  geom_line(color = "blue", size = 1) + 
  geom_point(color = "red", size = 3) + 
  labs(title = "Interest in Politics in the United Kingdom (2002-2020)", 
       x = "Survey Year", 
       y = "Interest in Politics (1-4)") +
  ylim(1, 4) + 
  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.

My partner and I created a line plot showing the mean of polintr (interest in politics) in the UK from 2002-2020. The responses range from 1 (very interested) to 4 (not at all interested). Overall, responses to this question seem fairly stable, with the mean consistently hovering around 2.5. The mean will slightly fluctuate up and down depending on the year, but never significantly. Interest in politics is lower in 2020 than in previous years, but if the pattern from the last 18 years continues, it will remain stable.

## Task 3
ess_selected <- ess |>
  filter(cntry %in% c("GB", "FR", "DE")) |>
  mutate(polintr = ifelse(polintr %in% c(7, 8, 9), NA, polintr))


task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -polintr, FUN=median), y = polintr, fill = cntry)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Boxplot comparison for interest in politics (United Kingdom, France, Germany)", 
       x = "Country", 
       y = "Scale (1-4)")

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

The boxplots compare interest in politics in Germany, France, and the United Kingdom. The interquartile ranges of each country are identical, meaning all three countries mostly feel “quite interested” in politics or “hardly interested in politics,” not one extreme or the other. Germany and the United Kingdom have medians of 2, meaning the most common response was “quite interested in politics,” while France has a median of 3, meaning the most common response was “hardly interested in politics.” From this, we would assume that people in Germany and the United Kingdom care more about politics than people in France. However, all three countries appear to be more centric than anything.

## Task 4
UK_data <- UK_data |>
  mutate(religion = case_when(
    rlgblg == 2 ~ "No",
    rlgblg == 1 ~ "Yes",
    rlgblg %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(rlgblg)
  ))

table(UK_data$religion)
## 
##    No   Yes 
## 10923 10020
table(UK_data$rlgblg)
## 
##     1     2     7     8 
## 10020 10923     9    27
polintr_religion <- datasummary_crosstab(polintr ~ religion, data = UK_data)
polintr_religion
polintr No Yes All
1 N 1346 1458 2809
% row 47.9 51.9 100.0
2 N 4242 4441 8701
% row 48.8 51.0 100.0
3 N 3045 2584 5634
% row 54.0 45.9 100.0
4 N 2283 1531 3819
% row 59.8 40.1 100.0
All N 10923 10020 20979
% row 52.1 47.8 100.0
UK_data <- UK_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(UK_data$educ_level)
## 
##    BA No BA 
##  6116 14863
polintredu <- datasummary_crosstab(polintr ~ educ_level, data = UK_data)
polintredu
polintr BA No BA All
1 N 1416 1393 2809
% row 50.4 49.6 100.0
2 N 2974 5727 8701
% row 34.2 65.8 100.0
3 N 1257 4377 5634
% row 22.3 77.7 100.0
4 N 466 3353 3819
% row 12.2 87.8 100.0
All N 6116 14863 20979
% row 29.2 70.8 100.0
table(UK_data$polintr, UK_data$educ_level) |>
  cprop()
##        
##         BA    No BA All  
##   1      23.2   9.4  13.4
##   2      48.7  38.6  41.5
##   3      20.6  29.5  26.9
##   4       7.6  22.6  18.2
##   Total 100.0 100.0 100.0

The first crosstab separates respondents into two groups, those who consider themselves religious, and those who do not. It then shows the responses of interest in politics for both groups. This data shows that people who consider themselves religious tend to be more interested in politics than people who don’t.

The second crosstab separates respondents into two groups, those who have a Bachelor’s degree or higher, and those who do not. It then shows the responses of interest in politics for both groups. The data shows that those who have a Bachelor’s degree or higher tend to be more interested in politics than those who do not.

## Task 5
 UK_clean <- UK_data |>
  filter(!is.na(educ_level) & !is.na(polintr))

UK_prob <- UK_clean |>
  mutate(polintr = case_when(
    polintr == 1 ~ "Very interested",
    polintr == 2 ~ "Quite interested",
    polintr == 3 ~ "Hardly interested",
    polintr == 4 ~ "Not at all interested",
    TRUE ~ as.character(polintr)
  ))

table(UK_prob$polintr)
## 
##     Hardly interested Not at all interested      Quite interested 
##                  5634                  3819                  8701 
##       Very interested 
##                  2809
table(UK_prob$polintr, UK_prob$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="Interest in politics in the United Kingdom",
       y = "Conditional Percentage",
       x = "Interest in Politics",
       fill = "At least BA vs. Not")

The Y axis shows the conditional percentages of responses to interest in politics. The X axis shows responses ranging from 1 (very interested), to 4 (not at all interested). There are two bars over each response, one for those who have a Bachelor’s degree or higher, the other for those who do not. The graph shows that people with a Bachelor’s degree or higher are more interested in politics than those who do not have a Bachelor’s degree or higher. While the difference in middling responses was not drastic, those with Bachelor’s degrees or higher had a much higher percentage of respondents who answered “very interested in politics,” and respondents without a Bachelor’s degree or higher had a much higher percentage of “not at all interested” responses.