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) ess <- read_fst(“All-ESS-Data.fst”) setwd(‘C:/Users/evand/Desktop/SOC202’) getwd()
#———- # TASK 1 # ———
table(ess\(prtyban) # Ban political parties that wish to overthrow democracy table(ess\)polactiv) # Do you think you could take an active role in a group involved with political issues? table(ess$stfeco) # How satisfied are you with the present state of economy in your country?
gb_data <- ess %>% # filter(cntry == “GB”) %>% mutate( prtyban = ifelse(prtyban %in% c(7, 8, 9), NA, prtyban), polactiv = ifelse(polactiv %in% c(7, 8, 9), NA, polactiv), stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco) )
datasummary_skim(gb_data %>% select(prtyban, polactiv, stfeco))
table(ess\(essround) 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] }
eco_by <- gb_data %>% group_by(year) %>% summarize(mean_sat = mean(stfeco, na.rm = TRUE))
ggplot(eco_by, aes(x = year, y = mean_sat)) + 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 = “Satisfaction With the Present State of Economy in Great Britain (2002-2020)”, x = “Survey Year”, y = “Satisfaction (0-10)”) + ylim(0, 10) + # Setting the y-axis limits from 0 to 10 theme_minimal()
task3_cntry <- ess %>% filter(cntry %in% c(“FR”, “GB”, “NL”)) %>% mutate(stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco))
task3plot <- ggplot(task3_cntry, aes(x = reorder(cntry, -stfeco, FUN=median), y = stfeco, fill = cntry)) + geom_boxplot() + theme_minimal() + theme(legend.position = “none”) + labs(title = “Boxplot comparison for satisfaction with the present state of economy (France, Great Britai, Netherlands)”, x = “Country”, y = “Scale (0-10)”) task3plot
gb_data <- gb_data %>% mutate(religion = case_when( rlgblg == 2 ~ “No”, rlgblg == 1 ~ “Yes”, rlgblg %in% c(7, 8, 9) ~ NA_character_, TRUE ~ as.character(rlgblg) ))
gb_data <- gb_data %>% mutate(born_in_country = recode(brncntr,
1 = “Yes”, 2 = “No”, 7 =
NA_character_, 8 = NA_character_,
stfecorel <- datasummary_crosstab(stfeco ~ religion, data = gb_data)
proptable <- table(gb_data\(stfeco, gb_data\)religion) cprop(proptable)
proptable2 <- table(gb_data\(stfeco, gb_data\)born_in_country) cprop(proptable2)
gb_clean <- gb_data %>% filter(!is.na(religion) & !is.na(stfeco))
gb_probs <- gb_clean %>% count(stfeco, religion) %>% group_by(religion) %>% mutate(prob = n / sum(n))
ggplot(gb_probs, aes(x = as.factor(stfeco), y = prob, color = religion)) + geom_point() + geom_line(aes(group = religion)) + labs(title = “Conditional Probabilities of Satisfaction With the Present Economic State”, subtitle = “by Belonging to a religion or denomination”, x = “Satisfaction”, y = “Probability”) + theme_minimal()