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("~/Desktop/SOC202/All-ESS-Data.fst")
#Task 1.
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]}
france_data <- ess %>%
filter(cntry == "FR") %>%
mutate(
clsprty = ifelse(clsprty %in% c(7, 8, 9), NA, clsprty),
stflife = ifelse(stflife %in% c(77, 88, 99), NA, stflife),
happy = ifelse(happy %in% c(7, 8, 9), NA, happy)
)
datasummary_skim(france_data %>% select(clsprty, stflife, happy))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| clsprty | 3 | 2 | 1.5 | 0.5 | 1.0 | 1.0 | 2.0 | |
| stflife | 12 | 0 | 6.4 | 2.4 | 0.0 | 7.0 | 10.0 | |
| happy | 11 | 63 | 6.4 | 5.2 | 0.0 | 5.0 | 88.0 |
#Task 2.
feeling_close_to_party <- france_data %>%
group_by(year) %>%
summarize(mean_close = mean(clsprty, na.rm = TRUE))
france_data %>%
ggplot(aes(x = clsprty, y = as.factor(year), fill = as.factor(year))) +
geom_density_ridges() + # geometry used for visualizing
labs(title = "Feel closer to a particular party than all other parties",
x = "Yes (Yes - No Answer)", y = "Survey Year",
fill = "Survey Year") +
theme_minimal()
## Picking joint bandwidth of 0.0995
## Warning: Removed 337 rows containing non-finite values
## (`stat_density_ridges()`).
#Task 3.
ess_selected <- ess %>%
filter(cntry %in% c("FR", "RU", "SC")) %>%
mutate(clsprty = ifelse(clsprty %in% c(77, 88, 99), NA, clsprty))
task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -clsprty, FUN=median), y = clsprty, fill = cntry)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Boxplot comparison for Feeling Closer to the Party (Italy, Russia, Scotland)",
x = "Country",
y = "Scale (0-10)")
task3plot
country_proportions <- ess %>%
filter(cntry %in% c("FR", "RU", "SC")) %>%
filter(!clsprty %in% c(7, 8, 9)) %>%
group_by(cntry) %>%
summarize(
total_respondents = n(),
yes_count = sum(clsprty == 1),
proportion_yes = yes_count / total_respondents
) %>%
arrange(-proportion_yes)
p4 <- ggplot(country_proportions, aes(x = reorder(cntry, -proportion_yes), y = proportion_yes, fill = cntry)) +
geom_bar(stat = "identity") +
labs(
title = "Proportion of People Feeling Close to a Party (France, Russia, Scotland)",
x = "Country",
y = "Proportion",
fill = "Country"
) +
scale_fill_viridis_d() +
theme_minimal()
p4
#Task 4.
france_data <- france_data %>%
mutate(close = recode(as.character(clsprty),
'1' = "Yes",
'2' = "No",
'7' = "Refusal" ,
'8' = "Don't Know",
'9' = "No Answer" ))
france_data <- france_data %>%
mutate(stf = recode(as.character(stflife),
'0' = "Extremely dissatisfied",
'1' = "1",
'2' = "2",
'3' = "3",
'4' = "4",
'5' = "5",
'6' = "6",
'7' = "7",
'8' = "8",
'9' = "9",
'10'= "Extremely satisfied" ))
france_data <- france_data %>%
mutate(happy = recode(as.character(happy),
'0' = "Extremely unhappy",
'1' = "1",
'2' = "2",
'3' = "3",
'4' = "4",
'5' = "5",
'6' = "6",
'7' = "7",
'8' = "8",
'9' = "9",
'10'= "Extremely happy" ))
table(france_data$stf)
##
## 1 2 3
## 264 669 1020
## 4 5 6
## 1082 2567 1899
## 7 8 9
## 3334 4359 1751
## Extremely dissatisfied Extremely satisfied
## 542 1518
table(france_data$close)
##
## No Yes
## 9194 9507
table(france_data$happy)
##
## 1 2 3 4
## 57 185 346 514
## 5 6 77 88
## 2349 1869 14 12
## Extremely happy Extremely unhappy
## 1622 90
clsprtystf <- datasummary_crosstab(clsprty ~ stf, data = france_data)
clsprtystf
| clsprty | 1 | Â 2 | Â 3 | Â 4 | Â 5 | Â 6 | Â 7 | Â 8 | Â 9 | Extremely dissatisfied | Extremely satisfied | All | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | N | 125 | 338 | 490 | 516 | 1250 | 923 | 1745 | 2229 | 916 | 249 | 717 | 9507 |
| % row | 1.3 | 3.6 | 5.2 | 5.4 | 13.1 | 9.7 | 18.4 | 23.4 | 9.6 | 2.6 | 7.5 | 100.0 | |
| 2 | N | 132 | 324 | 515 | 552 | 1264 | 939 | 1527 | 2058 | 804 | 282 | 777 | 9194 |
| % row | 1.4 | 3.5 | 5.6 | 6.0 | 13.7 | 10.2 | 16.6 | 22.4 | 8.7 | 3.1 | 8.5 | 100.0 | |
| All | N | 264 | 669 | 1020 | 1082 | 2567 | 1899 | 3334 | 4359 | 1751 | 542 | 1518 | 19038 |
| % row | 1.4 | 3.5 | 5.4 | 5.7 | 13.5 | 10.0 | 17.5 | 22.9 | 9.2 | 2.8 | 8.0 | 100.0 |
table(france_data$clsprty, france_data$stf) %>%
cprop()
##
## 1 2 3 4 5 6 7 8 9
## 1 48.6 51.1 48.8 48.3 49.7 49.6 53.3 52.0 53.3
## 2 51.4 48.9 51.2 51.7 50.3 50.4 46.7 48.0 46.7
## Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
##
## Extremely dissatisfied Extremely satisfied All
## 1 46.9 48.0 50.9
## 2 53.1 52.0 49.1
## Total 100.0 100.0 100.0
table(france_data$clsprty, france_data$happy)%>%
cprop()
##
## 1 2 3 4 5 6 77 88 Extremely happy
## 1 50.0 45.3 47.5 52.9 46.4 51.4 50.0 45.5 45.5
## 2 50.0 54.7 52.5 47.1 53.6 48.6 50.0 54.5 54.5
## Total 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0 100.0
##
## Extremely unhappy All
## 1 40.9 48.0
## 2 59.1 52.0
## Total 100.0 100.0
#Task 5.
france_clean <- france_data %>%
filter(!is.na(stflife) & !is.na(clsprty))
france_probs <- france_clean %>%
count(clsprty, stflife) %>%
group_by(stflife) %>%
mutate(prob = n / sum(n))
df <- france_data %>%
filter(!is.na(stflife) & !is.na(clsprty))
df <- df %>%
mutate(clsprty = case_when(
clsprty == 1 ~ "Yes",
clsprty == 0 ~ "No",
TRUE ~ as.character(clsprty)
))
table(df$clsprty)
##
## 2 Yes
## 9174 9498
table(df$clsprty, df$stflife) %>%
cprop() %>%
as.data.frame() %>%
filter(Var1 != "Total",
Var2 != "All") %>%
ggplot(aes(x=Var1, y=Freq, fill=Var2)) +
geom_col(position = "dodge") +
labs(title="Feeling close to a particular party in France",
y = "Satisfaction",
x = "Feeling close",
fill = "Extremely DS vs. Extremely S")
france_clean <- france_data %>%
filter(!is.na(stflife) & !is.na(clsprty))
france_probs <- france_clean %>%
count(stflife, clsprty) %>%
group_by(clsprty) %>%
mutate(prob = n / sum(n))
ggplot(france_probs, aes(x = as.factor(stflife), y = prob, color = clsprty)) +
geom_point() +
geom_line(aes(group = clsprty)) +
labs(title = "Conditional Probabilities of Life Satisfaction ",
subtitle = "by feeling close to certain party",
x = "Satisfaction",
y = "Closeness") +
theme_minimal()