packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr") # add any you need here
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("All-ESS-Data.fst")
UK_data <- ess %>%
filter(cntry == "GB")
#Task 2(B)
UK_data$year <- NA
replacements <- c(2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020)
for(i in 1:10){
UK_data$year[UK_data$essround == i] <- replacements[i]
}
UK_datasubset <- UK_data %>%
mutate(
edlvagb = ifelse(edlvagb == 0, 0, ifelse(edlvgb %in% c( 77, 88, 99), NA, edlvagb)),
uemp3m = ifelse(uemp3m %in% c(7, 8, 9), NA, uemp3m),
hhmmb = ifelse(hhmmb %in% c(77, 88, 99), NA, hhmmb)
)
summary_table <- datasummary_skim(UK_datasubset %>% select(uemp3m, hhmmb), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
summary_table
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max |
|---|---|---|---|---|---|---|---|
uemp3m | 3 | 0 | 1.8 | 0.4 | 1.0 | 2.0 | 2.0 |
hhmmb | 12 | 0 | 2.3 | 1.3 | 1.0 | 2.0 | 11.0 |
library(data.table)
##
## Attaching package: 'data.table'
## The following objects are masked from 'package:lubridate':
##
## hour, isoweek, mday, minute, month, quarter, second, wday, week,
## yday, year
## The following objects are masked from 'package:dplyr':
##
## between, first, last
## The following object is masked from 'package:purrr':
##
## transpose
UK_dataset <- ess[ess$cntry == "GB", ]
UK_cleandata <- UK_data %>%
filter(cntry == "GB", yrbrn > 2000 & yrbrn < 2022) %>%
mutate(
hhmmb = ifelse(hhmmb %in% c(77, 88, 99), NA, hhmmb),
uemp3m = ifelse(uemp3m %in% c(7, 8, 9), NA, uemp3m),
) %>%
select(hhmmb, uemp3m)
datasummary_skim(UK_cleandata)
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| hhmmb | 8 | 0 | 3.8 | 1.6 | 1.0 | 4.0 | 10.0 | |
| uemp3m | 2 | 0 | 1.9 | 0.3 | 1.0 | 2.0 | 2.0 |
#Task 2(C)
str(UK_cleandata$hhmmb)
## num [1:69] 4 3 3 3 5 5 2 3 2 4 ...
str(UK_cleandata$uemp3m)
## num [1:69] 2 2 2 2 2 2 2 2 2 2 ...
#Task 2 (D)
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$hhmmb)
##
## 1 2 3 4 5 6 7 8 9 10 11
## 98242 160942 91781 85370 34122 11892 3981 1521 578 341 172
## 12 13 14 15 16 17 18 19 20 22 23
## 124 35 21 22 8 1 4 1 1 2 1
## 24 77 88 99
## 1 460 104 828
UK_datasubset <- UK_data %>%
filter(cntry == "GB") %>%
select(cntry, year, yrbrn, hhmmb, uemp3m) %>%
mutate(
hhmmb = ifelse(hhmmb %in% c(77, 88, 99), NA, hhmmb),
uemp3m = ifelse(uemp3m %in% c(7, 8, 9), NA, uemp3m),
)
unemployment_by_year <- UK_datasubset %>%
group_by(year) %>%
summarize(unemployment_mean_by_year = mean(uemp3m, na.rm = TRUE))
unemployment_by_year
## # A tibble: 10 × 2
## year unemployment_mean_by_year
## <dbl> <dbl>
## 1 2002 1.77
## 2 2004 1.76
## 3 2006 1.77
## 4 2008 1.78
## 5 2010 1.78
## 6 2012 1.77
## 7 2014 1.72
## 8 2016 1.75
## 9 2018 1.76
## 10 2020 1.74
ggplot(unemployment_by_year, aes(x = year, y = unemployment_mean_by_year)) +
geom_line(color = "#9B30FF", size = 1) +
geom_point(color = "#BFEFFF", size = 4) +
labs(title = "Time trend of unemployment for more than three months in United Kingdom (2002-2020)",
x = "Year",
y = "Mean of unemployment (0-10 scale)") +
ylim(1, 2) +
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.
#As can be seen, both the table and graph show an approximate mean around the value of 1.75 and it has not changed any significantly in recent years. It suggests us that the people who suffer from unemployed more than three months have remained quite constant.
#Task 2 (E)
ess$uemp3m[ess$uemp3m %in% c(7, 8, 9)] <- NA
avg_mean_UK <- UK_datasubset %>%
group_by(year) %>%
summarize(mean_unemployment_UK = mean(uemp3m, na.rm = TRUE))
avg_unemployment_all_countries <- ess %>%
group_by(year) %>%
summarize(mean_unemployment_all = mean(uemp3m, na.rm = TRUE))
Combined_data <- left_join(avg_mean_UK, avg_unemployment_all_countries, by = "year")
long_data <- Combined_data %>%
pivot_longer(
cols = starts_with("mean_unemployment"),
names_to = "Unemployment_Type",
values_to = "Mean_Unemployment"
)
ggplot(long_data) +
geom_line(aes(x = year, y = Mean_Unemployment, color = Unemployment_Type), size = 1) +
geom_point(aes(x = year, y = Mean_Unemployment, color = Unemployment_Type), size = 3) +
labs(title = "Unemployment of baseline compared to UK (2002-2020)",
x = "Year",
y = "Mean Unemployment (0-10 scale)") +
ylim(1, 2) +
scale_color_manual(values = c("blue", "red"),
name = "Dataset",
labels = c("United Kingdom", "ALL ESS"),
breaks = c("unemployment_by_year", "mean_unemployment_all")) +
theme_minimal()
#The gray line is the trend from UK whereas the red line is the baseline from all counties in ESS data. The baseline shows a lower mean of the variable of the unemployment. Therefore, it tells us that UK's unemployment seems to be higher compared to the mean of all countries.
#Task 2(F & G)
library(modelsummary)
UK_datasubset <- UK_data %>%
filter(cntry == "GB", yrbrn > 2000 & yrbrn < 2020) %>%
mutate(
clsprty = ifelse(clsprty == 2, 0, clsprty),
hhmmb = ifelse(hhmmb %in% c(77, 88, 99), NA, hhmmb),
uemp3m = ifelse(uemp3m %in% c(7, 8, 9), NA, uemp3m),
chldhhe = ifelse(chldhhe %in% c(77, 88, 99), NA, chldhhe),
emplrel = ifelse(emplrel %in% c(7, 8, 9), NA, emplrel),
marsts = ifelse(marsts %in% c(77, 88, 99), NA, marsts),
gndr = ifelse(gndr %in% c(9), NA, gndr),
) %>%
select(hhmmb, uemp3m, chldhhe, emplrel, marsts, gndr, yrbrn)
datasummary_skim(UK_datasubset)
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| hhmmb | 8 | 0 | 3.8 | 1.6 | 1.0 | 4.0 | 10.0 | |
| uemp3m | 2 | 0 | 1.9 | 0.3 | 1.0 | 2.0 | 2.0 | |
| chldhhe | 3 | 0 | 2.1 | 0.7 | 1.0 | 2.0 | 6.0 | |
| emplrel | 4 | 0 | 3.6 | 2.5 | 1.0 | 3.0 | 6.0 | |
| marsts | 1 | 0 | 6.0 | 0.0 | 6.0 | 6.0 | 6.0 | |
| gndr | 2 | 0 | 1.6 | 0.5 | 1.0 | 2.0 | 2.0 | |
| yrbrn | 7 | 0 | 2002.6 | 1.6 | 2001.0 | 2002.0 | 2007.0 |
UK_data_V2 <- UK_datasubset %>%
rename(
`gender` = gndr,
`Ever had children living in household` = chldhhe,
`Legal martial status` = marsts,
`Employment relation` = emplrel,
`Number of people living regularly as member of household` = hhmmb,
`Ever unemployed more than three months` = uemp3m,
)
summary_table_v2 <- datasummary_skim(UK_data_V2 %>% select(`gender`,`Ever had children living in household`, `Legal martial status`, `Employment relation`, `Ever unemployed more than three months`, `Number of people living regularly as member of household`), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
datasummary_skim(UK_data_V2)
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| Number of people living regularly as member of household | 8 | 0 | 3.8 | 1.6 | 1.0 | 4.0 | 10.0 | |
| Ever unemployed more than three months | 2 | 0 | 1.9 | 0.3 | 1.0 | 2.0 | 2.0 | |
| Ever had children living in household | 3 | 0 | 2.1 | 0.7 | 1.0 | 2.0 | 6.0 | |
| Employment relation | 4 | 0 | 3.6 | 2.5 | 1.0 | 3.0 | 6.0 | |
| Legal martial status | 1 | 0 | 6.0 | 0.0 | 6.0 | 6.0 | 6.0 | |
| gender | 2 | 0 | 1.6 | 0.5 | 1.0 | 2.0 | 2.0 | |
| yrbrn | 7 | 0 | 2002.6 | 1.6 | 2001.0 | 2002.0 | 2007.0 |
# What stands out tall in this summarytable are the variables of "uemp3m" and "emplrel",considering the max value is 2 and 6 for these two variables, a mean of 1.9 for "uemp3m" and 3.6 for "emplrel" have suggested that most of people have been umemployed for more than three months while they used to be either employed or self-employed.
library(janitor)
##
## Attaching package: 'janitor'
## The following objects are masked from 'package:stats':
##
## chisq.test, fisher.test
table(UK_datasubset$hhmmb, UK_datasubset$uemp3m) %>%
cprop(tab,
digits = 1,
total = TRUE,
percent = FALSE,
drop = TRUE,
n = FALSE,)
##
## 1 2 All
## 1 0.0 4.8 4.3
## 2 16.7 19.0 18.8
## 3 16.7 19.0 18.8
## 4 16.7 31.7 30.4
## 5 16.7 15.9 15.9
## 6 16.7 4.8 5.8
## 7 16.7 3.2 4.3
## 10 0.0 1.6 1.4
## Total 100.0 100.0 100.0
table(UK_datasubset$chldhhe, UK_datasubset$emplrel) %>%
cprop(tab,
digits = 1,
total = TRUE,
percent = FALSE,
drop = TRUE,
n = FALSE,)
##
## 1 2 3 6 All
## 1 0.0 0.0 0.0 2.9 1.4
## 2 96.8 100.0 100.0 94.1 95.7
## 6 3.2 0.0 0.0 2.9 2.9
## Total 100.0 100.0 100.0 100.0 100.0
table(UK_datasubset$marsts, UK_datasubset$gndr) %>%
cprop(tab,
digits = 1,
total = TRUE,
percent = FALSE,
drop = TRUE,
n = FALSE,)
##
## 1 2 All
## 6 100 100 100
## Total 100 100 100
UK_data_V2 <- UK_datasubset %>%
rename(
`gender` = gndr,
`Ever had children living in household` = chldhhe,
`Legal martial status` = marsts,
`Employment relation` = emplrel,
`Number of people living regularly as member of household` = hhmmb,
`Ever unemployed more than three months` = uemp3m,
)
summary_table_v2 <- datasummary_skim(UK_data_V2 %>% select(`gender`,`Ever had children living in household`, `Legal martial status`, `Employment relation`, `Ever unemployed more than three months`, `Number of people living regularly as member of household`), output = "flextable")
## Warning: The histogram argument is only supported for (a) output types "default",
## "html", "kableExtra", or "gt"; (b) writing to file paths with extensions
## ".html", ".jpg", or ".png"; and (c) Rmarkdown, knitr or Quarto documents
## compiled to PDF (via kableExtra) or HTML (via kableExtra or gt). Use
## `histogram=FALSE` to silence this warning.
datasummary_skim(UK_data_V2)
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| Number of people living regularly as member of household | 8 | 0 | 3.8 | 1.6 | 1.0 | 4.0 | 10.0 | |
| Ever unemployed more than three months | 2 | 0 | 1.9 | 0.3 | 1.0 | 2.0 | 2.0 | |
| Ever had children living in household | 3 | 0 | 2.1 | 0.7 | 1.0 | 2.0 | 6.0 | |
| Employment relation | 4 | 0 | 3.6 | 2.5 | 1.0 | 3.0 | 6.0 | |
| Legal martial status | 1 | 0 | 6.0 | 0.0 | 6.0 | 6.0 | 6.0 | |
| gender | 2 | 0 | 1.6 | 0.5 | 1.0 | 2.0 | 2.0 | |
| yrbrn | 7 | 0 | 2002.6 | 1.6 | 2001.0 | 2002.0 | 2007.0 |
#TASK2 (H)
UK_visualdata <- ess %>%
filter(cntry == "GB") %>%
mutate(
uemp3m = ifelse(uemp3m %in% c(7, 8, 9), NA, uemp3m))
UK_visualdata$yrbrn[UK_visualdata$yrbrn %in% c(7777, 8888)] <- NA
UK_visualdata$uemp3m[UK_visualdata$uemp3m %in% c(7, 8, 9)] <- NA
uemp3m_proportions <- UK_visualdata %>%
filter(!is.na(yrbrn) & yrbrn >= 1920 & !is.na(uemp3m)) %>%
group_by(yrbrn, uemp3m) %>%
tally() %>%
mutate(proportion = n / sum(n)) %>%
ungroup()
task3plot <- ggplot(uemp3m_proportions, aes(x = yrbrn, y = proportion, color = as.factor(uemp3m))) +
geom_point() +
geom_smooth(method = 'loess', se = FALSE) +
labs(title = "Proportions of Unemployment by Year of Birth in UK",
x = "year born", y = "Proportion of Unemployment") +
theme_minimal() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank()) +
scale_color_discrete(name = "Year born vs unemployment", labels = c("Year born", "unemployment over three months"))
task3plot
## `geom_smooth()` using formula = 'y ~ x'