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"
setwd("~/Desktop/SOC202/Homework_5_project")
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]
}
TASK 1 Variables Chosen: iphlppl, imprich ,impsafe
finland_data <- ess %>%
filter(cntry == "FI") %>%
mutate(
iphlppl = ifelse(iphlppl %in% c(7, 8, 9), NA, iphlppl),
imprich = ifelse(imprich %in% c(7, 8, 9), NA, imprich),
impsafe = ifelse(impsafe %in% c(7, 8, 9), NA, impsafe)
)
datasummary_skim(finland_data %>% select(iphlppl, imprich, impsafe ))
| Unique (#) | Missing (%) | Mean | SD | Min | Median | Max | ||
|---|---|---|---|---|---|---|---|---|
| iphlppl | 7 | 7 | 2.2 | 0.9 | 1.0 | 2.0 | 6.0 | |
| imprich | 7 | 7 | 4.5 | 1.2 | 1.0 | 5.0 | 6.0 | |
| impsafe | 7 | 7 | 2.5 | 1.2 | 1.0 | 2.0 | 6.0 |
Upon looking on the data summaries, I noted the means for all three variables. The mean for iphppl being 2.2 means it is important to help and care for other people’s well being as the number 2 in this variable is “sounds like me”. The mean for the variable imprich (Important to be rich) is 4.5, where 4 is a little like me and 5 is not like me, so it is safe to assume that the average Finish person does not care about material wealth. The variable impsafe is at 2.5 where 2 is sounds like me and 3 is somewhat like me.
TASK 2 Variable of interest: imprich
imprich_by_year <- finland_data %>%
group_by(year) %>%
summarize(mean_imprich = mean(imprich, na.rm = TRUE))
imprich_by_year
## # A tibble: 10 × 2
## year mean_imprich
## <dbl> <dbl>
## 1 2002 4.33
## 2 2004 4.36
## 3 2006 4.45
## 4 2008 4.49
## 5 2010 4.32
## 6 2012 4.53
## 7 2014 4.54
## 8 2016 4.61
## 9 2018 4.63
## 10 2020 4.62
ggplot(imprich_by_year, aes(x = year, y = mean_imprich)) +
geom_line(color = "blue", size = 1) + # Line to show the trend
geom_point(color = "red", size = 3) + # Points to highlight each year's value
labs(title = "Importance of Being Rich in Finland (2002-2020)",
x = "Survey Year",
y = "Importance (0-10)") +
ylim(0, 10) + # Setting the y-axis limits from 0 to 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.
I note that the average Finnish’s importance on being rich has not
changed much over the years. It has only increased by a few decimal
points every few years, going closer to the value of 5, which is not
like me. The only time the average ever decreased was from 2008 to
2010.
TASK 3 Chosen countries for comparison: Spain (ES) and Hungary (HU)
ess_selected <- ess %>%
filter(cntry %in% c("FI", "ES", "HU")) %>%
mutate(imprich = ifelse(imprich %in% c(7, 8, 9), NA, imprich))
task3plot <- ggplot(ess_selected, aes(x = reorder(cntry, -imprich, FUN=median), y = imprich, fill = cntry)) +
geom_boxplot() +
theme_minimal() +
theme(legend.position = "none") +
labs(title = "Boxplot Comparison for Importance to be Rich (Finland, Spain, Hungary)",
x = "Country",
y = "Scale (0-10)")
task3plot
## Warning: Removed 4635 rows containing non-finite values (`stat_boxplot()`).
I note that Spain and Finland have the same mean of 5 in the importance
to be rich where 5 indicates ‘not like me’, whereas Hungary has a mean
of 4 which is ‘somewhat like me’. The lower outliers are 1 and 2 where 1
is ‘very much like me’ and 2 is ‘like me’.
TASK 4 Chosen variables: Education and Paid Work Status
# Re-coding education
finland_data <- finland_data %>%
mutate(
# Set 'other' to NA for rounds 1-4
edulvla = case_when(
essround < 5 & edulvla == 55 ~ NA_real_,
TRUE ~ edulvla
),
# Set 'other' to NA for rounds 5 and later
edulvlb = case_when(
essround >= 5 & edulvlb == 5555 ~ NA_real_,
TRUE ~ edulvlb
),
# Create educ_level column
educ_level = case_when(
essround < 5 & edulvla == 5 ~ "BA",
essround >= 5 & edulvlb > 600 ~ "BA",
TRUE ~ "No BA"
)
)
table(finland_data$educ_level) # View the table of educ_level
##
## BA No BA
## 5455 14077
finland_data <- finland_data %>%
mutate(pdwrk_recode = case_when(
pdwrk == 1 ~ 'yes',
pdwrk == 0 ~ 'no',
))
# check the recoded variable
table(finland_data$pdwrk_recode)
##
## no yes
## 9355 10177
imprichedu <- datasummary_crosstab(imprich ~ educ_level, data = finland_data)
imprichpdwrk <- datasummary_crosstab(imprich ~ pdwrk_recode, data = finland_data)
imprichedu
| imprich | BA | No BA | All | |
|---|---|---|---|---|
| 1 | N | 32 | 188 | 220 |
| % row | 14.5 | 85.5 | 100.0 | |
| 2 | N | 180 | 648 | 828 |
| % row | 21.7 | 78.3 | 100.0 | |
| 3 | N | 842 | 1825 | 2667 |
| % row | 31.6 | 68.4 | 100.0 | |
| 4 | N | 1394 | 2933 | 4327 |
| % row | 32.2 | 67.8 | 100.0 | |
| 5 | N | 1901 | 4401 | 6302 |
| % row | 30.2 | 69.8 | 100.0 | |
| 6 | N | 861 | 2920 | 3781 |
| % row | 22.8 | 77.2 | 100.0 | |
| All | N | 5455 | 14077 | 19532 |
| % row | 27.9 | 72.1 | 100.0 |
imprichpdwrk
| imprich | no | yes | All | |
|---|---|---|---|---|
| 1 | N | 145 | 75 | 220 |
| % row | 65.9 | 34.1 | 100.0 | |
| 2 | N | 436 | 392 | 828 |
| % row | 52.7 | 47.3 | 100.0 | |
| 3 | N | 1163 | 1504 | 2667 |
| % row | 43.6 | 56.4 | 100.0 | |
| 4 | N | 1719 | 2608 | 4327 |
| % row | 39.7 | 60.3 | 100.0 | |
| 5 | N | 2868 | 3434 | 6302 |
| % row | 45.5 | 54.5 | 100.0 | |
| 6 | N | 2224 | 1557 | 3781 |
| % row | 58.8 | 41.2 | 100.0 | |
| All | N | 9355 | 10177 | 19532 |
| % row | 47.9 | 52.1 | 100.0 |
table(finland_data$imprich, finland_data$educ_level) %>%
cprop()
##
## BA No BA All
## 1 0.6 1.5 1.2
## 2 3.5 5.0 4.6
## 3 16.2 14.1 14.7
## 4 26.8 22.7 23.9
## 5 36.5 34.1 34.8
## 6 16.5 22.6 20.9
## Total 100.0 100.0 100.0
table(finland_data$imprich, finland_data$pdwrk_recode) %>%
cprop()
##
## no yes All
## 1 1.7 0.8 1.2
## 2 5.1 4.1 4.6
## 3 13.6 15.7 14.7
## 4 20.1 27.3 23.9
## 5 33.5 35.9 34.8
## 6 26.0 16.3 20.9
## Total 100.0 100.0 100.0
I note that within the people who have no BAs, 26% of them choce 6 (not like me at all) on the importance of richness, while only 16.5 percent of those with BAs chose 6. Now in terms of paid work, those who do not have paid work, had a higher percentage of choosing 6 which has 26% while those who did have paid work was 16% who chose 6, which was very interesting as it was a total flip.
TASK 5
# double check clean
finland_clean <- finland_data %>%
filter(!is.na(educ_level) & !is.na(imprich))
# calculate conditional probabilities
finland_probs <- finland_clean %>%
count(imprich, educ_level) %>%
group_by(educ_level) %>%
mutate(prob = n / sum(n))
# plot
ggplot(finland_probs, aes(x = as.factor(imprich), y = prob, color = educ_level)) +
geom_point() +
geom_line(aes(group = educ_level)) +
labs(title = "Conditional Probabilities of Importance of Being Rich",
subtitle = "by Level of Education",
x = "Importance Scale",
y = "Probability") +
theme_minimal()
What I notice is that there is a general trend that goes upwards for the
importance scale for both non BA possesers and those with BAs. I also
note that after 5, which is the peak for both parties, it goes down.