# 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
## Warning: package 'viridis' was built under R version 4.3.2
## Loading required package: viridisLite
## 
## Attaching package: 'kableExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
## Warning: package 'ggridges' was built under R version 4.3.2
## Warning: package 'questionr' was built under R version 4.3.2
## [[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]
}

israel_data <- ess %>% # Start with the original 'ess' dataset
  filter(cntry == "IL") %>% # Filter the data to only include rows where country (cntry) is Israel ("IL")
  mutate(
    scnsenv = ifelse(scnsenv %in% c(7, 8, 9), NA, scnsenv), # For 'scnsenv', set values 7, 8, and 9 to NA.
    trstsci = ifelse(trstsci %in% c(77, 88, 99), NA, trstsci), # For 'trstsci', set values 77, 88, and 99 to NA.
    cptppol = ifelse(cptppol %in% c(77, 88, 99), NA, cptppol), # For 'cptppol', set values 77, 88, and 99 to NA.
  )

datasummary_skim(israel_data %>% select(scnsenv, trstsci, cptppol))
Unique (#) Missing (%) Mean SD Min Median Max
scnsenv 6 57 2.4 1.0 1.0 2.0 5.0
trstsci 12 92 6.4 2.8 0.0 7.0 10.0
cptppol 12 85 3.0 3.0 0.0 2.0 10.0
#israel_data %>% select(year, scnsenv)
#unique(israel_data$year)
#table(israel_data$scnsenv)
#table(israel_data$trstsci)

The median for each variable is as follows:
- Israelis agree that modern science can be relied on to solve environmental problems
- Although not complete trust, Israelis trust in scientists to a great extent
- Although not total inconfidence, Israelis are not very confident in their ability to participate in politics

The distribution of each variable is as follows:
- scnsenv: right-skewed, the median is lower than the mean
- trstsci: left-skewed, median is greater than the mean
- cptppol: right-skewed, the median is lower than the mean

Task 2

scnsenv_by_year <- israel_data %>%
  group_by(year) %>%
  summarize(mean_scnsenv = mean(scnsenv, na.rm = TRUE)) %>%
  filter(!is.na(mean_scnsenv))
scnsenv_by_year
## # A tibble: 3 × 2
##    year mean_scnsenv
##   <dbl>        <dbl>
## 1  2002         2.51
## 2  2008         2.32
## 3  2010         2.42
ggplot(scnsenv_by_year, aes(x = year, y = mean_scnsenv)) +
  geom_line(color = "blue", size = 1) +  # Line to show the trend
  geom_point(color = "red", size = 3) +  # Points to highlight each year's value
  labs(title = "Trust in Modern Science to Solve Environmental Problems in Isarael (2002-2010)", 
       x = "Survey Year", 
       y = "Level of Disagreement (1-5)") +
  ylim(1, 5) +  # Setting the y-axis limits from 1 to 5
  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.

Israelis were surveyed only during the year 2002, 2008, and 2010 for their trust in modern science to solve environmental problems, so my graph only showed the data for these rounds.

My graph shows that the mean level of disagreement towards the statement of trusting in modern science to solve environmental problems is about the same in these three rounds. The mean level of disagreement is around 2.5, which is in between the option “agree” and the option “neither agree nor disagree”

Task 3

ess_selected <- ess %>%
  filter(cntry %in% c("IL", "ES", "NL")) %>%
  mutate(scnsenv = ifelse(scnsenv %in% c(7, 8, 9), NA, scnsenv))

task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -scnsenv, FUN=median), y = scnsenv, fill = cntry)) +
  geom_boxplot() +
  theme_minimal() + 
  theme(legend.position = "none") + 
  labs(title = "Boxplot comparison for trust in modern science to solve environmental problems (Israel, Spain, Netherlands)", 
       x = "Country", 
       y = "Level of Disagreement (1-5)")

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

The median for both Spain and Israel is 2, while Netherlands’ median is 3. “2” means “agree”, while “3” means “neither agree nor disagree”

The interquartile range of Spain and Israel is the same, which ranges from “agree” to “neither agree nor disagree”. However, this range is “2” to “4” for Netherlands, meaning the middle 50% of the data chose “agree” to “disagree”.

The whiskers of Spain and Israel both cover “Agree strongly” to “Disagree”, but the whisker of Netherlands cover “Agree strongly” to “Disagree strongly”.

Spain and Israel both have outliers at “Disagree strongly”, while Netherlands has none.

Task 4

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

# check
table(israel_data$religion)
## 
##    No   Yes 
##   729 14111
scnsenvrlg <- datasummary_crosstab(scnsenv ~ religion, data = israel_data)
scnsenvrlg
scnsenv No Yes All
1 N 77 1132 1228
% row 6.3 92.2 100.0
2 N 279 2450 2756
% row 10.1 88.9 100.0
3 N 170 1713 1894
% row 9.0 90.4 100.0
4 N 111 763 880
% row 12.6 86.7 100.0
5 N 13 141 155
% row 8.4 91.0 100.0
All N 729 14111 16218
% row 4.5 87.0 100.0
#israel_data %>% group_by(religion) %>% count(trstsci)

This crosstab shows that most of the people participated in this survey belong to a religion. This crosstab also shows that whether or not the participant believes in a religion, the least number of participants chose “Strongly Disagree” while most number of people chose “Agree”

# Re-coding education
israel_data <- israel_data %>%
  mutate(
    # Set 'other' to NA for rounds 1-4
    edulvla = case_when(
      essround < 5 & edulvla == 55 ~ NA_real_,
      TRUE ~ edulvla
    ),
    # Set 'other' to NA for rounds 5 and later
    edulvlb = case_when(
      essround >= 5 & edulvlb == 5555 ~ NA_real_,
      TRUE ~ edulvlb
    ),
    # Create educ_level column
    educ_level = case_when(
      essround < 5 & edulvla == 5 ~ "BA",
      essround >= 5 & edulvlb > 600 ~ "BA",
      TRUE ~ "No BA"
    )
  )

# View the table of educ_level
table(israel_data$educ_level) 
## 
##    BA No BA 
##  5572 10646
table(israel_data$scnsenv, israel_data$educ_level) %>%
  cprop()
##        
##         BA    No BA All  
##   1      16.1  18.7  17.8
##   2      40.2  39.7  39.9
##   3      27.3  27.4  27.4
##   4      14.0  12.1  12.7
##   5       2.4   2.2   2.2
##   Total 100.0 100.0 100.0

The proportion of people who chose “Strongly disagree” is the lowest in for both people having a backelor’s degree and without a bachelor’s degree. The proportion of Israelis choosing “Agree” is the highest, but among the people who chose this option, there’s only 0.5% more respondents have bachelor’s degree compared to the proportion of respondents without a bachelor’s degree.

Task 5

# double check clean
israel_clean <- israel_data %>%
  filter(!is.na(educ_level) & !is.na(scnsenv))

# calculate conditional probabilities
israel_probs <- israel_clean %>%
  count(scnsenv, educ_level) %>%
  group_by(educ_level) %>%
  mutate(prob = n / sum(n))
# plot
ggplot(israel_probs, aes(x = as.factor(scnsenv), y = prob, color = educ_level)) +
  geom_point() +
  geom_line(aes(group = educ_level)) +
  labs(title = "Conditional Probabilities of Trust in Modern Science to Solve Environmental Problems",
       subtitle = "by Level of Education",
       x = "Trust Scale", 
       y = "Probability") +
  theme_minimal()

The participants’ degree of trust in modern science to solve environmental problems wouldn’t be affected by whether or not they have a Bachelor’s degree.

The participants, whether or not they have a Bachelor’s degree, are most likely to choose “Agree”. Whether or not they have a bachelor’s degree, the participants are the least likely to choose “Strongly Disagree”.