Mission 1.

packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer", 
              "fst", "viridis", "knitr", "rmarkdown", "ggridges", "viridis", "questionr", "flextable", "infer")
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 --
## v dplyr     1.1.2     v readr     2.1.4
## v forcats   1.0.0     v stringr   1.5.0
## v ggplot2   3.4.3     v tibble    3.2.1
## v lubridate 1.9.2     v tidyr     1.3.0
## v purrr     1.0.1     
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
## i Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Loading required package: viridisLite
## 
## 
## Attaching package: 'flextable'
## 
## 
## The following object is masked from 'package:purrr':
## 
##     compose
## [[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] "rmarkdown"    "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] "ggridges"     "rmarkdown"    "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"    "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"        
## 
## [[11]]
##  [1] "questionr"    "ggridges"     "rmarkdown"    "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] "flextable"    "questionr"    "ggridges"     "rmarkdown"    "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"        
## 
## [[13]]
##  [1] "infer"        "flextable"    "questionr"    "ggridges"     "rmarkdown"   
##  [6] "knitr"        "viridis"      "viridisLite"  "fst"          "RColorBrewer"
## [11] "modelsummary" "lubridate"    "forcats"      "stringr"      "dplyr"       
## [16] "purrr"        "readr"        "tidyr"        "tibble"       "ggplot2"     
## [21] "tidyverse"    "stats"        "graphics"     "grDevices"    "utils"       
## [26] "datasets"     "methods"      "base"
ess <- read_fst("All-ESS-Data.fst")

Mission 2.

swiss_data <- ess %>% 
  filter(cntry == "CH") 

write_fst(swiss_data, "C:/Users/mimi h/Documents/UOFT/YEAR FOUR/SOC202/Homework_6_Project/swiss_data.fst") 

Mission 3.

#rm(list=ls()); gc()

df <- read_fst("C:/Users/mimi h/Documents/UOFT/YEAR FOUR/SOC202/Homework_6_Project/swiss_data.fst")

Mission 4.

df$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
  df$year[df$essround == i] <- replacements[i]
}

swiss_data <- df

swiss_data_table_subset <- swiss_data %>%
  mutate(
    tstfnch = ifelse(tstfnch %in% c(7, 8, 9), NA, tstfnch), #Trust in financial officials
    mnyacth = ifelse(mnyacth %in% c(7, 8, 9), NA, mnyacth), #Agreement with: "If you want to make money, you can't always act honestly."
    fairelc = ifelse(fairelc %in% c(77, 88, 99), NA, fairelc) #Rating: "How important is it to democracy that national elections are fair and free?"
    
  )

summary_table <- datasummary_skim(swiss_data_table_subset %>% select(tstfnch, mnyacth, fairelc), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
##   "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
##   ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
##   compiled to PDF (via kableExtra)  or HTML (via kableExtra or gt). Use
##   `histogram=FALSE` to silence this warning.
summary_table

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

tstfnch

6

87

3.2

1.0

1.0

3.0

5.0

mnyacth

6

88

3.0

1.2

1.0

3.0

5.0

fairelc

12

83

9.0

1.5

0.0

10.0

10.0

swiss_data_v2 <- swiss_data_table_subset %>%
  rename(
    `Trust in Financial Officials` = tstfnch,
    `Relationship between Financial Success and Dishonesty` = mnyacth,
    `Importance of Fair Elections` = fairelc
  )

summary_table_v2 <- datasummary_skim(swiss_data_v2 %>% select(`Trust in Financial Officials`,`Relationship between Financial Success and Dishonesty`, `Importance of Fair Elections`), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
##   "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
##   ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
##   compiled to PDF (via kableExtra)  or HTML (via kableExtra or gt). Use
##   `histogram=FALSE` to silence this warning.
summary_table_v2 <- add_header_lines(summary_table_v2, values = "Table 1: Descriptive Statistics for Financial Trust, Morality, and Democracy in Switzerland")

summary_table_v2

Table 1: Descriptive Statistics for Financial Trust, Morality, and Democracy in Switzerland

Unique (#)

Missing (%)

Mean

SD

Min

Median

Max

Trust in Financial Officials

6

87

3.2

1.0

1.0

3.0

5.0

Relationship between Financial Success and Dishonesty

6

88

3.0

1.2

1.0

3.0

5.0

Importance of Fair Elections

12

83

9.0

1.5

0.0

10.0

10.0

Mission 7.

#Re-coding religious status into a binary variable.

swiss_data <- swiss_data %>%
  mutate(religion = case_when(
    rlgblg == 2 ~ "Non-religious",
    rlgblg == 1 ~ "Religious",
    rlgblg %in% c(7, 8, 9) ~ NA_character_,
    TRUE ~ as.character(rlgblg)
  ))
table(swiss_data$religion)
## 
## Non-religious     Religious 
##          6008         10855
#Cleaning rlgblg and freehms (gays and lesbians should live life as they wish) and religiosity because my previous variables seemed specific to the 2004 ESS round and therefore could not be plotted by year.

swiss_data_table_subset <- swiss_data %>%
  mutate(
    rlgblg = ifelse(rlgblg == 2, 0, ifelse(rlgblg %in% c(7, 8, 9), NA, rlgblg)), 
    freehms = ifelse(freehms %in% c(7, 8, 9), NA, freehms),
  )

#Visualizing.

avg_freehms_by_year <- aggregate(freehms ~ year + rlgblg, data=swiss_data_table_subset, mean)

p1 <- ggplot(avg_freehms_by_year, aes(x=year, y=freehms, color=as.factor(rlgblg))) + 
  geom_line(aes(group=rlgblg)) +
  labs(title="Disapproval of Gay and Lesbian Freedom", 
       subtitle = "for those who are religious vs. not religious in Switzerland",
       x="Survey Year", 
       y="Disapproval of Gay and Lesbian Freedom") +
  scale_color_discrete(name="Religious Status", labels=c("Non-religious", "Religious")) +
  theme_minimal()

p1