#summary(data)
library(fst)
ess <- read_fst("All-ESS-Data.fst")
library (tidyverse)
## ── 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
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)
## 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" "fstcore" "fst"
## [13] "stats" "graphics" "grDevices" "utils" "datasets" "methods"
## [19] "base"
##
## [[2]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "fstcore" "fst" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[3]]
## [1] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [6] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [11] "tidyverse" "fstcore" "fst" "stats" "graphics"
## [16] "grDevices" "utils" "datasets" "methods" "base"
##
## [[4]]
## [1] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [6] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [11] "ggplot2" "tidyverse" "fstcore" "fst" "stats"
## [16] "graphics" "grDevices" "utils" "datasets" "methods"
## [21] "base"
##
## [[5]]
## [1] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [6] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [11] "ggplot2" "tidyverse" "fstcore" "fst" "stats"
## [16] "graphics" "grDevices" "utils" "datasets" "methods"
## [21] "base"
##
## [[6]]
## [1] "viridis" "viridisLite" "RColorBrewer" "modelsummary" "lubridate"
## [6] "forcats" "stringr" "dplyr" "purrr" "readr"
## [11] "tidyr" "tibble" "ggplot2" "tidyverse" "fstcore"
## [16] "fst" "stats" "graphics" "grDevices" "utils"
## [21] "datasets" "methods" "base"
##
## [[7]]
## [1] "knitr" "viridis" "viridisLite" "RColorBrewer" "modelsummary"
## [6] "lubridate" "forcats" "stringr" "dplyr" "purrr"
## [11] "readr" "tidyr" "tibble" "ggplot2" "tidyverse"
## [16] "fstcore" "fst" "stats" "graphics" "grDevices"
## [21] "utils" "datasets" "methods" "base"
##
## [[8]]
## [1] "kableExtra" "knitr" "viridis" "viridisLite" "RColorBrewer"
## [6] "modelsummary" "lubridate" "forcats" "stringr" "dplyr"
## [11] "purrr" "readr" "tidyr" "tibble" "ggplot2"
## [16] "tidyverse" "fstcore" "fst" "stats" "graphics"
## [21] "grDevices" "utils" "datasets" "methods" "base"
##
## [[9]]
## [1] "rmarkdown" "kableExtra" "knitr" "viridis" "viridisLite"
## [6] "RColorBrewer" "modelsummary" "lubridate" "forcats" "stringr"
## [11] "dplyr" "purrr" "readr" "tidyr" "tibble"
## [16] "ggplot2" "tidyverse" "fstcore" "fst" "stats"
## [21] "graphics" "grDevices" "utils" "datasets" "methods"
## [26] "base"
##
## [[10]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "fstcore" "fst"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
##
## [[11]]
## [1] "ggridges" "rmarkdown" "kableExtra" "knitr" "viridis"
## [6] "viridisLite" "RColorBrewer" "modelsummary" "lubridate" "forcats"
## [11] "stringr" "dplyr" "purrr" "readr" "tidyr"
## [16] "tibble" "ggplot2" "tidyverse" "fstcore" "fst"
## [21] "stats" "graphics" "grDevices" "utils" "datasets"
## [26] "methods" "base"
##
## [[12]]
## [1] "questionr" "ggridges" "rmarkdown" "kableExtra" "knitr"
## [6] "viridis" "viridisLite" "RColorBrewer" "modelsummary" "lubridate"
## [11] "forcats" "stringr" "dplyr" "purrr" "readr"
## [16] "tidyr" "tibble" "ggplot2" "tidyverse" "fstcore"
## [21] "fst" "stats" "graphics" "grDevices" "utils"
## [26] "datasets" "methods" "base"
#summary(data)
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(
edulvla = ifelse(edulvla == 2, 0, ifelse(edulvla %in% c(7, 8, 9), NA, edulvla)),
pdwrk = ifelse(pdwrk %in% c(77, 88, 99), NA, pdwrk),
rlgblg = ifelse(rlgblg %in% c(77, 88, 99), NA, rlgblg),
)
table(france_data$clsprty)
##
## 1 2 7 8
## 9507 9194 101 236
datasummary_skim(france_data %>% select(edulvla, pdwrk, rlgblg))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| edulvla | 8 | 61 | 2.8 | 2.9 | 0.0 | 3.0 | 88.0 | |
| pdwrk | 2 | 0 | 0.5 | 0.5 | 0.0 | 1.0 | 1.0 | |
| rlgblg | 4 | 0 | 1.5 | 0.7 | 1.0 | 2.0 | 8.0 |
From the data summary table, I notice that the average individual in France obtains an upper secondary level of education, have been paid in the last 7 days, and do not belong to a particular religion or denomination.
#summary(data)
france_data %>%
mutate(yrbrn = case_when(
yrbrn %in% c(7777, 8888, 9999) ~ NA_integer_,
TRUE ~ as.integer(yrbrn)
)) %>%
group_by(yrbrn) %>%
summarise(avg_edulvla = mean(edulvla, na.rm = TRUE)) %>%
ggplot(aes(x= yrbrn, y = avg_edulvla)) +
geom_smooth(method = "loess", se = FALSE, color = "grey 50") +
geom_point(aes(color = avg_edulvla), size = 2.5) +
scale_color_gradient(low = "darkred", high = "pink") +
labs(title = "Average Education by Year of Birth",
x = "Year of Birth", y = "Average Education in France (0-10)" ) +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
## Warning: Removed 13 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 13 rows containing missing values (`geom_point()`).
The visual of the average education of France by year illustrates that people born between 1900-1955 have lower levels of education (lower or less than secondary education), and after that more people obtained post-secondary education. Until 1990 where a negative decline occurred.
#summary(data)
ess_selected <- ess %>%
filter(cntry %in% c("FR", "NO", "SE")) %>%
mutate(edulvla = ifelse(edulvla %in% c(77, 88, 99), NA,
edulvla))
task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -edulvla, FUN=median), y = edulvla, fill = cntry)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Boxplot Comparison for Education in France, Norway, Sweden",
x = "Country",
y = "Scale (0-10)")
task3plot
## Warning: Removed 31227 rows containing non-finite values (`stat_boxplot()`).
From looking at the boxplot, I notice that the median education level of France, Norway, and Sweden all lies on 3 which represents the completion of an upper secondary education. However, Sweden has most responses in the lower quartile, indicating lower levels of education while Norway has responses solely focused in the upper quartile to the maximum indicating their high levels of education. I also noticed there is a outlier for Sweden at the maximum. Furthermore, France ranges from the lower quartile to the maximum since people born between 1900-1955 obtained lower levels of education as seen in the visual in task 2.
#summary(data)
fraance_data <- france_data %>%
mutate(religion = recode(rlgblg,
`1` = "Yes",
`2` = "No",
`7` = NA_character_,
`8` = NA_character_,
`9` = NA_character_))
table(france_data$religion)
## < table of extent 0 >
table(france_data$rlgblg)
##
## 1 2 7 8
## 9509 9434 53 42
edulvlarlg <- datasummary_crosstab(edulvla ~ rlgblg, data = france_data)
edulvlarlg
| edulvla | 1 | 2 | 7 | 8 | All | |
|---|---|---|---|---|---|---|
| 0 | N | 486 | 527 | 0 | 5 | 1018 |
| % row | 47.7 | 51.8 | 0.0 | 0.5 | 100.0 | |
| 1 | N | 907 | 538 | 2 | 2 | 1449 |
| % row | 62.6 | 37.1 | 0.1 | 0.1 | 100.0 | |
| 3 | N | 1281 | 1587 | 1 | 10 | 2879 |
| % row | 44.5 | 55.1 | 0.0 | 0.3 | 100.0 | |
| 4 | N | 6 | 7 | 0 | 0 | 13 |
| % row | 46.2 | 53.8 | 0.0 | 0.0 | 100.0 | |
| 5 | N | 951 | 1046 | 4 | 2 | 2003 |
| % row | 47.5 | 52.2 | 0.2 | 0.1 | 100.0 | |
| 77 | N | 0 | 1 | 1 | 0 | 2 |
| % row | 0.0 | 50.0 | 50.0 | 0.0 | 100.0 | |
| 88 | N | 3 | 0 | 0 | 1 | 4 |
| % row | 75.0 | 0.0 | 0.0 | 25.0 | 100.0 | |
| All | N | 9509 | 9434 | 53 | 42 | 19038 |
| % row | 49.9 | 49.6 | 0.3 | 0.2 | 100.0 |
france_data <- france_data %>%
mutate(paid_work = case_when(
pdwrk == 0 ~ "No",
pdwrk == 1 ~ "Yes",
pdwrk %in% c(7, 8, 9) ~ NA_character_,
TRUE ~ as.character(pdwrk)
))
table(france_data$paid_work)
##
## No Yes
## 9466 9572
table(france_data$edulvla, france_data$paid_work) %>%
cprop()
##
## No Yes All
## 0 17.1 10.7 13.8
## 1 32.3 7.7 19.7
## 3 33.7 44.2 39.1
## 4 0.2 0.2 0.2
## 5 16.7 37.1 27.2
## 77 0.0 0.0 0.0
## 88 0.1 0.0 0.1
## Total 100.0 100.0 100.0
I notice that individuals in France that do not belong to a particular religion or denomination have more people obtaining a upper secondary education and tertiary education than individuals that belong to a particular religion. As for the paid work status variable, individuals that have had paid work in the last 7 days, have a higher level of education than those who do not.
#summary(data)
france_clean <- france_data %>%
filter(!is.na(pdwrk) & !is.na(edulvla))
france_probs <- france_clean %>%
count(edulvla, pdwrk) %>%
group_by(pdwrk) %>%
mutate(prob = n / sum(n))
ggplot(france_probs, aes(x = as.factor(edulvla), y = prob, color = pdwrk)) +
geom_point() +
geom_line(aes(group = pdwrk)) +
labs(title = "Conditional Probabilities of Education Levels in France",
subtitle = "By Paid Work Status",
x = "Education Level",
y = "Probability") +
theme_minimal()
From examining the visual, individuals that have paid work in the last 7 days have higher probabilities of obtaining a upper secondary education as the light blue point at value 3 reaches the maximum. Furthermore, their possibilities of obtaining a post-secondary non-tertiary education is lower than them completing a tertiary education. Individuals that have not done paid work in the last 7 days, have lower probabilities of obtaining a education higher than upper secondary education. As the black points that indicate “not marked” are higher in education levels 1-2 and lower in education levels 3-5 compared to the blue points.