Setting up the Environment.
# 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
## 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"
Load class dataset.
ess <- read_fst("All-ESS-Data.fst")
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$clsprty)
##
## 1 2 7 8 9
## 231368 246470 2391 9334 992
table(ess$stfeco)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 39574 25671 45330 59142 55182 75639 57084 58459 41634 13226 8012 424 10562
## 99
## 616
table(ess$stfgov)
##
## 0 1 2 3 4 5 6 7 8 9 10 77 88
## 50082 30238 46909 58918 55276 80173 54097 51400 30634 8648 6253 1012 14087
## 99
## 782
france_data <- ess %>%
filter(cntry == "FR") %>%
mutate(
clsprty = ifelse(clsprty == 2, 0, ifelse(clsprty %in% c(7, 8, 9), NA, clsprty)),
stfeco = ifelse(stfeco %in% c(77, 88, 99), NA,stfeco),
stfgov = ifelse(stfgov %in% c(77, 88, 99), NA, stfgov),
)
datasummary_skim(france_data %>% select(clsprty, stfeco, stfgov))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| clsprty | 3 | 2 | 0.5 | 0.5 | 0.0 | 1.0 | 1.0 | |
| stfeco | 12 | 1 | 3.5 | 2.1 | 0.0 | 3.0 | 10.0 | |
| stfgov | 12 | 1 | 3.7 | 2.3 | 0.0 | 4.0 | 10.0 |
My Observations: There is a much higher average amount of individuals who are satisfied with the national government and who are satisfied with the present state of the economy in their country, then there are people who feel closer to a particular party compared to other parties.
satisfaction_by_year <- france_data %>%
group_by(year) %>%
summarize(mean_satisfaction = mean(stfeco, na.rm = TRUE))
satisfaction_by_year
## # A tibble: 10 × 2
## year mean_satisfaction
## <dbl> <dbl>
## 1 2002 3.50
## 2 2004 3.76
## 3 2006 3.7
## 4 2008 2.99
## 5 2010 3.30
## 6 2012 3.13
## 7 2014 2.94
## 8 2016 3.49
## 9 2018 3.68
## 10 2020 4.61
ggplot(satisfaction_by_year, aes(x = year, y = mean_satisfaction)) +
geom_line(color = "blue", size = 1) +
geom_point(color = "red", size = 3) +
labs(title = "Satisfaction with the current economic state in France (2002-2020)",
x = "Survey Year",
y = "Satisfaction (0-10)") +
ylim(0, 10) +
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 Observations: Since 2015, there has been a positive increase in the
amount of French citizens that feel satisfied with their present state
of the economy in France. Furthermore, it has been roughly 10 years
since there has been an increase of satisfaction in this particular
category.
ess_selected <- ess %>%
filter(cntry %in% c("FR", "IT", "GB")) %>%
mutate(stfeco = ifelse(stfeco %in% c(77, 88, 99), NA, stfeco))
task3plot <- ggplot(ess_selected, 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 current economy (France, Italy, Great Britain)",
x = "Country",
y = "Scale (0-10)")
task3plot
## Warning: Removed 1082 rows containing non-finite values (`stat_boxplot()`).
My Observations: In this visual, there are two outliers. The first
belonging to France, and the second belonging to Italy, both being at
the same point in the scale. Furthermore, Great Britain has the highest
median, while France has the lowest.
france_data <- france_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(france_data$educ_level)
##
## BA No BA
## 4235 14803
france_data <- france_data %>%
mutate(geo = recode(as.character(domicil),
'1' = "Urban",
'2' = "Peri-Urban",
'3' = "Rural",
'4' = "Rural",
'5' = "Rural",
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_))
table(france_data$geo)
##
## Peri-Urban Rural Urban
## 2361 13087 3584
table(france_data$domicil)
##
## 1 2 3 4 5 7 8
## 3584 2361 6257 5668 1162 3 3
stfecoedu <- datasummary_crosstab(stfeco ~ educ_level, data = france_data)
stfecogeo <- datasummary_crosstab(stfeco ~ geo, data = france_data)
stfecoedu
| stfeco | BA | No BA | All | |
|---|---|---|---|---|
| 0 | N | 243 | 1788 | 2031 |
| % row | 12.0 | 88.0 | 100.0 | |
| 1 | N | 215 | 942 | 1157 |
| % row | 18.6 | 81.4 | 100.0 | |
| 2 | N | 571 | 2204 | 2775 |
| % row | 20.6 | 79.4 | 100.0 | |
| 3 | N | 828 | 2683 | 3511 |
| % row | 23.6 | 76.4 | 100.0 | |
| 4 | N | 754 | 2250 | 3004 |
| % row | 25.1 | 74.9 | 100.0 | |
| 5 | N | 682 | 2590 | 3272 |
| % row | 20.8 | 79.2 | 100.0 | |
| 6 | N | 503 | 1126 | 1629 |
| % row | 30.9 | 69.1 | 100.0 | |
| 7 | N | 301 | 587 | 888 |
| % row | 33.9 | 66.1 | 100.0 | |
| 8 | N | 81 | 293 | 374 |
| % row | 21.7 | 78.3 | 100.0 | |
| 9 | N | 14 | 43 | 57 |
| % row | 24.6 | 75.4 | 100.0 | |
| 10 | N | 11 | 58 | 69 |
| % row | 15.9 | 84.1 | 100.0 | |
| All | N | 4235 | 14803 | 19038 |
| % row | 22.2 | 77.8 | 100.0 |
stfecogeo
| stfeco | Peri-Urban | Rural | Urban | All | |
|---|---|---|---|---|---|
| 0 | N | 209 | 1466 | 355 | 2031 |
| % row | 10.3 | 72.2 | 17.5 | 100.0 | |
| 1 | N | 130 | 814 | 211 | 1157 |
| % row | 11.2 | 70.4 | 18.2 | 100.0 | |
| 2 | N | 310 | 1987 | 477 | 2775 |
| % row | 11.2 | 71.6 | 17.2 | 100.0 | |
| 3 | N | 427 | 2427 | 657 | 3511 |
| % row | 12.2 | 69.1 | 18.7 | 100.0 | |
| 4 | N | 387 | 2018 | 599 | 3004 |
| % row | 12.9 | 67.2 | 19.9 | 100.0 | |
| 5 | N | 398 | 2267 | 606 | 3272 |
| % row | 12.2 | 69.3 | 18.5 | 100.0 | |
| 6 | N | 248 | 1037 | 343 | 1629 |
| % row | 15.2 | 63.7 | 21.1 | 100.0 | |
| 7 | N | 133 | 577 | 178 | 888 |
| % row | 15.0 | 65.0 | 20.0 | 100.0 | |
| 8 | N | 55 | 242 | 77 | 374 |
| % row | 14.7 | 64.7 | 20.6 | 100.0 | |
| 9 | N | 12 | 33 | 12 | 57 |
| % row | 21.1 | 57.9 | 21.1 | 100.0 | |
| 10 | N | 10 | 37 | 22 | 69 |
| % row | 14.5 | 53.6 | 31.9 | 100.0 | |
| All | N | 2361 | 13087 | 3584 | 19038 |
| % row | 12.4 | 68.7 | 18.8 | 100.0 |
table(france_data$stfeco, france_data$educ_level) %>%
cprop()
##
## BA No BA All
## 0 5.8 12.3 10.8
## 1 5.1 6.5 6.2
## 2 13.6 15.1 14.8
## 3 19.7 18.4 18.7
## 4 17.9 15.4 16.0
## 5 16.2 17.8 17.4
## 6 12.0 7.7 8.7
## 7 7.2 4.0 4.7
## 8 1.9 2.0 2.0
## 9 0.3 0.3 0.3
## 10 0.3 0.4 0.4
## Total 100.0 100.0 100.0
df <- france_data %>%
filter(!is.na(educ_level) & !is.na(stfeco))
df <- df %>%
mutate(stfeco = case_when(
stfeco == 1 ~ "Yes",
stfeco == 0 ~ "No",
TRUE ~ as.character(stfeco)
))
table(df$stfeco)
##
## 10 2 3 4 5 6 7 8 9 No Yes
## 69 2775 3511 3004 3272 1629 888 374 57 2031 1157
table(df$stfeco, df$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="Satisfaction with current economic state in France",
y = "Conditional Percentage",
x = "Satisfaction",
fill = "At least BA vs. Not")
My Observations: When looking at the data based on those who received a
bachelors degree or above vs. not, those who said they have not received
a bachelors degree were the ones who felt the most that they were
satisfied with the state of economy in France. When looking at the data
based on those paid work status, there was a majority higher amount of
individuals in the yes category who felt that they were satisfied with
the state of the economy in France.
france_clean <- france_data %>%
filter(!is.na(geo) & !is.na(stfgov))
france_probs <- france_clean %>%
count(stfgov, geo) %>%
group_by(geo) %>%
mutate(prob = n / sum(n))
ggplot(france_probs, aes(x = as.factor(stfgov), y = prob, color = geo)) +
geom_point() +
geom_line(aes(group = geo)) +
labs(title = "Satisfaction with national government",
subtitle = "by Place of Residence",
x = "Satisfaction",
y = "Probability") +
theme_minimal()