packages <- c("tidyverse", "modelsummary", "forcats", "RColorBrewer",
"fst", "viridis", "knitr", "kableExtra", "rmarkdown", "ggridges", "viridis", "questionr")
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")
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]
}
My three variables of interest are: (1) lrscale (Placement on left right scale — https://ess-search.nsd.no/en/variable/query/lrscale/1), (2) imprich (Important to be rich, have money and expensive things — https://ess-search.nsd.no/en/variable/query/imprich/1), (3) hincfel (Feeling about household’s income nowadays — https://ess-search.nsd.no/en/variable/query/hincfel/1) *hincfel is the socio-demographic value
uk_data <- ess %>%
filter(cntry == "GB") %>%
mutate(
lrscale = ifelse(lrscale %in% c(77, 88, 99), NA, lrscale),
imprich = ifelse(imprich %in% c(7, 8, 9), NA, imprich),
hincfel = ifelse(hincfel %in% c(7, 8, 9), NA, hincfel)
)
datasummary_skim(uk_data %>% select (lrscale, imprich, hincfel))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| lrscale | 12 | 11 | 5.0 | 1.9 | 0.0 | 5.0 | 10.0 | |
| imprich | 7 | 2 | 4.3 | 1.3 | 1.0 | 5.0 | 6.0 | |
| hincfel | 5 | 1 | 1.8 | 0.8 | 1.0 | 2.0 | 4.0 |
Now this is quite interesting. Politically, the majority of people are “center”, with a SD of +-1.9, so there is a decent amount falling into what can be considered “left” and “right.” When it comes to the importance of being rich, having money and expensive things, people actually fall on the side of it not being like them. Most people voted 5, meaning they thought it was “Not like me.” Lastly, for how people felt about their household income, most felt they were either living comfortably or coping (doing okay) on present income. Very few found it difficult and/or very difficult.
*Note: The hincfel variable is heavily restricted by the people who are able to access/answer the ESS, and thus not representative of the state of a country’s welfare.
The main outcome of interest for this task will be: hincfel. The data will be better understood with knowledge of its categories: “Which of the descriptions on this card come closest to how you feel about your household’s income nowadays” 1 — Living comfortably on present income, 2 — Coping on present income, 3 — Difficult on present income, 4 — Very difficult on present income
hincfel_yoy <- uk_data %>%
group_by(year) %>%
summarize(mean_hincfel = mean(hincfel, na.rm = TRUE))
*Note: The title of the plot is different than the variable’s name to make it sound more cohesive — it has the same meaning.
ggplot(hincfel_yoy, aes(x = year, y = mean_hincfel)) +
geom_line(color = "turquoise3", size = 2) +
geom_point(color = "gold2", size = 3) +
labs(title = "Feeling about the household's income nowadays in the UK, bi-yearly 2002 - 2020",
x = "Survey Year",
y = "Difficulty of living on income") +
ylim(0, 4) +
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.
Wow, this is a very stable variable. One notable thing about this visualization is that it actually seems to be trending down, albeit very slowly. This would mean that people in the UK (who were a part of the survey) are actually feeling better about their income in correlation to their living. We can’t draw that conclusion, but it’s still an interesting insight.
The 2 other countries I will be comparing the UK to are Lithuania (LT) and the Russian Federation (RU).
cntry_selected <- ess %>%
filter(cntry %in% c("GB", "LT", "RU")) %>%
mutate(hincfel = ifelse(hincfel %in% c(7, 8, 9), NA, hincfel))
tri_hincfel <- ggplot(cntry_selected, aes(x = reorder (cntry, -hincfel, FUN=median), y = hincfel, fill = cntry)) +
geom_boxplot(alpha = 0.5) +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Comparison on the feeling about the household's income nowadays (UK, Russia, Lithuania",
x = "Country",
y = "Difficulty living on household income")
ylim(0, 4)
## <ScaleContinuousPosition>
## Range:
## Limits: 0 -- 4
tri_hincfel
## Warning: Removed 680 rows containing non-finite values (`stat_boxplot()`).
Unfortunately, I did not choose the best variable for this. However, I tried to find countries with varying results. My first attempt, using the UK, France, and Germany, was a failure (they were identical). However, with the UK, Lithuania, and Russia, at the very least, they are ALL different. While the UK is clearly living more comfortably with their income, both Lithuania and Russia are around the middle. Despite Lithuania and Russia’s interquartile range being the same, Lithuania’s median is 2, while Russia’s median is 3. This means that out of the three, Russia finds it the most difficult to live with their household income (quite literally “Difficult on present income”). Due to the nature of box plots, Lithuania and Russia also have no outliers, while the UK has one due its interquartile range being between 1 and 2 and thus its furthest whisker reaching 3.
For the cross-tab, I will be using education level and ever having children in the household as my socio-demographical variables. The reason I chose education is NOT because it’s a part of the tutorial (though admittedly, it’s certainly helpful) but because the correlation between education and livability on household income is an interesting one to me. Furthermore, having children in the house certainly affects how household income may be distributed. It’s reasonable to note that this variable is “ever having” meaning that it may not presently be a valid application to the household. But nonetheless, I find it interesting: (1) edulvla/edulvlb (Highest level of education — https://ess-search.nsd.no/en/variable/query/edulvlb/1 and https://ess-search.nsd.no/en/variable/query/edulvla/1), (2) chldhhe (Ever had children living in household — https://ess-search.nsd.no/en/variable/query/chldhhe/1)
uk_data <- uk_data %>%
mutate(
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
edu_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
table(uk_data$edu_level)
##
## BA No BA
## 6116 14863
uk_data <- uk_data %>%
mutate(chldhhe = case_when(
chldhhe == 2 ~ "No",
chldhhe == 1 ~ "Yes",
chldhhe %in% c(6, 7, 8, 9) ~ NA_character_,
TRUE ~ as.character(chldhhe)
))
table(uk_data$chldhhe)
##
## No Yes
## 6280 7933
hincfel_edu <- datasummary_crosstab(hincfel ~ edu_level, data = uk_data)
hincfel_edu
| hincfel | BA | No BA | All | |
|---|---|---|---|---|
| 1 | N | 3151 | 5189 | 8340 |
| % row | 37.8 | 62.2 | 100.0 | |
| 2 | N | 2296 | 6765 | 9061 |
| % row | 25.3 | 74.7 | 100.0 | |
| 3 | N | 512 | 2134 | 2646 |
| % row | 19.3 | 80.7 | 100.0 | |
| 4 | N | 125 | 638 | 763 |
| % row | 16.4 | 83.6 | 100.0 | |
| All | N | 6116 | 14863 | 20979 |
| % row | 29.2 | 70.8 | 100.0 |
table(uk_data$hincfel, uk_data$chldhhe) %>%
cprop()
##
## No Yes All
## 1 43.6 44.4 44.0
## 2 42.0 43.2 42.7
## 3 11.4 9.6 10.4
## 4 3.0 2.9 2.9
## Total 100.0 100.0 100.0
As expected, the cross tab show that the higher level of education you have, the more likely you are to feel comfortable about your household income. This is likely due to the positive correlation between higher education and higher income (a closely related topic). Due to the lower number of people with a Bachelor’s Degree, you can come to this conclusion by looking at how the proportion of those without a BA grow when feelings about household income are lower. As for the column percentage for ever having a child in the house, the results are very even. Ever having a child in the house seems to have little effect on your feeling about household income. However, I would be curious to see how this would change if there was a variable for CURRENTLY having a child in the house.
Since my results with education level were far more interesting, I will be using edulvla/edulvlb for this final task.
df <- uk_data %>%
filter(!is.na(edu_level) & !is.na(hincfel))
df <- df %>%
mutate(hincfel = recode(as.character(hincfel),
'1' = "Comfortable",
'2' = "Coping",
'3' = "Difficult",
'4' = "Very Difficult",
'7' = NA_character_,
'8' = NA_character_,
'9' = NA_character_,
))
table(df$hincfel)
##
## Comfortable Coping Difficult Very Difficult
## 8340 9061 2646 763
table(df$hincfel, df$edu_level) %>%
cprop() %>%
as.data.frame() %>%
filter(Var1 != "Total",
Var2 != "All") %>%
ggplot(aes(x = Var1, y = Freq, fill = Var2)) +
geom_col(position = "dodge") +
labs(title = "Feeling about household income nowadays in the UK",
y = "Conditional Percentage",
x = "Feeling about household income nowadays",
fill = "At least BA or no BA")
Like I observed in the 4th task, having a Bachelor’s Degree greatly helps you in feeling more comfortable with household income nowadays. Those with a BA show a significant higher likelihood of being comfortable, while those with no BA show a significantly higher likelihood of being in a coping, difficult, or very difficult lifestyle due to (most likely) lower income.
That is all.